向Sendgrid模板添加数组数据

4
我希望使用Sendgrid发送包含每个用户独特数据的发票电子邮件。这似乎非常简单,但没有人想到提供如何实现的说明。在电子邮件中,我想用一个类似于以下数组的方式填充四列'N'行:
[{date: 05/05/15, amount: $30, user: abc123, type: A}, 
{date: X, amount: Y, user: Z, type: B} . . . ]

我不明白如何创建这个模板,或者这个模板应该存在于哪里,以便我调用它来填充给定客户的数据。
我看了Sendgrid视频: https://sendgrid.com/docs/User_Guide/Templates/index.html https://www.youtube.com/watch?v=Z0TmOqQarww 还有其他一些教程选项,例如: 如何将动态数据传递到在sendgrid webapp上设计的电子邮件模板? :-| Sendgrid
不幸的是,如何遍历数组并不清楚。我使用Angular,但由于它位于前端,而我的sendgrid位于Express中,我不确定这是否是一个解决方案。
我看了sendwithus作为一个选择,但似乎这可能是一个不必要的复杂化,考虑到我认为这是一个相对简单的用例;我不确定sendwithus是否正在添加价值。
1个回答

5
我正在做同样的事情。 Sendgrid Node模块在github上,展示了如何安装node的sendgrid 首先,你需要创建一个JavaScript对象来保存你的配置。
 var proccessing_payload   = {
    to      :   'webdev@example.com',
    from    :   'processing@example.com',
    subject :   'Transaction ID : 123 ~ Order is ready for processing',
    text    :   'You have a new order to process.\n Please login to process your order. \n DON'T FORGET TO SEND AN EMAIL WITH : tracking information and other useful information.',
    html    :  '<p>You have a new order to process. Please login to process your order. <br/> DON'T FORGET TO SEND AN EMAIL WITH : tracking information and other useful information.</p>'

};

这是发送电子邮件所需的基本设置。

现在,您需要添加一些数据以发送到您在sendGrid仪表板中创建的模板。为此,我只是扩展了processing_payload对象。

第一步:设置过滤器告诉sendGrid要使用哪个模板。

注意* 我只有一个模板版本。我没有使用其他版本。

proccessing_payload.filters = {
   "templates": {
      "settings": {
        "enable": 1,
        "template_id": <YOUR TEMPLATE ID FROM SENDGRID>
      }
    }
};

第二步:您需要将数据映射到您的sendGrid模板项。

注意* 在我的sendGrid模板中,我有一个表格,在其中一个表格单元格内,我有“-product-”。该“-product-”文本字符串将被替换为我在对象中放置的任何内容。以下是示例。

 proccessing_payload.setSubs = {
   "-product-" : ['This will be the replacement for the key. You will see this text in my email']
 };

现在我们发送邮件:
_sendGrid.sendEmail(proccessing_payload);

_sendGrid是我设置的变量,在那里我需要我创建的sendGrid控制器。 例如:

var _sendGrid = require('./sendgrid.server.controller.js');

更多参考资料

exports.sendEmail = function(options){
var _options = options || {};

var payload   = {
    to      :   options.to,
    from    :   options.from,
    subject :   options.subject,
    text    :   options.text,
    html    :   options.html,
    setFrom :   options.setFrom,
    replyto :   options.replyto ? options.replyto : 'no-reply@poshbellies.com'
};

var email = new sendgrid.Email(payload);

if(options.filters){
    email.setFilters(options.filters);
}

if(options.setSubs){
    email.setSubstitutions(options.setSubs);
}

sendgrid.send(email, function(err, json) {
    if (err) { console.error(err); }
    console.log('//////--- SEND GRID : ');
    console.log(json);
});}

我 sendgrid.server.contoller.js 中的 sendEmail 方法如下。

再举个例子,这里是我的 sendGrid 模板中一个使用 -product- 标签的片段。你也可以使用 #product# 或其他标记。

    <table width="100%">
            <tbody>
                <tr>
                    <th>qty</th>
                    <th>category</th>
                    <th>Product</th>
                </tr>
                <tr>
                    <td align="center">-qty-</td>
                    <td align="center">-category-</td>
                    <td align="center">-product-</td>
                </tr>
            </tbody>
        </table>

希望这能帮到你!

嘿谢谢!出于某种原因,只有在我将空格设置为setHtml函数时,我的电子邮件才能工作。你知道这是为什么吗?sgEmail.setHtml(' '); 没有这行代码,我会得到一个错误。 - Matt Kim

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接