首页 > 解决方案 > 如何将表单提交中的值传递给 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} ,但价值没有通过。

标签: node.jsexpresspug

解决方案


我最终像这样想通了

html: pug.renderFile(__dirname + '/views/tracking.pug', { email: email, revenue: revenue })

推荐阅读