Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js mailgun inline/embed images

How do you embed images with Node.js mailgun. From reading the documentation, they use

-F inline=@files/cartman.jpg

The problem though, its for a curl example.

Here's what I have so far

Mailgun.sendHtmlEmail({
    apiKey: '..',
    domain: '...',
    toEmail: created.email,
    toName: created.email,
    subject: 'mySite update',
    htmlMessage: '<html><img style="display:block;" class="img1" src="cid:test.png" width="600" height="64" /></html>',
    inline: 'email1/test.png',
    fromEmail: '[email protected]',
    fromName: 'Admin'
}).exec({
    // An unexpected error occurred.
    error: function (err){
        console.log(err);
    },
    // OK.
    success: function (){
    }
});

When this was tested, there was no image attached.

I'm assuming you have to access the inline file for the email in a way Node.js can understand.

If so how do you do that?

Can someone point me in the right direction, please.

like image 824
user2124726 Avatar asked May 22 '26 16:05

user2124726


1 Answers

I would suggest you to use mailcomposer module to generate email message with attachments.

mailcomposer = new MailComposer({forceEmbeddedImages: true});
mailcomposer.addAttachment({
  fileName: 'image.png',
  filePath: IMAGES_PATH + '/image.png',
  cid: '0o1q9i2w8u38ur.image.png'
});

mailcomposer.setMessageOption({
  to: created.email,
  from: '[email protected]',
  subject: 'mySite update',
  body: '...',
  html: '<html>...</html>'
});

And you can send it through mailgun:

mailcomposer.buildMessage(function (err, message) {
  if (err) {
    // ...
  }

  mailgun
    .messages()
    .sendMime({
      to: created.email,
      message: message
    }, function (err, response) {
      // ...
    }); 
});
like image 60
Talgat Medetbekov Avatar answered May 24 '26 06:05

Talgat Medetbekov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!