The short — canonical — way to attach PDFs to Mailers in Rails 4+

You will probably need to send a PDF as a mail attachment at some point. I often need to look up how to do this right, because, if you just take the pdf and attach it, chances are it will be corrupt and people can't open it.

Now I'll write down on my blog how to deal with this, and have it as a reference for later.

Alright, take the safe approach and specify the read mode as read-only and binary. Furthermore set the mime_type to "application/pdf".

path = File.join(Rails.root, "public", "system", "uploads", "store", "my_pdf_file.pdf")  
attachments["my_pdf_file.pdf"] = {  
        mime_type: "application/pdf",
        content: File.read(path, mode: "rb")
      }

With these settings I can send pdf attachments with Rails Mailers without any problems, and I can be sure the recipients will be able to open and read the PDFs. No matter if they are on a Windows machine, a Mac, Linux or in space.