Setting the Attachment Filename in ActionMailer

November 18, 2010

For the life of me, I couldn't find ANY rails 2.3.x documentation that specified the options to pass into the attachment block within an ActionMailer model. So, I hope you stumble on this early in your google searches.

ANSWER: set the :filename hash value

To give you some context, I was creating a PDF from HTML with PDFKit and setting it as an attachment within an email. I kept generating a generic file name "Mail Attachment.pdf" with the following code:


attachment "application/pdf" do |a|
  a.body = PDFKit.new(path_to_url).to_pdf
end

All I had to do was set the :filename attribute. That's it [as I place a dunce cap on head].


attachment "application/pdf" do |a|
  a.body = PDFKit.new(path_to_url).to_pdf
  a.filename = "filename.pdf"
end

@drydevelopment