将数组数据添加到Sendgrid模板

ras*_*adb 4 javascript arrays sendgrid

我想发送一张发票电子邮件,其中包含每个用户使用Sendgrid的唯一数据.这看起来很简单,以至于没有人想过要包含如何做到这一点的指示.在电子邮件中,我想用'N'行填充四列,其中包含如下数组:

[{date: 05/05/15, amount: $30, user: abc123, type: A}, 
{date: X, amount: Y, user: Z, type: B} . . . ]
Run Code Online (Sandbox Code Playgroud)

我不明白我是如何创建这个模板的,或者我应该在哪里存在这个模板来调用它来填充给定客户的数据.

我查看了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是否/如何增加价值.

Wes*_*uff 5

我现在正在做同样的事情.

github上的Sendgrid节点模块,显示了如何为节点安装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>'
Run Code Online (Sandbox Code Playgroud)

};

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

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

第一步:设置过滤器以告诉sendGrid您要使用的模板.

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

proccessing_payload.filters = {
   "templates": {
      "settings": {
        "enable": 1,
        "template_id": <YOUR TEMPLATE ID FROM SENDGRID>
      }
    }
};
Run Code Online (Sandbox Code Playgroud)

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

注意*在我的sendGrid模板中,我有一个表,在其中一个表格单元格中,我有"-product-".那个"-product-"文本字符串将替换为我放入该对象的文本字符串.以下示例.

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

现在我们发送电子邮件:

_sendGrid.sendEmail(proccessing_payload);
Run Code Online (Sandbox Code Playgroud)

_sendGrid是我设置的变量,我需要我创建的sendGrid控制器.示例:

var _sendGrid = require('./sendgrid.server.controller.js');
Run Code Online (Sandbox Code Playgroud)

更多参考

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);
});}
Run Code Online (Sandbox Code Playgroud)

sendgrid.server.contoller.js中的sendEmail方法如下所示.

再举一个例子.这是我的sendGrid模板的片段,我使用的是-product-标签.你不必使用-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>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!