【发布时间】:2016-03-12 00:36:33
【问题描述】:
我正在开发一个 Node.js 应用程序,该应用程序包含用于用户邀请之类的 Sendgrid 事务电子邮件模板。具体来说,我在带有官方 sendgrid v2 npm 的 Ubuntu 上使用 nodejs v0.10.25。我实际发送消息的功能如下:
function sendEmail(payload, options, cb){
//for debugging purposes
console.log(options);
var email = new sendgrid.Email({
subject: payload.subject,
html: payload.html,
from: global.config.sendgrid.mailFrom,
fromname: global.config.sendgrid.mailName,
replyto: options.replyto ? options.replyto : 'no-reply@' + global.config.sendgrid.mailhost
});
email.setSmtpapiTos(options.addressees);
if(options.filters)
email.setFilters(options.filters);
if(options.subs)
email.setSubstitutions(options.subs);
if(options.category)
email.setCategories(options.category);
if(options.unique_args)
email.setUniqueArgs(options.unique_args);
sendgrid.send(email, function(err, json){
return cb(err, json);
});
}
包含替换的选项对象如下所示:
{ category: 'Support Invite',
addressees: [ %EMAIL1%, %EMAIL2% ],
sub:
{ '-name-': [ 'Shawn 1', 'Shawn 2' ],
'-id-': [ 1, 2 ],
'-pic-':
[ '<img height="100" src="%PICTURE URL%?sz=50" style="width: 100px; height: 100px;" width="100" />',
'<img height="100" src="%PICTURE URL%?sz=50" style="width: 100px; height: 100px;" width="100" />' ] },
filters:
{
templates:
{
settings:
{ 'enable': 1,
'template_id': global.config.sendgrid.transactions.supportInvite} } },
unique_args:
{
sender: '104294048950213400380' } }
最后,这是该测试中模板的 HTML:
<html>
<head>
<title></title>
</head>
<body>
<div style="text-align: center;"><span>-pic-</span></div>
<div><span style="font-family:tahoma,geneva,sans-serif;">Hello -name-,</span></div>
<div> </div>
<div><span style="font-family:tahoma,geneva,sans-serif;"><%body%></span></div>
<div> </div>
<div> </div>
<div> </div>
<div><span style="font-family:tahoma,geneva,sans-serif;"><span style="font-size:9px;">Invitation ID:</span> -id-</span></div>
</body>
</html>
邮件将使用正确的模板、主题和发件人信息发送给适当的收件人。我还可以从我的 SendGrid 统计数据中验证它们是否被标记了适当的类别和 SMTP 参数(“发件人”)。 但没有进行替换。例如,-name- 标签仍然存在于我的测试邮箱中的结果邮件中。
我曾尝试在 SendGrid 咨询文档,包括他们的 SMTP API,并查询他们的支持数据库。我也搜索过 StackExchange,但最相关的问题(this、this 和 this)并没有完全提供答案。特别有趣的是,每个人都使用不同的字符作为替换标记。不知道这是纯粹的选择还是取决于语言。我尝试了不同的字符(-、: 和 %),但结果相同。
【问题讨论】:
标签: javascript node.js sendgrid