Nodejs - 使电子邮件模板动态化

时间:2016-04-12 13:50:40

标签: javascript node.js html5 express mean-stack

我正在MEAN STACK中开发项目,我需要在我的应用程序中发送许多电子邮件。 我已将电子邮件模板作为单独的所有电子邮件模板制作,但我想在电子邮件模板中制作通用页眉和页脚。

  

Route.js

router
    .route('/api/user/register')
    .post(
        //for get email template
        var fs = require("fs");
        var message = fs.readFileSync(basePath + "app/client/views/layout/registerEmailTemplate.html").toString();

        .......
);

router
    .route('/api/user/forgotpassword')
    .post(
         //for get email template
        var fs = require("fs");
        var message = fs.readFileSync(basePath + "app/client/views/layout/resetPasswordEmailTemplate.html").toString();

        .......
);


router
    .route('/api/user/accountActivation')
    .post(
        //for get email template
        var fs = require("fs");
        var message = fs.readFileSync(basePath + "app/client/views/layout/accountActivationEmailTemplate.html").toString();

        .......
);
  

registerEmailTemplate.html

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <title>My site title</title>
    <style>
        here all common css
    </style>
</head>
<body style="margin:0px;padding:0px">
        here registratin html code
</body>
</html>
  

resetPasswordEmailTemplate.html

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <title>My site title</title>
    <style>
        here all common css
    </style>
</head>
<body style="margin:0px;padding:0px">
        here resetpassword html code
</body>
</html>
  

accountActivationEmailTemplate.html

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <title>My site title</title>
    <style>
        here all common css
    </style>
</head>
<body style="margin:0px;padding:0px">
        here account activation html code
</body>
</html>
  

依赖关系

"express"  => "version": "4.13.4",
"mongoose" => "version": "4.4.4",
"mongodb"  => "version": "2.4.9",
"OS"  => "ubuntu 14.04 lts 32bit",

任何机构都可以为我提供生成动态电子邮件模板的正确指南

1 个答案:

答案 0 :(得分:0)

当我不得不动态地将名字插入电子邮件时,我在html上使用了字符串格式。这仍然适用于模板。首先,您必须将format函数添加到字符串原型中。

if (!String.prototype.format) {
  String.prototype.format = function() {
    var args = arguments;
    return this.replace(/{(\d+)}/g, function(match, number) { 
      return typeof args[number] != 'undefined'
        ? args[number]
        : match
      ;
    });
  };
}

然后您可以将模板注入电子邮件。

router
    .route('/api/user/register')
    .post(
        //for get email template
        var fs = require("fs");

        var header = fs.readFileSync('pathToHeader');
        var footer = fs.readFileSync('pathToFooter');
        var message = fs.readFileSync(basePath + "app/client/views/layout/registerEmailTemplate.html").toString();

        var formattedMessage = string.format(message,header,footer);
);

您的电子邮件文件看起来像

{0}
        here resetpassword html code
{1}