diff --git a/README b/README index cdce11c..9af9665 100644 --- a/README +++ b/README @@ -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 diff --git a/lib/simple_mailer.rb b/lib/simple_mailer.rb index 2164c74..f126813 100644 --- a/lib/simple_mailer.rb +++ b/lib/simple_mailer.rb @@ -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 @@ -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, @@ -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 @@ -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