Skip to content

Commit

Permalink
Accept smtp_settings
Browse files Browse the repository at this point in the history
  • Loading branch information
janko committed Apr 30, 2015
1 parent 6b009c2 commit 59c72f1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ There are four special headers that simple_mailer processes:

All other headers are used verbatim in the message.

== Configuration

You can pass in options just like with the Mail gem.

SimpleMailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "localhost",
:user_name => "bob",
:password => "secret",
:authentication => :plain,
}

== Testing

Testing support is probably the main reason to use simple_mailer over
Expand Down
19 changes: 18 additions & 1 deletion lib/simple_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module SimpleMailer
extend self

DEFAULT_SMTP_HOST = 'localhost'.freeze
DEFAULT_SMTP_DOMAIN = 'localhost'.freeze

# The array of emails sent
def self.emails_sent
Expand All @@ -37,6 +38,8 @@ def self.test_mode?

# The smtp server to sent email to
attr_accessor :smtp_server
# Custom SMTP configuration for any service (taken from the "mail" gem)
attr_accessor :smtp_settings

# The emails sent in test mode. Is an array of arrays. Each
# element array is a array of three elements, the message, from address,
Expand Down Expand Up @@ -91,7 +94,11 @@ def _send_email(msg, from, to)
if SimpleMailer.test_mode?
test_mode_send_email(msg, from, to)
else
Net::SMTP.start(smtp_server || DEFAULT_SMTP_HOST){|s| s.send_message(msg, from, to)}
domain = smtp_settings[:domain] || DEFAULT_SMTP_DOMAIN
username, password = smtp_settings.values_at(:user_name, :password)
authentication = smtp_settings[:authentication]

smtp.start(domain, username, password, authentication){|s| s.send_message(msg, from, to)}
end
end

Expand All @@ -100,4 +107,14 @@ def _send_email(msg, from, to)
def test_mode_send_email(msg, from, to)
emails_sent << [msg, from, to]
end

def smtp
address = smtp_server || smtp_settings[:address] || DEFAULT_SMTP_HOST
port = smtp_settings[:port] || 25

Net::SMTP.new(address, port).tap do |smtp|
smtp.enable_starttls_auto if smtp_settings[:enable_starttls_auto] == false
smtp.enable_tls if smtp_settings[:tls] || smtp_settings[:ssl]
end
end
end

0 comments on commit 59c72f1

Please sign in to comment.