问题描述
我要以独特的与Sendgrid每个用户数据发送发票电子邮件。这似乎是这么简单的东西,没有人认为包括如何做到这一点的方向。在电子邮件我想填充四列以'N'行像一个数组:
I want to send an invoice email with data unique to each user with Sendgrid. This seems like something so simple that no one has thought to include directions on how to do this. Within the email I want to populate four columns with 'N' rows with an array like:
[{date: 05/05/15, amount: $30, user: abc123, type: A},
{date: X, amount: Y, user: Z, type: B} . . . ]
我不明白我怎么创造这个模板或在此模板是应该存在我称之为填充一个给定的客户的数据。
I don't understand how I create this template or where this template is supposed to exist for me to call it to populate for a given customer's data.
我看了看Sendgrid视频:
I looked at the Sendgrid videos:https://sendgrid.com/docs/User_Guide/Templates/index.htmlhttps://www.youtube.com/watch?v=Z0TmOqQarww
像其他几个教程选项一起:
How通过动态的数据,电子邮件模板desgined上sendgrid web应用? : - | Sendgrid 。
along with several other tutorial options like:How to pass dynamic data to email template desgined on sendgrid webapp ? :-| Sendgrid.
不幸的是,小于清楚如何遍历数组。我用的角度却自认为生活在前端和我sendgrid住在防爆preSS我不知道,要么是一个解决方案。
Unfortunately it was less than clear how to iterate through an array. I use Angular but since that lives on the front end and my sendgrid lives in Express I'm not sure that's a solution either.
我看了看sendwithus作为一个选项,但它似乎像它可能给什么,我相信是一个比较简单的用例的不必要的复杂;我不知道是否/如何sendwithus是增加值。
I took a look at sendwithus as an option but it seems like it's probably an unnecessary complication given what I believe is a relatively simple use case; I wasn't sure if/how sendwithus is adding value.
推荐答案
我的工作同样的事情现在。
I am working on the same thing right now.
首先要创建一个JavaScript对象来保存你的配置。
First you want to create a javascript object to hold your configuration.
var proccessing_payload = {
to : '[email protected]',
from : '[email protected]',
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>'
};
这是基本的设置,您需要发送电子邮件。
That is the base setup you will need to send an email.
现在你将要添加一些数据发送到您已在sendGrid仪表板内创建模板。要做到这一点,我只是延长了processing_payload对象。
Now you will want to add some data to send over to your template you have created inside your sendGrid Dashboard. To do this I just extended the processing_payload object.
第一步:设置过滤器来告诉sendGrid你想用什么模板
First : Set your filters to tell sendGrid what template you want to use.
注*我只有一个模板版本。我没有使用其他版本。
Note* I only have one template version. I am not using other versions.
proccessing_payload.filters = {
"templates": {
"settings": {
"enable": 1,
"template_id": <YOUR TEMPLATE ID FROM SENDGRID>
}
}
};
二:您需要将数据映射到sendGrid模板项目
Second : You need to map your data to your sendGrid Template items.
注*在我sendGrid模板我有一个表,里面的表格单元格我有 - 产品 - 。这 - 产品 - 文本字符串将与替换以往我把该对象。下面的例子。
Note* In my sendGrid template I have a table and inside one of the table cells I have "-product-". That "-product-" text string will be replaced with that ever I put in the object. Example below.
proccessing_payload.setSubs = {
"-product-" : ['This will be the replacement for the key. You will see this text in my email']
};
现在我们发送电子邮件:
Now we send the email:
_sendGrid.sendEmail(proccessing_payload);
_sendGrid是变量我设置,我所需要的sendGrid控制器我创建。
例如:
_sendGrid is the variable I set where I required the sendGrid controller I created.Example :
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 : '[email protected]'
};
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方法看起来是这样的。
The sendEmail method inside my sendgrid.server.contoller.js looks like this.
有关远一点的例子。下面是从我的sendGrid模板片断,我现在用的是 - 产品 - 标签。而你没有使用 - 产品 - 。您可以使用#产品#或什么的。
For a little further example. Here is a snippet from my sendGrid template where I am using the -product- tag. And you dont have to use -product-. You can use #product# or whatever.
<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>
希望这有助于!
这篇关于添加阵列数据来Sendgrid模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!