2015-07-13 68 views
0

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

以下是我的电子邮件模板

<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发送电子邮件 //处理SER忘记证书请求

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

    Email.transporter.sendMail({ 
     from: '[email protected]', 
     to: to, 
     subject: subject, 
     html: content 
    }); 
}; 

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

回答

2

可能使用类似Jade或下划线来做到这一点,如果你需要使用这个功能,这可能是正确的选择。如果只有几个变量,为什么不使用

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

谢谢,我用“fs”模块读取模板文件为字符串,然后对键进行简单的替换操作。 – joy

相关问题