NodeMailer从数据库发送带有模板的HTML电子邮件

时间:2020-06-16 23:31:47

标签: javascript nodemailer

在我的应用中,我将nodemailer设置为发送电子邮件。该应用程序是使用Vue和Firebase构建的。 我需要从UI修改电子邮件模板,因此它们以HTML格式保存在数据库中。 在这些模板中,我需要能够使用动态值。

这是创建电子邮件的方法:

 const mailOptions = {
     from: "Test",
     to: req.body.email,
     subject: "New email from" + " " + req.body.email,
     html: req.body.arenile.email_template
  };

FireStore中req.body.arenile.email_template的内容是:

<p>Date: ${req.body.reservation_date} </p><p>Email: ${req.body.email}</p>

问题在于变量没有按原样进行评估和打印在电子邮件上。

我尝试过:

html: ` ${req.body.arenile.email_template}`

但不能解决问题。

如果我使用:

html: `<p>Date: ${req.body.reservation_date} </p><p>Email: ${req.body.email}</p>`

工作正常。 ${req.body.email}打印为email@example.com

但是:

html: ` ${req.body.arenile.email_template}`

没有。 ${req.body.email}打印为${req.body.email}

我不确定是否需要以不同的方式转义HTML,或者是否在错误的位置评估了请求数据。

我该如何解决?

1 个答案:

答案 0 :(得分:0)

我通过这种方式解决了

var html = req.body.arenile.email_template;
html = html.replace('{{email}}', req.body.email);
html: html

然后使用{{email}}将打印当前值。