Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept smtp_settings #1

Merged
merged 1 commit into from
May 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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
Expand Down
22 changes: 21 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,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,
Expand Down Expand Up @@ -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

Expand All @@ -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
70 changes: 61 additions & 9 deletions spec/simple_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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('[email protected]', '[email protected]', '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('[email protected]', '[email protected]', '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
Expand Down