gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:57:03 24 4
gpt4 key购买 nike

我正在 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>

dependencies

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

谁能给我一个生成动态电子邮件模板的正确指南

最佳答案

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

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}

关于javascript - Nodejs - 使电子邮件模板动态化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36575318/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com