Sending Multi-part Emails in Rails with Attachments

If ActionMailer detects that you have multiple view formats for your mailer, it will automatically use these templates as a multi-part email.

You may be surprised to find that if you add an attachment to your mail, this is no longer the case.

Implicit template rendering is not performed if any attachments or parts have been added to the email. This means that you'll have to manually add each part to the email and set the content type of the email to multipart/alternative.

You can restore the order of things by explicitly stating the templates you wish to use, like so:

attachments["attachment.pdf"] = some_file

  to:      "",
  from:    "",
  subject: "Here's a multi-part email with an attachment!"
) do |format|
   format.html { render "some_template" }
   format.text { render "some_template" }

Piece of cake.

Respond to this post via e-mail