diff --git a/actionmailer/CHANGELOG.md b/actionmailer/CHANGELOG.md index af91907f46..6fc25b3bb4 100644 --- a/actionmailer/CHANGELOG.md +++ b/actionmailer/CHANGELOG.md @@ -2,7 +2,7 @@ * Explicit multipart messages no longer set the order of the MIME parts. *Nate Berkopec* - + * Do not render views when mail() isn't called. Fix #7761 diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index dcf13e927c..95a680ac23 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -499,6 +499,7 @@ def method_missing(method_name, *args) # method, for instance). def initialize(method_name=nil, *args) super() + @_mail_was_called = false @_message = Mail.new process(method_name, *args) if method_name end @@ -506,7 +507,8 @@ def initialize(method_name=nil, *args) def process(*args) #:nodoc: lookup_context.skip_default_locale! - @_message = NullMail.new unless super + super + @_message = NullMail.new unless @_mail_was_called end class NullMail #:nodoc: @@ -666,6 +668,7 @@ def attachments # end # def mail(headers={}, &block) + @_mail_was_called = true m = @_message # At the beginning, do not consider class default for parts order neither content_type diff --git a/actionmailer/test/base_test.rb b/actionmailer/test/base_test.rb index 4a608a9ad4..170923673b 100644 --- a/actionmailer/test/base_test.rb +++ b/actionmailer/test/base_test.rb @@ -505,6 +505,12 @@ def teardown mail.deliver end + test 'the return value of mailer methods is not relevant' do + mail = BaseMailer.with_nil_as_return_value + assert_equal('Welcome', mail.body.to_s.strip) + mail.deliver + end + # Before and After hooks class MyObserver diff --git a/actionmailer/test/mailers/base_mailer.rb b/actionmailer/test/mailers/base_mailer.rb index 52342bc59e..8fca6177bd 100644 --- a/actionmailer/test/mailers/base_mailer.rb +++ b/actionmailer/test/mailers/base_mailer.rb @@ -118,4 +118,9 @@ def email_with_translations def without_mail_call end + + def with_nil_as_return_value(hash = {}) + mail(:template_name => "welcome") + nil + end end