【问题标题】:NodeMailer send HTML email with template from databaseNodeMailer 从数据库发送带有模板的 HTML 电子邮件
【发布时间】:2020-06-16 23:31:47
【问题描述】:

在我的应用程序中,我设置了 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,或者是否在错误的位置评估请求数据..

我该如何解决?

【问题讨论】:

  • 你试过html: req.body.arenile.email_template而不是html: ` ${req.body.arenile.email_template}`吗?
  • 是的,结果相同
  • 控制台有输出吗? (或尝试console.dir(req.body.arenile.email_template)

标签: javascript nodemailer


【解决方案1】:

我是这样解决的:

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

然后使用{{email}} 将打印正确的值。

【讨论】:

    猜你喜欢
    • 2018-07-11
    • 2021-08-06
    • 2015-11-11
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 2015-01-13
    • 1970-01-01
    相关资源
    最近更新 更多