method

attachment

attachment(params, &block)
public

Add an attachment to a multipart message. This is simply a part with the content-disposition set to "attachment".

3Notes

Static and dynamic attachments

mikael · Feb 20, 20095 thanks

You can attach static files directly:

attachment :content_type => "image/jpeg", :body => File.read("someimage.jpg")

and you can also define attachments dynamically by using a block:

attachment "text/csv" do |a| a.body = my_data.to_csv end

Attachment's name

himn1 · Sep 8, 20092 thanks

Files attached in a standard way are shown up as "noname". You can specify any name by using the :filename key:

attachment :content_type => "application/pdf",
         :filename     => "Othersheet.pdf",
         :body         => File.read("example.pdf")

attachments and implicit multipart

mpearce · Apr 30, 2009

There is a small gotcha - this caught me up for a while.

If you are using implicit multipart mime types by naming your template xxx.text.html.erb and xxx.text.plain.erb, you will need to change your template name back to the original xxx.erb.

If you use the implicit template name, your attachment will be the only thing in the body of the message - it will ignore your template.

See the "Multipart email" section of the ActionMailer.base documentation.