node.js - Sendgrid 中的替代代币列表

标签 node.js email templates sendgrid mandrill

我正在向单个用户发送一封交易电子邮件,并希望在电子邮件中嵌入项目列表。我可以使用 Node.js 中的 NPM 包“sendgrid”添加单个替换值 - https://github.com/sendgrid/sendgrid-nodejs :

email.addFilter('templates', 'enable', 1);
email.addFilter('templates', 'template_id', '11122233445....');
email.addSubstitution('{{TOKEN1}}', 'My Token1 Replacement');

但我看不到如何将项目列表添加到 sendgrid 中的模板中。使用 Mandrill,我可以使用 Handlebars 并执行以下操作。名单:

items: [
    {name: "one", url: "/one"},
    {name: "two", url: "/two"},
];

在模板中:

<ul>
    {{#each ITEMS}}
    <li>
        {{name}} - <a href="{{url}}">Blah</a>
    </li>
    {{/each}}
</ul>

然后在代码中:

var rcpt = { "rcpt": email, 
    "vars": [
        { "name":"ITEMS", "content": items }
    ]
};
mergeVars.push(rcpt);
var message = { ...
    "merge_vars": mergeVars,
}
mandrillClient.messages.sendTemplate({... "message": message });

结果:

<ul>
    <li>one <a href="/one">Blah</a></li>
    <li>two <a href="/two">Blah</a></li>
</ul>

有没有办法在 Sendgrid 中做到这一点?

最佳答案

SendGrid 没有 each模板中的逻辑。

最好的解决方法是在模板中添加:

<ul>
  {item1}
  {item2}
  {item3}
</ul>

然后在替换中,您将填充适当的 <li> item 1 details</li>或 NULL(视情况而定)。确保您的模板具有与最大列表大小一样多的替换标记,并且这些项目的默认值为 NULL,这样即使没有项目,它们也会被填充。

关于node.js - Sendgrid 中的替代代币列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33903789/

相关文章:

php - mail() 在新服务器上不起作用

python - 如何检查 Django 模板中的多重性

c++ - mycomparison 是对象、函数还是函数指针?

node.js - 错误:监听 EADDRINUSE:地址已在使用中:::4000

javascript - 读取本地文件的 Electron 功能 - FS - 不读取

node.js - 在终端中无法访问全局安装的 npm 包

c++ - 重载 'min(int&, int&)' 的调用不明确

node.js - express.js 中的 "app.render"和 "res.render"有什么区别?

json - .net Core 和 Serilog 电子邮件接收器 - json 配置

vba - Outlook 无法识别一个或多个名称