【问题标题】:Sendgrid SMTP API not replacing substitution tags when sending messageSendgrid SMTP API 在发送消息时不替换替换标签
【发布时间】: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>&nbsp;</div>

<div><span style="font-family:tahoma,geneva,sans-serif;">&lt;%body%&gt;</span></div>

<div>&nbsp;</div>

<div>&nbsp;</div>

<div>&nbsp;</div>

<div><span style="font-family:tahoma,geneva,sans-serif;"><span style="font-size:9px;">Invitation ID:</span>&nbsp;-id-</span></div>
</body>
</html>

邮件将使用正确的模板、主题和发件人信息发送给适当的收件人。我还可以从我的 SendGrid 统计数据中验证它们是否被标记了适当的类别和 SMTP 参数(“发件人”)。 但没有进行替换。例如,-name- 标签仍然存在于我的测试邮箱中的结果邮件中。

我曾尝试在 SendGrid 咨询文档,包括他们的 SMTP API,并查询他们的支持数据库。我也搜索过 StackExchange,但最相关的问题(thisthisthis)并没有完全提供答案。特别有趣的是,每个人都使用不同的字符作为替换标记。不知道这是纯粹的选择还是取决于语言。我尝试了不同的字符(-、: 和 %),但结果相同。

【问题讨论】:

    标签: javascript node.js sendgrid


    【解决方案1】:

    我看到你的选项对象有一个属性 sub,而不是 subs。 option.subs 将始终未定义,因此在

    中为 false

    如果(options.subs)

    要么尝试将其更改为

    如果(options.sub)

    或将选项中的属性从 sub 重命名为 subs。

    【讨论】:

    • 是的,我在发布这个问题后一个小时就发现了这一点。我觉得我的第一个 StackExchange 问题是一个愚蠢的错字,我觉得自己像个白痴。还是谢谢。
    猜你喜欢
    • 2016-07-26
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    相关资源
    最近更新 更多