Sendgrid-nodejs: Adding encoded images to attachment argument

Created on 6 Apr 2018  路  5Comments  路  Source: sendgrid/sendgrid-nodejs

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 with a link.

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:

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

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

waiting for feedback question

All 5 comments

Hello @lvh1g15,

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

With Best Regards,

Elmer

//imageData= "data:image/png;base64,ine793nfdsf......."

imageb64 = imageData.replace('data:image/png;base64,' , ''); 
//remove data:image/png;base64,            

const msg = {
            to: '[email protected]',
            from: '[email protected]',
            subject: "image attached",
            html :'<img src="cid:myimagecid"/>',
            attachments: [
              {
                filename: "imageattachment.png",
                content: imageb64,
                content_id: "myimagecid",
              }
            ]
          };

      sgMail.send(msg);

Hello @lvh1g15,

Were you able to resolve the issue?

I have the same problem in gmail... any solutions?

@huggler Please open a new issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thinkingserious picture thinkingserious  路  4Comments

agostonbonomi picture agostonbonomi  路  3Comments

umarhussain15 picture umarhussain15  路  3Comments

TobiahRex picture TobiahRex  路  3Comments

Loriot-n picture Loriot-n  路  4Comments