Search code examples
node.jsamazon-s3lambdanodemailer

To send mail Embedded with image in Lambda function using nodeJS


I have created lambda function in AWS which send mail to various recipients. I need to add company logo/banner at bottom of the mail. Below is the code

var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
host: 'xxxxxx.xxxx.com',
port:587,
secure : true
});
var text = 'xxxxxxxxxx <img src="/Images/banner.jpg"  width="800px" width="78%" height="100px" 
height="11%" border="0" alt="Email banner">';
var mailOptions = {
    from: 'donotreplyhere@xxxx.com',
    to: 'skumar@xxx.com',
    subject: 'Test subject',
    html: text
};
exports.handler = async (event, context, callback) => {
 return new Promise((resolve,reject)=>{
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
    console.log("Error " +error);
   }   else {
        console.log('Email sent: ' + info.response);
     }
    });

 });
}

I am receiving the mail but image is not being displayed getting error : The linked image can't be displayed.

I also tried with s3 url by uploading images in s3 bucket but using its url like

<img src="https://falcon-bucket-us-east-1-xxxxxx.s3.amazonaws.com/dev/Images/banner.jpg"  
 width="800px" width="78%" height="100px" "height="11%" border="0" alt="Email banner">';

But this didn't worked, may be because its not public and i can't make it public.

Any other way that i can use to accomplish this task.

Thank for looking


Solution

  • Here are some of the options:

    • If you can use CloudFront in-front of S3 and use the CloudFront URL for the static content, do so. Here is more information.
    • You can also host the static content on any publicly accessible website e.g. on Github pages which is also a CDN for free.
    • You can use Data URLs in place of the images. You must convert the images to the Base64 encoded version and use them directly in the HTML.
    • You can use SVG as an image. You must convert your existing images to SVG and directly embed the contents in the HTML.