按照 SendGrids github 上的USE CASE确实设法用正确的模板向我发送电子邮件,但替换显然不起作用,并且在生成的电子邮件中留空。服务器端:
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>
在我的收件箱中生成的电子邮件:
你好 ,
很高兴您正在试用模板功能!
我希望你在 :) 过得愉快
这里显然缺少变量。如何正确替换变量?