Send PDF attachment through mail


#1

How to send the PDF attachment in the mail?

I’m using below code for sending pdf attachment mail. Here the mail functionality working properly and I’m getting the pdf file in the attachment through the mail. But when I open the pdf file, got the error ‘Failed to load PDF document’. I think, there can be the contentType issue.

 const data = fs.readFileSync('/home/rails/project/check/stackoverflow23.pdf');

  console.log('data',data)

  Email.send({
    from: `from-address`,
    to: 'to-address',
    subject: 'mySubject',
    text: 'sssswwwwws',
    attachments: [{'filename': 'report.pdf', 'contents': data}]
  });

Kindly let me know what is the issue here and how to resolve this?

Note: PDF file is correct.


#2

contents should be content and you need to add a contentType header:

attachments: [{filename: 'report.pdf', content: data, contentType: 'application/pdf'}]