Sendgrid-nodejs: How do I add attachments?

Created on 6 Sep 2016  ·  10Comments  ·  Source: sendgrid/sendgrid-nodejs

Our emails are sending fine and we now want to add an attachment. So we copied this example: https://github.com/sendgrid/sendgrid-nodejs/blob/93763a74a85969382d59fcb3708d22051498f998/examples/helpers/mail/example.js

And made code like so:

const sendgrid: any = require('sendgrid')(process.env.sendgrid_key);

sendEmail(data: IEmailData): void {
    const email: any = new sendgrid.Email();
    email.addTo(data.emailTo);
    for (let attachment in data.attachments)
      this.addAttachment(email, attachment);
    sendgrid.send(email, (error: any, json: string) => {
      if (error)
        console.log(error);
    });
  }

  addAttachment(email: any, attachmentData: any): void {
    const attachment = new sendgrid.Attachment();
    attachment.setContent(attachmentData.content);
    attachment.setType(attachmentData.type);
    attachment.setFilename(attachmentData.filename);
    attachment.setDisposition(attachmentData.disposition);
    email.addAttachment(attachment);
  }

But when calling sendEmail we get told sendgrid.Attachment is not a function (first line of the second function).

I printed out the sendgrid object and it looks like this:

{ api_key: 'SG.DaV0L',
  api_user: null,
  options: 
   { uriParts: 
      { protocol: 'https',
        host: 'api.sendgrid.com',
        port: '',
        endpoint: '/api/mail.send.json' },
     uri: 'https://api.sendgrid.com/api/mail.send.json' },
  version: '1.9.2',
  Email: [Function: Email],
  smtpapi: [Function: smtpapi],
  send: [Function] }
  • Is the example wrong?
  • Should we get the attachment helper class some other way?

Thank you,
Richard.

bug

Most helpful comment

The example link is not working.
This is the new link:
https://github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/attachments.md

All 10 comments

Figured out after reading the code. That example in github is totally wrong. Should be like this:

addAttachment(email: any, attachmentData: any): void {    
  email.addFile(attachmentData);
}

Thanks @RichardJECooke,

We will investigate further and fix the example. Thanks for taking the time to share the solution!

If you email us at [email protected] with your mailing address and T-shirt size, we would love to send you some swag.

Thanks!

The example link is not working.
This is the new link:
https://github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/attachments.md

Hi, I know this issue has been closed but this is more a problem I have been having when trying to send embedded images to a gmail user.

I found a couple of reasons to why gmail would not render images due to some additional security they impose so I thought I would use the attachments parameter to add a content_id and see if that works instead of using a regular src tag.

When it is sent send grid returns a bad request. Here is my method.

I encode the JPG like so:

    let bitmap = fs.readFileSync(imageDir);
    imageBase64URL = new Buffer(bitmap).toString('base64');

Then I pass in the encoded JPG into the content field inside the attachments argument:

mailer.send({
       to: '[email protected]',
       from: '[email protected]',
       subject: `You have been removed from ${house.name} by ${removerName}`,
       html: result,
       attachments: [
            {
               content: imageBase64URL,
               filename: 'cluttr-logo.jpg',
               contentId: 'cluttr-logo'
            },
       ]
});

Finally I embed my CID like so into my ejs file:

html <img alt="Logo" src="cid:cluttr-logo" width="60" height="60"

Hopefully someone has some insight to what I am doing wrong. Thanks.

Hello @lvh1g15,

I think you need to add a disposition: inline, like so.

With Best Regards,

Elmer

@thinkingserious : Hi, do you know how to send an attachment with the transactional template?

@thinkingserious is there a way I can add a URL attachment to my email, what are the other ways I can add attachment to my email. couldn't exactly find a doc stating the ways attachment work On Sendgrid.

Regards,
Mohit

@thinkingserious : Hi, do you know how to send an attachment with the transactional template?

if you're using the v3 API, just added a attachments key. content needs to be Base64 encoded

https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html

email = {
   "from":{  
      "email":"[email protected]"
   },
   "personalizations":[  
      {  
         "to":[  
            {  
               "email" : "[email protected]"
            }
         ],
         "dynamic_template_data": {
         }
      }
   ],
   "attachments": [
       {
           "content": "",
           "filename" ""
       }
   ]
   "template_id":"someid"
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

egges picture egges  ·  3Comments

thinkingserious picture thinkingserious  ·  4Comments

amlcodes picture amlcodes  ·  4Comments

agostonbonomi picture agostonbonomi  ·  3Comments

murphman300 picture murphman300  ·  4Comments