allegutta
allegutta

Reputation: 5644

How to attach a PDF to an email sent from an express app

I am trying to send an email with a PDF attachment, but the PDF cannot be opened. (How can I load a PDF from the filesystem and include that in an email that is sent from an express app?)

This is what I have now: I load the PDF from the filesystm like this: var pdfAttach = fs.readFileSync('./pdfs/test.pdf', 'binary'); and include it in the object that is sent to Mandrill like this:

{
...
attachments:
  [{content: pdfAttach,
    name: "testing.pdf",
    type: "application/pdf"
  }],
...
}

The recipient gets an email with an PDF file that he cannot open. Any ideas on how to solve this?

Upvotes: 1

Views: 1632

Answers (1)

cassini
cassini

Reputation: 365

If you take a look to Mandrill's Messages API here, you will see that the content of the attachment must be a base64 encoded string. so the code would be:

var pdfAttach = fs.readFileSync('./pdfs/test.pdf');
...
attachments:
  [{content: pdfAttach.toString('base64'),
    name: "testing.pdf",
    type: "application/pdf"
}],
...

Hope this helps.

Upvotes: 1

Related Questions