我试图将ejs模板发送到nodemailer html选项
var transporter = nodemailer.createTransport(smtpTransport({
service: 'gmail',
host : 'smtp.gmail.com',
secureConnection : true,
auth : {
user: 'user@gmail.com',
pass: 'abc123'
}
}));
mailOptions = {
to : "Your email",
subject : "confirm your email"
html : "HOW DO I SENT AN EJS TEMPLATE HERE?"
}
如何将EJS模板传递给html属性?
答案 0 :(得分:1)
使用ejs模块将模板呈现为HTML,然后传递给nodemailer
var ejs = require('ejs');
var transporter = nodemailer.createTransport(smtpTransport({
service: 'gmail',
host : 'smtp.gmail.com',
secureConnection : true,
auth : {
user: 'user@gmail.com',
pass: 'abc123'
}
}));
mailOptions = {
to : "Your email",
subject : "confirm your email"
html : ejs.renderFile(__dirname + '/YOUR_MAIL_TEMPLATE.ejs')
}
答案 1 :(得分:0)
基于以上答案,由于渲染文件是异步的,因此您只需在回调内包含mailOptions即可,如下所示:
ejs.renderFile(__dirname + "/hello.ejs", { name: name }, function (err, data) {
if (err) {
console.log(err);
} else {
var mainOptions = {
from: '"YOUR_NAME" YOUR_EMAIL_ADDRESS',
to: email,
subject: 'Account Activated',
html: data
};
//console.log("html data ======================>", mainOptions.html);
transporter.sendMail(mainOptions, function (err, info) {
if (err) {
res.json({
msg: 'fail'
})
} else {
res.json({
msg: 'success'
})
}
});
}
});
注意:另外,请确保将其包装的函数也是异步的