继USE CASE在 SendGrids github 上确实设法向我发送带有正确模板的电子邮件,但替换显然不起作用,并且在生成的电子邮件中留空。服务器端:
const sgmailer = require("@sendgrid/mail");
sgmailer.setApiKey(process.env.SENDGRID_API_KEY);
sgmailer.setSubstitutionWrappers('{{', '}}');
const msg = {
to: '...',
from: '[email protected]',
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 ,
我很高兴您正在尝试模板功能!
我希望您度过愉快的一天:)
这里显然缺少变量。
如何正确替换变量?
因为我使用的是dynamic来自 SendGrid 的模板,我无法使用“替换”标签,但必须使用“dynamic_template_data”标签,请参阅这个问题。当将 msg-object 更改为
const msg = {
to: '...',
from: '[email protected]',
subject: 'Hello world',
text: 'Hello plain world!',
html: '<p>Hello HTML world!</p>',
templateId: '...',
dynamic_template_data: {
name: 'Some One',
city: 'Denver',
},
};
有用。据我所知,SendGrid 文档中没有记录这一点。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)