diff --git a/README b/README index cdce11c..2274992 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.update( + :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..ae5ed51 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,16 @@ 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) + def smtp_settings + @smtp_settings ||= { + :address => smtp_server || DEFAULT_SMTP_HOST, + :port => 25, + :enable_starttls_auto => true, + :domain => DEFAULT_SMTP_DOMAIN, + } + end # 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 +102,9 @@ 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)} + smtp.start(*smtp_settings.values_at(:domain, :user_name, :password, :authentication)) do |s| + s.send_message(msg, from, to) + end end end @@ -100,4 +113,11 @@ def _send_email(msg, from, to) def test_mode_send_email(msg, from, to) emails_sent << [msg, from, to] end + + def smtp + Net::SMTP.new(*smtp_settings.values_at(:address, :port)).tap do |smtp| + smtp.enable_starttls_auto unless smtp_settings[:enable_starttls_auto] == false + smtp.enable_tls if smtp_settings[:tls] || smtp_settings[:ssl] + end + end end diff --git a/spec/simple_mailer.rb b/spec/simple_mailer.rb index a780f06..cf4aba8 100644 --- a/spec/simple_mailer.rb +++ b/spec/simple_mailer.rb @@ -7,16 +7,29 @@ $message = [nil, nil, nil] module Net class SMTP - class Mock - def initialize(host) - @host = host - end - def send_message(msg, from, to) - $message = [msg, from, to, @host] - end + attr_reader :address, :port, :domain, :user_name, :password, :authentication, + :starttls_auto, :tls + + def initialize(address, port = nil) + @address, @port = address, port + end + + def enable_starttls_auto + @starttls_auto = true + end + + def enable_tls + @tls = true + end + + def start(domain = nil, user_name = nil, password = nil, authentication = nil) + @domain, @user_name, @password, @authentication = domain, user_name, password, authentication + yield self end - def self.start(host, *args) - yield(Mock.new(host)) + + def send_message(msg, from, to) + $message = [msg, from, to, @address] + self end end end @@ -29,6 +42,7 @@ module SimpleMailerSpecs end after do @mailer.smtp_server = nil + @mailer.instance_variable_set(:@smtp_settings, nil) SimpleMailer.instance_variable_set(:@test_mode, false) end @@ -127,6 +141,44 @@ module SimpleMailerSpecs END_MESSAGE SimpleMailer.instance_variable_set(:@test_mode, false) end + + it "should give proper default smtp settings" do + @mailer.send(:smtp).address.must_equal 'localhost' + @mailer.send(:smtp).port.must_equal 25 + @mailer.send(:smtp).starttls_auto.must_equal true + + smtp = @mailer.send_email('from@from.com', 'to@to.com', 'Test Subject', 'Test Body') + smtp.domain.must_equal 'localhost' + end + + it "should take smtp_server as the address" do + @mailer.smtp_server = 'blah.com' + @mailer.send(:smtp).address.must_equal 'blah.com' + end + + it "should have configurable smtp settings" do + @mailer.smtp_settings.update( + :address => 'smtp.gmail.com', + :port => 587, + :user_name => 'bob', + :password => 'secret', + :domain => 'mydomain.com', + :authentication => :plain, + :enable_starttls_auto => false, + :tls => true, + ) + + @mailer.send(:smtp).address.must_equal 'smtp.gmail.com' + @mailer.send(:smtp).port.must_equal 587 + @mailer.send(:smtp).starttls_auto.must_equal nil + @mailer.send(:smtp).tls.must_equal true + + smtp = @mailer.send_email('from@from.com', 'to@to.com', 'Test Subject', 'Test Body') + smtp.domain.must_equal 'mydomain.com' + smtp.user_name.must_equal 'bob' + smtp.password.must_equal 'secret' + smtp.authentication.must_equal :plain + end end describe "SimpleMailer module itself" do