user3483782 user3483782 - 17 days ago 9x
Node.js Question

attach PDF in node js using sendgrid helper not working properly

I am using Sendgrid (with helper) to send emails from nodeJs server.
When I send a PDF file as an attachment the response is 200 OK and am receiving the mail with PDF attached. But that file was not opening , when I try to open showing the following error.

"There was a problem previewing this document."

I am using "pdfkit" module to create a pdf file.

Here is my code.

var doc = new PDFDocument({
size: 'letter'
doc.text('my text11111')
fs.readFile('will.pdf', function (err, data) {
mailer.sendMail([{ name: '', email: '' }], 'PDF TEST', "PDF TEST BODY", { 'base64Content': data.toString('base64'), 'fileName': 'test', 'fileType': 'pdf' });

In the send mail function the attachment is generated as follows.

function sendMail(toList, subject, message, attachmentObj) {
if (attachmentObj) {
var attachment = new helper.Attachment()
attachment.setType("application/" + attachmentObj.fileType)
attachment.setFilename(attachmentObj.fileName + "." + attachmentObj.fileType)


I faced the same issue with this...

Actually i was given the relative path of the file and was facing the same issue of Previewing the document sent in mail then i tried with giving the absolute (full path) and it worked.

So you can give absolute path and then see.


Attachments parameter accept an array so try passing array as parameter like given below:

  attachments: [{   // file on disk as an attachment
            filename: 'text3.txt',
            path: '/path/to/file.txt' // stream this file