使用 Nodejs 的 SendGrid 模板中的变量替换不起作用
Variable substitution in SendGrid templates with Nodejs does not work
在 SendGrids USE CASE 之后 github 确实成功地用正确的模板向我发送了电子邮件,但替换显然不起作用,并且在生成的电子邮件中留空邮件。服务器端:
const sgmailer = require("@sendgrid/mail");
sgmailer.setApiKey(process.env.SENDGRID_API_KEY);
sgmailer.setSubstitutionWrappers('{{', '}}');
const msg = {
to: '...',
from: 'sender@example.org',
subject: 'Hello world',
text: 'Hello plain world!',
html: '<p>Hello HTML world!</p>',
templateId: '...',
substitutions: {
name: 'Some One',
city: 'Denver',
},
};
sgmailer.send(msg)
HTML 模板中:
<html>
<head>
<title></title>
</head>
<body>
Hello {{name}},
<br /><br/>
I'm glad you are trying out the template feature!
<br /><br/>
<%body%>
<br /><br/>
I hope you are having a great day in {{city}} :)
<br /><br/>
</body>
</html>
在我的收件箱中生成的电子邮件:
Hello ,
I'm glad you are trying out the template feature!
I hope you are having a great day in :)
这里显然缺少变量。
如何正确替换变量?
因为我使用的是来自 SendGrid 的 dynamic 模板,所以我不能使用 "substitutions" 标签,而必须使用 "dynamic_template_data" 标签,参见this issue。将 msg-object 更改为
时
const msg = {
to: '...',
from: 'sender@example.org',
subject: 'Hello world',
text: 'Hello plain world!',
html: '<p>Hello HTML world!</p>',
templateId: '...',
dynamic_template_data: {
name: 'Some One',
city: 'Denver',
},
};
有效。据我所知,这在 SendGrid 文档中没有记录。
你也可以这样做:
import { getConfig } from '../config';
const msg = {
to: recipient,
from: global.gConfig['SENDGRID_EMAIL_FROM'], // or getConfig().SENDGRID_EMAIL_FROM
templateId: this.templateId,
dynamicTemplateData: this.variables,
};
getConfig.ts
export function getConfig(): any {
return process.env;
}
在 SendGrids USE CASE 之后 github 确实成功地用正确的模板向我发送了电子邮件,但替换显然不起作用,并且在生成的电子邮件中留空邮件。服务器端:
const sgmailer = require("@sendgrid/mail");
sgmailer.setApiKey(process.env.SENDGRID_API_KEY);
sgmailer.setSubstitutionWrappers('{{', '}}');
const msg = {
to: '...',
from: 'sender@example.org',
subject: 'Hello world',
text: 'Hello plain world!',
html: '<p>Hello HTML world!</p>',
templateId: '...',
substitutions: {
name: 'Some One',
city: 'Denver',
},
};
sgmailer.send(msg)
HTML 模板中:
<html>
<head>
<title></title>
</head>
<body>
Hello {{name}},
<br /><br/>
I'm glad you are trying out the template feature!
<br /><br/>
<%body%>
<br /><br/>
I hope you are having a great day in {{city}} :)
<br /><br/>
</body>
</html>
在我的收件箱中生成的电子邮件:
Hello ,
I'm glad you are trying out the template feature!
I hope you are having a great day in :)
这里显然缺少变量。 如何正确替换变量?
因为我使用的是来自 SendGrid 的 dynamic 模板,所以我不能使用 "substitutions" 标签,而必须使用 "dynamic_template_data" 标签,参见this issue。将 msg-object 更改为
时const msg = {
to: '...',
from: 'sender@example.org',
subject: 'Hello world',
text: 'Hello plain world!',
html: '<p>Hello HTML world!</p>',
templateId: '...',
dynamic_template_data: {
name: 'Some One',
city: 'Denver',
},
};
有效。据我所知,这在 SendGrid 文档中没有记录。
你也可以这样做:
import { getConfig } from '../config';
const msg = {
to: recipient,
from: global.gConfig['SENDGRID_EMAIL_FROM'], // or getConfig().SENDGRID_EMAIL_FROM
templateId: this.templateId,
dynamicTemplateData: this.variables,
};
getConfig.ts
export function getConfig(): any {
return process.env;
}