我是nodejs的新手。我正在使用nodemailer进行电子邮件验证。我必须在nodemailer html属性选项中包含模板。我尝试了很多事情,但没有为我工作。我正在使用哈巴狗模板。我也想知道如何在模板中传递变量。希望您理解我的问题。
NodeMailer:
const nodemailer = require('nodemailer');
var emailTemplate = require('../views/mail-templates/email-verification.pug');
const mailOptions = {
from: 'Do not reply [email protected]',
to: email,
subject: 'Verify your email',
text: 'Please verify your email by clicking on this link.',
html: emailTemplate
};
email-verification.pug
doctype html
html
head
title "Email Verification"
body
h1 Verify your email by clicking on this link.
a(href='') Click here for verification
在NodeMailer中:
var ejs = require('ejs');
ejs.renderFile('./emailTemplate.html', {
test: "sample test"
}, {}, function(err, formattedHtml) {
if (err) {
console.log(err);
return;
}
// You will get final html in dynamic values
console.log("Final html with dynamic values" , formattedHtml);
});
emailTemplate.html
<html>
<%= test %>
</html>
有关更多ejs语法,请参考:https://ejs.co/