HTML模板和绑定数据以在Nodejs中作为电子邮件发送

时间:2015-07-13 08:40:45

标签: node.js

我在服务器上使用Nodejs并使用nodemailer npm模块将HTML电子邮件内容发送给用户。我需要使用模板发送电子邮件。我不想在服务器端构建HTML字符串作为字符串处理。我想使用一些模板引擎,以便我可以在单独的文件中分离我的HTML标记,然后绑定我的数据以创建HTML字符串,我可以将其作为电子邮件发送。

以下是我的电子邮件模板

<div>
    <img src="https://localhost:8443/img/application/forgotpassword/forgot-password-bg.jpg" alt="Forgot your myFitCode App password?"/>
    <div>
        Dear Mr. $name, 
    </div>
    <div>
        Greetings from myFitCode App. 
    </div>
    <div>
        We always endeavor to make health and fitness simpler and easier for our customers. 
    </div>
    <div>
        Your temporary password is created as below. 
    </div>
    <div>
        $temppassword
    </div>
    <div>
        Please note; this is temporary password to activate your account. You can not use this password to login into App. You must have to click following link and create new password to continue to access App.
    </div>
    <div>
        <a href="https://localhost:8443/img/application/forgotpassword/forgot-password-bg.jpg">Create New Password</a>
    </div>
    <div>
        Sincerely,
    </div>
    <div>The myFitCode Team</div>       
</div>

以下是我使用nodemailer发送电子邮件的方法 //处理服务忘记凭据请求

exports.sendEmail = function (to, subject, content) {

    Email.transporter.sendMail({
        from: 'sdfasdfsfd@gmail.com',
        to: to,
        subject: subject,
        html: content
    });
};

如何通过阅读模板和绑定数据来构建HTML内容? 请指教。

1 个答案:

答案 0 :(得分:2)

可以使用类似Jade或下划线的东西来做这件事,如果你需要经常使用这个功能,这可能是正确的方法。如果只有少数变量,为什么不使用

var personalisedHtml = templateHtml.replace("$name", customerName).replace("$temppassword", temppassword)