在电子邮件模板更换占位符 [英] Replacing placeholders in Email Template

查看:237
本文介绍了在电子邮件模板更换占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是更好的方式来取代电子邮件模块占位符?

所以,我做了一个邮件服务,需要的参数,如 TEMPLATENAME receiverMail &安培; 当地人。当地人有占位符的值。

  VAR当地人= {
    用户名:约翰,
    网站:w3schools.com
    网址:HTTP:// ABC COM
};mail.sendMail('登记','xyz.com,当地人);

从邮件服务相关code: -

  VAR mailOptions = {
        来源:ABC< foo@blurdybloop.com>//发件人地址
        于:电子邮件,//接收器的列表
        主题:template_subject //主题行
        HTML:// template_html HTML主体

template_html 内容的HTML体包括占位符这是从MongoDB的拉动。

当地人是动态的和放大器;可以增加或减少。

什么是替换占位符的方式?

模板: - (请注意,这些占位符是动态的和放;可以通过管理员进行更改)

 < P>
    嗨{{名}},
&所述; / P>
&所述p为H.;
   XYZ希望你能加入它的网络针对< A HREF ={{URL}}>&XYZ LT; / a取代。
    < BR />
    我们的网站{{网站}}
&所述; / P>


解决方案

我们可以使用普通的前pressions搜索和与用户指定的数据替换模板。

我已经把一个小演示在

工作演示

在这里,我用实际数据替换模板。

我使用的是低于code为模板替换数据:

 为(VAR I = 0; I< data.length;我++){
        VAR OBJ =数据[I]
        输出+ =&所述; P>中+ template.replace(/ \\<%=(*)\\%方式>?/克,功能(匹配属性){
            返回OBJ [属性]
        })+&下; / P>中; }

What is the better way to replace placeholders in email module?

So, I have made a mail service which takes parameters like templatename , receiverMail & locals. locals have the values for placeholders.

var locals = {
    username: "John",
    website: "w3schools.com",
    url: "http:// abc. com"
};

mail.sendMail('Registration', 'xyz.com', locals);

Relevant Code from Mail service :-

var mailOptions = {
        from: "Abc <foo@blurdybloop.com>", // sender address
        to: email, // list of receivers
        subject: template_subject, // Subject line
        html: template_html // html body

template_html contents the html body including the placeholders this is pulled from mongodb.

The locals are dynamic & can be increased or decreased.

What is the way to replace the placeholders?

Template:- (pls note these placeholders are dynamic & can be changed by admin)

<p>
    Hi {{username}},
</p>
<p>
   Xyz would like you to join it's network on <a href="{{url}}">xyz</a>.
    <br />
    Our website {{ website }}
</p>

解决方案

We can use regular expressions to search and replace the template with the user specified data.

I have put up a small demo at

Working DEMO

Here I am replacing the template with the actual data.

I am using the below code for replacing the data in the template:

for (var i = 0; i < data.length; i++) {
        var obj = data[i];
        output +="<p>" + template.replace(/\<%=(.*?)\%>/g, function(match, property) {
            return obj[property];
        }) + "</p>";

 }

这篇关于在电子邮件模板更换占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆