node.js - 如何将表单提交中的值传递给 pug 电子邮件模板
问题描述
我有一个发布到 /submit-form 路由的表单,我需要传递 req.body.revenue 值以在表单提交后发出的电子邮件模板中使用。
const email = req.body.email;
const revenue = req.body.revenue;
// Prepare the nodemailer
let transporter = nodemailer.createTransport({
host: 'smtp.mailgun.org',
port: 587,
secure: false,
tls: { cipers: 'SSLv3' },
auth: {
user: 'postmaster@sandbox.mailgun.org',
pass: 'xyz',
}
});
// Prepare the email data
const data = {
from: 'Excited User <me@samples.mailgun.org>',
to: email,
subject: 'Hello Pixel, Where those cookies at?',
text: 'Gonna load the pixel next in the HTML',
html: { path: 'pixel-tracker-email.html' }
};
// Send the mail via Nodemailer
transporter.sendMail(data)
.then(msg => console.log(msg))
.catch(err => console.log(err));
console.log('Email sent via Nodemailer!');
我已经为我的电子邮件模板设置了 pug,并尝试将 html 值更改为如下所示:
// Prepare the email data
const data = {
from: 'Excited User <me@samples.mailgun.org>',
to: email,
subject: 'Hello Pixel, Where those cookies at?',
text: 'Gonna load the pixel next in the HTML',
html: var html = pug.renderFile('path/to/email-template.pug', {revenue});
};
然后我在实际的 email-template.pug 文件中使用 #{revenue} ,但价值没有通过。
解决方案
我最终像这样想通了
html: pug.renderFile(__dirname + '/views/tracking.pug', { email: email, revenue: revenue })
推荐阅读
- php - 将 PHP 创建的 CSV 直接保存为 ISO 编码
- angular - Angular 服务工作者 maxSize 配置
- url - 所有按钮的 TYPO3 预览 URL 配置
- javascript - 我想在 Node.js 中调用 Python 并使用 SymPy 模块
- sybase - 增加打开对象的值数量的原因是什么?
- outlook-addin - Office JS Add-in API Compose 预约获取联系方式
- javascript - 用于 3 个表单的 Javascript 动画切换
- python-3.x - 如何从 Outlook 加载项运行 python
- symfony - $encoder->encodePassword($user, $password) 上的 Symfony 4 错误;
- docker - Dockerfile VOLUME 源不存在