Flowdock
method

attachment

Importance_3
v1.1.6 - Show latest stable - 3 notes - Class: ActionMailer::PartContainer
attachment(params, &block) public

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

Show source
Register or log in to add new notes.
February 20, 2009
5 thanks

Static and dynamic attachments

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
September 9, 2009
2 thanks

Attachment's name

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")
April 30, 2009
0 thanks

attachments and implicit multipart

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.