diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-07-29 09:21:47 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-07-29 09:24:06 +0200 |
commit | b8120ab14d695d5ae7392193a6135b5e7cef9c80 (patch) | |
tree | 0f9115cf688c19e6b3180aca03a2e844a399493e /actionmailer/lib | |
parent | 6924f848c45897004ba9cc4f96c032286dcce346 (diff) | |
download | rails-b8120ab14d695d5ae7392193a6135b5e7cef9c80.tar.gz rails-b8120ab14d695d5ae7392193a6135b5e7cef9c80.tar.bz2 rails-b8120ab14d695d5ae7392193a6135b5e7cef9c80.zip |
Raise an exception when attachments are added after `mail` was called.
Closes #16163
Adding attachments after a call to `mail` will result in invalid emails.
This is related to the fact, that `mail` is making the required preparations
before the email is ready to be sent. These change depending on your
added attachments.
Diffstat (limited to 'actionmailer/lib')
-rw-r--r-- | actionmailer/lib/action_mailer/base.rb | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 048f2ddc35..d9b88ec608 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -649,7 +649,22 @@ module ActionMailer # mail.attachments[0] # => Mail::Part (first attachment) # def attachments - @_message.attachments + if @_mail_was_called + LateAttachmentsProxy.new(@_message.attachments) + else + @_message.attachments + end + end + + class LateAttachmentsProxy < SimpleDelegator + def inline; _raise_error end + def []=(_name, _content); _raise_error end + + private + def _raise_error + raise RuntimeError, "Can't add attachments after `mail` was called.\n" \ + "Make sure to use `attachments[]=` before calling `mail`." + end end # The main method that creates the message and renders the email templates. There are |