我正在开发一个Node.js应用程序,该应用程序包含用于用户邀请等事件的Sendgrid交易电子邮件模板。具体来说,我在Ubuntu上使用nodejs v0.10.25 w /官方sendgrid v2 npm。我实际发送消息的功能如下:
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);
});
}
包含替换的options对象如下所示:
{ 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)并没有提供答案。特别有趣的是每个人都使用不同的字符替换标记。不知道这纯粹是出于选择还是依赖于语言。我尝试了不同的字符( - ,:和%),但结果相同。
答案 0 :(得分:1)
我看到你的选项对象有一个属性sub,而不是subs。 option.subs将始终未定义,因此在
中为falseif(options.subs)
尝试将其更改为
if(options.sub)
或者将选项中的属性重命名为sub。