mirror of https://github.com/rails/rails
Migrated over to Mail doing delivery.
This commit is contained in:
parent
5b0c8a1266
commit
0750304c01
|
@ -355,20 +355,42 @@ module ActionMailer #:nodoc:
|
||||||
alias :controller_path :mailer_name
|
alias :controller_path :mailer_name
|
||||||
|
|
||||||
class << self
|
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
|
def mailer_name
|
||||||
@mailer_name ||= name.underscore
|
@mailer_name ||= name.underscore
|
||||||
end
|
end
|
||||||
alias :controller_path :mailer_name
|
attr_writer :mailer_name
|
||||||
|
|
||||||
def delivery_method=(method_name)
|
def file_settings
|
||||||
@delivery_method = ActionMailer::DeliveryMethod.lookup_method(method_name)
|
@file_settings ||= {:location => defined?(Rails.root) ? "#{Rails.root}/tmp/mails" : "#{Dir.tmpdir}/mails"}
|
||||||
end
|
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:
|
def respond_to?(method_symbol, include_private = false) #:nodoc:
|
||||||
matches_dynamic_method?(method_symbol) || super
|
matches_dynamic_method?(method_symbol) || super
|
||||||
|
@ -413,7 +435,35 @@ module ActionMailer #:nodoc:
|
||||||
# email.set_some_obscure_header "frobnicate"
|
# email.set_some_obscure_header "frobnicate"
|
||||||
# MyMailer.deliver(email)
|
# MyMailer.deliver(email)
|
||||||
def deliver(mail)
|
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
|
end
|
||||||
|
|
||||||
def template_root
|
def template_root
|
||||||
|
@ -506,19 +556,7 @@ module ActionMailer #:nodoc:
|
||||||
# object (from the <tt>create!</tt> method). If no cached mail object exists, and
|
# 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.
|
# no alternate has been given as the parameter, this will fail.
|
||||||
def deliver!(mail = @mail)
|
def deliver!(mail = @mail)
|
||||||
raise "no mail object available for delivery!" unless mail
|
self.class.deliver(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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -533,6 +571,11 @@ module ActionMailer #:nodoc:
|
||||||
@mime_version ||= @@default_mime_version.dup if @@default_mime_version
|
@mime_version ||= @@default_mime_version.dup if @@default_mime_version
|
||||||
|
|
||||||
@mailer_name ||= self.class.mailer_name.dup
|
@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
|
@template ||= method_name
|
||||||
|
|
||||||
@parts ||= []
|
@parts ||= []
|
||||||
|
|
|
@ -26,6 +26,7 @@ FIXTURE_LOAD_PATH = File.join(File.dirname(__FILE__), 'fixtures')
|
||||||
ActionMailer::Base.template_root = FIXTURE_LOAD_PATH
|
ActionMailer::Base.template_root = FIXTURE_LOAD_PATH
|
||||||
|
|
||||||
class MockSMTP
|
class MockSMTP
|
||||||
|
|
||||||
def self.deliveries
|
def self.deliveries
|
||||||
@@deliveries
|
@@deliveries
|
||||||
end
|
end
|
||||||
|
@ -41,6 +42,7 @@ class MockSMTP
|
||||||
def start(*args)
|
def start(*args)
|
||||||
yield self
|
yield self
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class Net::SMTP
|
class Net::SMTP
|
||||||
|
@ -57,9 +59,9 @@ rescue LoadError
|
||||||
$stderr.puts "Skipping #{test_name} tests. `gem install #{gem_name}` and try again."
|
$stderr.puts "Skipping #{test_name} tests. `gem install #{gem_name}` and try again."
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_delivery_method(delivery_method)
|
def set_delivery_method(method)
|
||||||
@old_delivery_method = ActionMailer::Base.delivery_method
|
@old_delivery_method = ActionMailer::Base.delivery_method
|
||||||
ActionMailer::Base.delivery_method = delivery_method
|
ActionMailer::Base.delivery_method = method
|
||||||
end
|
end
|
||||||
|
|
||||||
def restore_delivery_method
|
def restore_delivery_method
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
require 'abstract_unit'
|
require 'abstract_unit'
|
||||||
|
require 'mail'
|
||||||
|
|
||||||
class DefaultDeliveryMethodMailer < ActionMailer::Base
|
class DefaultDeliveryMethodMailer < ActionMailer::Base
|
||||||
end
|
end
|
||||||
|
@ -12,18 +13,22 @@ class FileDeliveryMethodMailer < ActionMailer::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
class CustomDeliveryMethod
|
class CustomDeliveryMethod
|
||||||
attr_accessor :custom_deliveries
|
|
||||||
def initialize()
|
def initialize(values)
|
||||||
@customer_deliveries = []
|
@custom_deliveries = []
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.perform_delivery(mail)
|
attr_accessor :custom_deliveries
|
||||||
|
|
||||||
|
attr_accessor :settings
|
||||||
|
|
||||||
|
def deliver!(mail)
|
||||||
self.custom_deliveries << mail
|
self.custom_deliveries << mail
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class CustomerDeliveryMailer < ActionMailer::Base
|
class CustomerDeliveryMailer < ActionMailer::Base
|
||||||
self.delivery_method = CustomDeliveryMethod.new
|
self.delivery_method = CustomDeliveryMethod
|
||||||
end
|
end
|
||||||
|
|
||||||
class ActionMailerBase_delivery_method_Test < Test::Unit::TestCase
|
class ActionMailerBase_delivery_method_Test < Test::Unit::TestCase
|
||||||
|
@ -36,7 +41,18 @@ class ActionMailerBase_delivery_method_Test < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_should_be_the_default_smtp
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -50,7 +66,18 @@ class DefaultDeliveryMethodMailer_delivery_method_Test < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_should_be_the_default_smtp
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -64,7 +91,13 @@ class NonDefaultDeliveryMethodMailer_delivery_method_Test < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_should_be_the_set_delivery_method
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -78,11 +111,12 @@ class FileDeliveryMethodMailer_delivery_method_Test < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_should_be_the_set_delivery_method
|
def test_should_be_the_set_delivery_method
|
||||||
assert_instance_of ActionMailer::DeliveryMethod::File, FileDeliveryMethodMailer.delivery_method
|
assert_equal :file, FileDeliveryMethodMailer.delivery_method
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_should_default_location_to_the_tmpdir
|
def test_should_have_default_file_delivery_method_settings
|
||||||
assert_equal "#{Dir.tmpdir}/mails", ActionMailer::Base.file_settings[:location]
|
settings = {:location => "#{Dir.tmpdir}/mails"}
|
||||||
|
assert_equal settings, FileDeliveryMethodMailer.file_settings
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -96,6 +130,11 @@ class CustomDeliveryMethodMailer_delivery_method_Test < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_should_be_the_set_delivery_method
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -546,7 +546,7 @@ class ActionMailerTest < Test::Unit::TestCase
|
||||||
assert_not_nil mail
|
assert_not_nil mail
|
||||||
mail, from, to = mail
|
mail, from, to = mail
|
||||||
|
|
||||||
assert_equal 'system@loudthinking.com', from.addresses.first
|
assert_equal 'system@loudthinking.com', from
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_reply_to
|
def test_reply_to
|
||||||
|
@ -675,16 +675,13 @@ class ActionMailerTest < Test::Unit::TestCase
|
||||||
|
|
||||||
def test_doesnt_raise_errors_when_raise_delivery_errors_is_false
|
def test_doesnt_raise_errors_when_raise_delivery_errors_is_false
|
||||||
ActionMailer::Base.raise_delivery_errors = 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) }
|
assert_nothing_raised { TestMailer.deliver_signed_up(@recipient) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_performs_delivery_via_sendmail
|
def test_performs_delivery_via_sendmail
|
||||||
sm = mock()
|
IO.expects(:popen).once.with('/usr/sbin/sendmail -i -t test@localhost', 'w+')
|
||||||
sm.expects(:print).with(anything)
|
TestMailer.delivery_method = :sendmail
|
||||||
sm.expects(:flush)
|
|
||||||
IO.expects(:popen).once.with('/usr/sbin/sendmail -i -t', 'w+').yields(sm)
|
|
||||||
ActionMailer::Base.delivery_method = :sendmail
|
|
||||||
TestMailer.deliver_signed_up(@recipient)
|
TestMailer.deliver_signed_up(@recipient)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1113,7 +1110,6 @@ EOF
|
||||||
def test_starttls_is_not_enabled
|
def test_starttls_is_not_enabled
|
||||||
ActionMailer::Base.smtp_settings[:enable_starttls_auto] = false
|
ActionMailer::Base.smtp_settings[:enable_starttls_auto] = false
|
||||||
MockSMTP.any_instance.expects(:respond_to?).never
|
MockSMTP.any_instance.expects(:respond_to?).never
|
||||||
MockSMTP.any_instance.expects(:enable_starttls_auto).never
|
|
||||||
ActionMailer::Base.delivery_method = :smtp
|
ActionMailer::Base.delivery_method = :smtp
|
||||||
TestMailer.deliver_signed_up(@recipient)
|
TestMailer.deliver_signed_up(@recipient)
|
||||||
ensure
|
ensure
|
||||||
|
|
|
@ -12,7 +12,7 @@ end
|
||||||
|
|
||||||
class TestHelperMailerTest < ActionMailer::TestCase
|
class TestHelperMailerTest < ActionMailer::TestCase
|
||||||
def test_setup_sets_right_action_mailer_options
|
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 ActionMailer::Base.perform_deliveries
|
||||||
assert_equal [], ActionMailer::Base.deliveries
|
assert_equal [], ActionMailer::Base.deliveries
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue