将模板作为邮件发送

Send template as a mail

我对这个问题很纠结。我想用 html 将邮件发送给用户 template.But 代码根本不起作用,我将 html 页面 url 存储在一个变量中并附加发送到邮件,但它没有显示 html 的内容。任何人都可以帮助我吗?`

                        var html = '/index1.html';
                            transporter.sendMail({
                            from : xxxx@gmail.com,
                            to : xxxx@gmail.com,
                            subject : 'Invitation',
                            html : html
                        });
                    `

您的 html 变量只是一个字符串,其值为 /index1.html 而不是实际页面。 :)

如果您正在使用 jQuery,您可以通过像

一样获取 index1.html 的内容来实现
$.ajax({
    type: 'GET',
    url: '/index1.html',
    success: function (htmlContent) { // htmlContent will have your html markup
        transporter.sendMail({
            from : xxxx@gmail.com,
            to : xxxx@gmail.com,
            subject : 'Invitation',
            html : htmlContent // set your html to the markup read from ./index1.html
        });
    }
});

编辑:

在 OP 的最新评论后更新

如果你在后端做这个,你可以使用节点的fs模块来读取文件

var fs = require("fs");
var filename = "./index1.html";

var data = fs.readFileSync(filename); //data will have contents of your index1.html

transporter.sendMail({
    from : xxxx@gmail.com,
    to : xxxx@gmail.com,
    subject : 'Invitation',
    html : data
});