Javascript 通过nodeEmailer-node.js以电子邮件形式发送时,html中的图像不显示?
我使用node.js中的nodeEmailer发送html电子邮件,我使用电子邮件模板npm包[]中的模板概念发送 此处,电子邮件发送成功,但收到时HTML中的图像未显示在电子邮件上 这是发送邮件的主文件:Javascript 通过nodeEmailer-node.js以电子邮件形式发送时,html中的图像不显示?,javascript,node.js,email,nodemailer,Javascript,Node.js,Email,Nodemailer,我使用node.js中的nodeEmailer发送html电子邮件,我使用电子邮件模板npm包[]中的模板概念发送 此处,电子邮件发送成功,但收到时HTML中的图像未显示在电子邮件上 这是发送邮件的主文件: const nodemailer = require("nodemailer"); var Email = require('email-templates'); const path = require('path'); var transporter = nodemailer.crea
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!'));
这是我希望图像显示的HTML部分
<img style="width: 100%;
height: auto;"
src="./images/background.jpg"
alt="game">
您可以执行以下操作:
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
}]
然后在html img src属性中,具有相同的cid值,如下所示:
<img src="cid:logo1"/>
“/images/background.jpg”
是一个相对路径url。假设您希望用户查看的图像不在任何电子邮件客户端(网站)上,这并不是一个很大的想象他们正在使用,并且将是无效路径。那么,…我该怎么办?要么将图像作为base64编码图像嵌入电子邮件,要么将其URL完全插入每个人都可以访问的服务器路径。base64编码图像我如何使用它在此处应用您必须指定资源驻留的位置,以便电子邮件模板代码>包括它们。请检查文档中的juiceResources
和webResources
。