Migrated over to Mail doing delivery.

This commit is contained in:
Mikel Lindsaar 2010-01-16 22:10:11 +11:00 committed by José Valim
parent 5b0c8a1266
commit 0750304c01
5 changed files with 125 additions and 45 deletions

View File

@ -355,20 +355,42 @@ module ActionMailer #:nodoc:
alias :controller_path :mailer_name
class << self
attr_writer :mailer_name
delegate :settings, :settings=, :to => ActionMailer::DeliveryMethod::File, :prefix => :file
delegate :settings, :settings=, :to => ActionMailer::DeliveryMethod::Sendmail, :prefix => :sendmail
delegate :settings, :settings=, :to => ActionMailer::DeliveryMethod::Smtp, :prefix => :smtp
def mailer_name
@mailer_name ||= name.underscore
end
alias :controller_path :mailer_name
attr_writer :mailer_name
def delivery_method=(method_name)
@delivery_method = ActionMailer::DeliveryMethod.lookup_method(method_name)
def file_settings
@file_settings ||= {:location => defined?(Rails.root) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails"}
end
attr_writer :file_settings
def sendmail_settings
@sendmail_settings ||= { :location => '/usr/sbin/sendmail',
:arguments => '-i -t' }
end
attr_writer :sendmail_settings
def smtp_settings
@smtp_settings ||= { :address => "localhost",
:port => 25,
:domain => 'localhost.localdomain',
:user_name => nil,
:password => nil,
:authentication => nil,
:enable_starttls_auto => true }
end
attr_writer :smtp_settings
def custom_settings
@custom_settings ||= {}
end
attr_writer :custom_settings
attr_writer :delivery_method
alias :controller_path :mailer_name
def respond_to?(method_symbol, include_private = false) #:nodoc:
matches_dynamic_method?(method_symbol) || super
@ -413,7 +435,35 @@ module ActionMailer #:nodoc:
# email.set_some_obscure_header "frobnicate"
# MyMailer.deliver(email)
def deliver(mail)
new.deliver!(mail)
raise "no mail object available for delivery!" unless mail
begin
ActiveSupport::Notifications.instrument("action_mailer.deliver",
:mailer => self.name) do |payload|
set_payload_for_mail(payload, mail)
mail.delivery_method delivery_method, delivery_settings
if @@perform_deliveries
mail.deliver!
self.deliveries << mail
end
end
rescue Exception => e # Net::SMTP errors or sendmail pipe errors
raise e if raise_delivery_errors
end
mail
end
# Get the delivery settings set. This is set using the <tt>:smtp_settings</tt>,
# <tt>:sendmail_settings</tt>, <tt>:file_settings</tt> or <tt>:custom_setings</tt>
# options hashes. You can set <tt>:custom_settings</tt> if you are providing
# your own Custom Delivery Method and want to pass options to it.
def delivery_settings
if [:smtp, :sendmail, :file].include?(delivery_method)
instance_variable_get("@#{delivery_method}_settings")
else
@custom_settings
end
end
def template_root
@ -506,19 +556,7 @@ module ActionMailer #:nodoc:
# object (from the <tt>create!</tt> method). If no cached mail object exists, and
# no alternate has been given as the parameter, this will fail.
def deliver!(mail = @mail)
raise "no mail object available for delivery!" unless mail
begin
ActiveSupport::Notifications.instrument("action_mailer.deliver",
:template => template, :mailer => self.class.name) do |payload|
self.class.set_payload_for_mail(payload, mail)
self.delivery_method.perform_delivery(mail) if perform_deliveries
end
rescue Exception => e # Net::SMTP errors or sendmail pipe errors
raise e if raise_delivery_errors
end
mail
self.class.deliver(mail)
end
private
@ -533,6 +571,11 @@ module ActionMailer #:nodoc:
@mime_version ||= @@default_mime_version.dup if @@default_mime_version
@mailer_name ||= self.class.mailer_name.dup
@delivery_method = self.class.delivery_method
@smtp_settings = self.class.smtp_settings.dup
@sendmail_settings = self.class.sendmail_settings.dup
@file_settings = self.class.file_settings.dup
@custom_settings = self.class.custom_settings.dup
@template ||= method_name
@parts ||= []

View File

@ -26,6 +26,7 @@ FIXTURE_LOAD_PATH = File.join(File.dirname(__FILE__), 'fixtures')
ActionMailer::Base.template_root = FIXTURE_LOAD_PATH
class MockSMTP
def self.deliveries
@@deliveries
end
@ -41,6 +42,7 @@ class MockSMTP
def start(*args)
yield self
end
end
class Net::SMTP
@ -57,9 +59,9 @@ rescue LoadError
$stderr.puts "Skipping #{test_name} tests. `gem install #{gem_name}` and try again."
end
def set_delivery_method(delivery_method)
def set_delivery_method(method)
@old_delivery_method = ActionMailer::Base.delivery_method
ActionMailer::Base.delivery_method = delivery_method
ActionMailer::Base.delivery_method = method
end
def restore_delivery_method

View File

@ -1,4 +1,5 @@
require 'abstract_unit'
require 'mail'
class DefaultDeliveryMethodMailer < ActionMailer::Base
end
@ -12,18 +13,22 @@ class FileDeliveryMethodMailer < ActionMailer::Base
end
class CustomDeliveryMethod
attr_accessor :custom_deliveries
def initialize()
@customer_deliveries = []
def initialize(values)
@custom_deliveries = []
end
def self.perform_delivery(mail)
attr_accessor :custom_deliveries
attr_accessor :settings
def deliver!(mail)
self.custom_deliveries << mail
end
end
class CustomerDeliveryMailer < ActionMailer::Base
self.delivery_method = CustomDeliveryMethod.new
self.delivery_method = CustomDeliveryMethod
end
class ActionMailerBase_delivery_method_Test < Test::Unit::TestCase
@ -36,7 +41,18 @@ class ActionMailerBase_delivery_method_Test < Test::Unit::TestCase
end
def test_should_be_the_default_smtp
assert_instance_of ActionMailer::DeliveryMethod::Smtp, ActionMailer::Base.delivery_method
assert_equal :smtp, ActionMailer::Base.delivery_method
end
def test_should_have_default_smtp_delivery_method_settings
settings = { :address => "localhost",
:port => 25,
:domain => 'localhost.localdomain',
:user_name => nil,
:password => nil,
:authentication => nil,
:enable_starttls_auto => true }
assert_equal settings, ActionMailer::Base.smtp_settings
end
end
@ -50,7 +66,18 @@ class DefaultDeliveryMethodMailer_delivery_method_Test < Test::Unit::TestCase
end
def test_should_be_the_default_smtp
assert_instance_of ActionMailer::DeliveryMethod::Smtp, DefaultDeliveryMethodMailer.delivery_method
assert_equal :smtp, DefaultDeliveryMethodMailer.delivery_method
end
def test_should_have_default_smtp_delivery_method_settings
settings = { :address => "localhost",
:port => 25,
:domain => 'localhost.localdomain',
:user_name => nil,
:password => nil,
:authentication => nil,
:enable_starttls_auto => true }
assert_equal settings, DefaultDeliveryMethodMailer.smtp_settings
end
end
@ -64,7 +91,13 @@ class NonDefaultDeliveryMethodMailer_delivery_method_Test < Test::Unit::TestCase
end
def test_should_be_the_set_delivery_method
assert_instance_of ActionMailer::DeliveryMethod::Sendmail, NonDefaultDeliveryMethodMailer.delivery_method
assert_equal :sendmail, NonDefaultDeliveryMethodMailer.delivery_method
end
def test_should_have_default_sendmail_delivery_method_settings
settings = {:location => '/usr/sbin/sendmail',
:arguments => '-i -t'}
assert_equal settings, NonDefaultDeliveryMethodMailer.sendmail_settings
end
end
@ -78,11 +111,12 @@ class FileDeliveryMethodMailer_delivery_method_Test < Test::Unit::TestCase
end
def test_should_be_the_set_delivery_method
assert_instance_of ActionMailer::DeliveryMethod::File, FileDeliveryMethodMailer.delivery_method
assert_equal :file, FileDeliveryMethodMailer.delivery_method
end
def test_should_default_location_to_the_tmpdir
assert_equal "#{Dir.tmpdir}/mails", ActionMailer::Base.file_settings[:location]
def test_should_have_default_file_delivery_method_settings
settings = {:location => "#{Dir.tmpdir}/mails"}
assert_equal settings, FileDeliveryMethodMailer.file_settings
end
end
@ -96,6 +130,11 @@ class CustomDeliveryMethodMailer_delivery_method_Test < Test::Unit::TestCase
end
def test_should_be_the_set_delivery_method
assert_instance_of CustomDeliveryMethod, CustomerDeliveryMailer.delivery_method
assert_equal CustomDeliveryMethod, CustomerDeliveryMailer.delivery_method
end
def test_should_have_default_custom_delivery_method_settings
settings = {}
assert_equal settings, CustomerDeliveryMailer.custom_settings
end
end

View File

@ -546,7 +546,7 @@ class ActionMailerTest < Test::Unit::TestCase
assert_not_nil mail
mail, from, to = mail
assert_equal 'system@loudthinking.com', from.addresses.first
assert_equal 'system@loudthinking.com', from
end
def test_reply_to
@ -675,16 +675,13 @@ class ActionMailerTest < Test::Unit::TestCase
def test_doesnt_raise_errors_when_raise_delivery_errors_is_false
ActionMailer::Base.raise_delivery_errors = false
TestMailer.delivery_method.expects(:perform_delivery).raises(Exception)
Mail::Message.any_instance.expects(:deliver!).raises(Exception)
assert_nothing_raised { TestMailer.deliver_signed_up(@recipient) }
end
def test_performs_delivery_via_sendmail
sm = mock()
sm.expects(:print).with(anything)
sm.expects(:flush)
IO.expects(:popen).once.with('/usr/sbin/sendmail -i -t', 'w+').yields(sm)
ActionMailer::Base.delivery_method = :sendmail
IO.expects(:popen).once.with('/usr/sbin/sendmail -i -t test@localhost', 'w+')
TestMailer.delivery_method = :sendmail
TestMailer.deliver_signed_up(@recipient)
end
@ -1113,7 +1110,6 @@ EOF
def test_starttls_is_not_enabled
ActionMailer::Base.smtp_settings[:enable_starttls_auto] = false
MockSMTP.any_instance.expects(:respond_to?).never
MockSMTP.any_instance.expects(:enable_starttls_auto).never
ActionMailer::Base.delivery_method = :smtp
TestMailer.deliver_signed_up(@recipient)
ensure

View File

@ -12,7 +12,7 @@ end
class TestHelperMailerTest < ActionMailer::TestCase
def test_setup_sets_right_action_mailer_options
assert_instance_of ActionMailer::DeliveryMethod::Test, ActionMailer::Base.delivery_method
assert_equal :test, ActionMailer::Base.delivery_method
assert ActionMailer::Base.perform_deliveries
assert_equal [], ActionMailer::Base.deliveries
end