[英]How to substitute Sendgrid template variables? (in node-js)
It seems to be a simple parameter I'm missing but I'm having trouble figuring out exactly what it is.这似乎是我遗漏的一个简单参数,但我无法弄清楚它到底是什么。
This is the request I'm sending with '@sendgrid/mail':这是我使用“@sendgrid/mail”发送的请求:
email.js:电子邮件.js:
const sgMail = require('@sendgrid/mail');
function emailRequest() {
msg = {
to: 'test+10@gmail.com
from: 'info@owner.io',
subject: 'Receipt for Business Expenses',
template_id: 'db6d11ae-41e4-4e1a-a71b-f5368eb21c9c',
personalizations: [
{
to: 'test+10@gmail.com,
from: 'info@ownr.io,
subject: 'Receipt for Business Expenses,
template_id: 'db6d11ae-41e4-4e1a-a71b-f5368eb21c9c',
substitutions: {
':firstname': 'Bobba',
':ordernumber': 'WHAAA',
':orderdate': 'today',
':ordertime': 'NOW!',
},
custom_args: {
':firstname': 'Bobba',
':ordernumber': 'WHAAA',
':orderdate': 'today',
':ordertime': 'NOW!',
},
},
],
sub: {
':firstname': 'Bobba',
':ordernumber': 'WHAAA',
':orderdate': 'today',
':ordertime': 'NOW!',
},
substitutions: {
':firstname': 'Bobba',
':ordernumber': 'WHAAA',
':orderdate': 'today',
':ordertime': 'NOW!',
},
};
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
return sgMail
.send(msg)
.then(response => {
return response;
})
.catch(err => {
throw err;
});
}
The email sends, but I'm still getting unsubstituted templates:电子邮件已发送,但我仍然收到未替换的模板:
The source code for sendgrid-nodejs mail.js seems to say that as long as there is 'substitutions', it will initialize the mailing class with those substitutions but it's not working: sendgrid-nodejs mail.js 的源代码似乎说只要有“替换”,它就会用这些替换初始化邮件类,但它不起作用:
https://github.com/sendgrid/sendgrid-nodejs/blob/master/packages/helpers/classes/mail.js https://github.com/sendgrid/sendgrid-nodejs/blob/master/packages/helpers/classes/mail.js
https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html
How do you properly substitute variables into templates?您如何正确地将变量替换为模板? Am I using the wrong package?我是否使用了错误的软件包?
After a bit more digging, I found the answer in the issues section of their github. 经过一番挖掘后,我在他们的github的问题部分找到了答案。 I was missing 'substitutionWrappers'. 我错过了'substitutionWrappers'。 To get it working, all I had to do was add 'substitutionWrappers' to the message along with 'substitutions': 为了使它工作,我所要做的就是在消息中添加'substitutionWrappers'以及'替换':
const msg = {
to: 'test@email.com'
from: 'info@gmail.io',
subject: 'Receipt for Business Expenses',
template_id: 'da6db3ae-41e4-4e1a-a71b-f5368ab41c9c',
substitutionWrappers: [':', ''],
substitutions: {
firstname: 'Bobba',
ordernumber: 'WHAAA',
orderdate: 'today',
ordertime: 'NOW!',
},
};
There's no need to specify the substitution wrappers as it will assume that you're using Handlebars templating by default.无需指定替换包装器,因为它会假定您默认使用 Handlebars 模板。
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: 'recipient@example.org',
from: 'sender@example.org',
templateId: 'd-f43daeeaef504760851f727007e0b5d0',
dynamicTemplateData: {
subject: 'Testing Templates',
name: 'Some One',
city: 'Denver',
},
};
sgMail.send(msg);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.