Rit*_*P.V 0 javascript email node.js nodemailer
我正在使用node.js中的nodemailer发送html电子邮件,我正在使用电子邮件模板npm包的模板概念发送[ https://www.npmjs.com/package/email-templates]。
此处,电子邮件已成功发送,但收到时,HTML 中的图像未显示在电子邮件上。
这是发送邮件的主文件:
const nodemailer = require("nodemailer");
var Email = require('email-templates');
const path = require('path');
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'test@gmail.com',
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: 'test@gmail.com',
to: 'test@gmail.com',
},
locals: {
tournament_name:"tournament_name",
date:"date",
time:"time",
fee:"4",
sections:"sections",
description:"description"
},
}).then(() => console.log('email has been sent!'));
Run Code Online (Sandbox Code Playgroud)
这是我希望显示图像的 HTML 部分
<img style="width: 100%;
height: auto;"
src="./images/background.jpg"
alt="game">
Run Code Online (Sandbox Code Playgroud)
你可以这样做:
email.send({
template:path.join(__dirname,'./templates/emailCampaign'),
message: {
from: 'test@gmail.com',
to: 'test@gmail.com',
},
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
}]
Run Code Online (Sandbox Code Playgroud)
然后在你的 html img src 属性中,具有相同的 cid 值,如下所示:
<img src="cid:logo1"/>
Run Code Online (Sandbox Code Playgroud)