sendgrid-nodejs: Unable to send attachments

I am unable to send attachments. In the email, I see the attachment with the correct name, but when I click on it to open, it says, “Failed to load PDF document”.

I have followed the step in demo examples.

` var attachment = new helper.Attachment(); var file = fs.readFileSync(‘/home/vcap/app/common/models/scc.txt’); var base64File = new Buffer(file).toString(‘base64’); attachment.setContent(base64File);

          attachment.setType("application/pdf")
          attachment.setFilename("TheBeast.pdf")
          attachment.setDisposition("attachment")
          mail.addAttachment(attachment);`

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 16 (8 by maintainers)

Most upvoted comments

Here is what worked for me:

mkdir nodejs
cd nodejs
mkdir node_modules
npm install --save sendgrid
cp ~/Downloads/your.pdf .
vi temp.js
node temp.js

Following is the contents of temp.js:

var helper = require('sendgrid').mail;
var sg = require('sendgrid')(process.env.SENDGRID_API_KEY);
var fs = require('fs');

var mail = new helper.Mail();
var email = new helper.Email('dx@sendgrid.com', 'Example User');
mail.setFrom(email);

mail.setSubject('Hello World from the SendGrid Node.js Library');

var personalization = new helper.Personalization();
email = new helper.Email('elmer.thomas@sendgrid.com', 'Example User');
personalization.addTo(email);
mail.addPersonalization(personalization);

var content = new helper.Content('text/html', '<html><body>some text here</body></html>')
mail.addContent(content);

var attachment = new helper.Attachment();
var file = fs.readFileSync('your.pdf');
var base64File = new Buffer(file).toString('base64');
attachment.setContent(base64File);
attachment.setType('application/pdf');
attachment.setFilename('my_file.pdf');
attachment.setDisposition('attachment');
mail.addAttachment(attachment);

var request = sg.emptyRequest({
  method: 'POST',
  path: '/v3/mail/send',
  body: mail.toJSON(),
});

sg.API(request, function(err, response) {
  console.log(response.statusCode);
  console.log(response.body);
  console.log(response.headers);
});

I’m guessing your original .pdf is not being loaded correctly.

Evening, I am also struggling with this, my attachments come through with correct name and bytesize but wont open, prompted with “damage file / incorrectly decoded”.

Here is my code:

const msg = {
    to: req.body.sendEmail,
    from: req.body.email,
    subject: 'Lets attach it',
    html: output,
    attachments: [
    {
        content: new Buffer(req.body.filetoSync).toString('base64'),
        filename: req.body.filetoSync,
        type: 'plain/text',
        disposition: 'attachment',
        contentId: 'mytext'
    },
],

};

Hello @Abdul993,

Do you have a code sample you can share?

Thanks!

With Best Regards,

Elmer

Hi @thinkingserious I am facing exact same issue with attaching PDFs in email. The email I get has the attachments but its just a blank page, not the file I sent. Also I checked the contents of attachments, it has some gibberish which does not open with a PDF reader.

Here is sample code for reference. Can you help me with this ?

var helper = require('sendgrid').mail;
var fileUrl = " A s3 File URL for PDF file"
var filename = "filename.pdf"
var mail = new helper.Mail(); // Main mail object
// Do a ton of stuff with mail object.

// fetch attachment file from s3 and add it to mail object.
S3.get(fileUrl, function(err, attach) {
  if (err || !attach)
    return callback(err);

  if (!err && attach) {
    var att = new helper.Attachment()
    // encode content of file in base64. required by SG.
    var encodedString = new Buffer(attach).toString('base64');
    att.setContent(encodedString);
    att.setType("application/pdf")
    att.setFilename(filename);
    att.setDisposition("attachment");
    mail.addAttachment(att);
    var toSend = mail.toJSON();
    
    // send the mail object to send function.
    // sg_send(sendgrid, toSend, callback);
  }
});