Saturday, May 25, 2024
 Popular · Latest · Hot · Upcoming
13
rated 0 times [  14] [ 1]  / answers: 1 / hits: 7223  / 4 Years ago, wed, june 3, 2020, 12:00:00

I am sending an html email using nodemailer in node.js, I am sending using the templates concept of the email-templates npm package [https://www.npmjs.com/package/email-templates].



Here, the email is sending successfully but the image in the HTML is not displaying on the email when received.



this is the main file which sends the mail :



const nodemailer = require(nodemailer);
var Email = require('email-templates');
const path = require('path');

var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: '[email protected]',
pass: 'password'
}
});

const email = new Email({
transport: transporter,
send: true,
preview: false,
views: {
options: {
extension: 'ejs'
}
},
juice: true,
juiceResources: {
preserveImportant: true,
webResources: {
relativeTo: path.join(__dirname,'./templates/emailCampaign/images')
}
}
});

email.send({
template:path.join(__dirname,'./templates/emailCampaign'),
message: {
from: '[email protected]',
to: '[email protected]',
},
locals: {
tournament_name:tournament_name,
date:date,
time:time,
fee:4,
sections:sections,
description:description
},


}).then(() => console.log('email has been sent!'));



This is the HTML part where I want my image to show



<img style=width: 100%;
height: auto;
src=./images/background.jpg
alt=game>

More From » node.js

 Answers
17

You can do something like:


  email.send({
template:path.join(__dirname,'./templates/emailCampaign'),
message: {
from: '[email protected]',
to: '[email protected]',
},
locals: {
tournament_name:"tournament_name",
date:"date",
time:"time",
fee:"4",
sections:"sections",
description:"description"
},
attachments: [{
filename: 'logo.jpg',
path: `${__dirname}/../public/images/logo.jpg`,
cid: 'logo1' //same cid value as in the html img src
}]


And then in your html img src attribute, have the same cid value like so:


<img src="cid:logo1"/>

Check the documentation here


[#3588] Monday, June 1, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hailie

Total Points: 25
Total Questions: 112
Total Answers: 111

Location: Belize
Member since Tue, Dec 8, 2020
4 Years ago
hailie questions
Tue, Apr 6, 21, 00:00, 3 Years ago
Tue, May 19, 20, 00:00, 4 Years ago
Mon, May 4, 20, 00:00, 4 Years ago
;