我遵循了 SendGrid 文档来创建动态交易电子邮件。但是不知何故我无法分配变量。他们总是返回空。
const sgMail = require("@sendgrid/mail")
const SENDGRID_API_KEY = "deleted for safety, no worries i fill the right value here"
sgMail.setSubstitutionWrappers("{{", "}}")
sgMail.setApiKey(SENDGRID_API_KEY)
const msg = {
to: "deleted for safety, no worries i fill the right value here",
from: "deleted for safety, no worries i fill the right value here",
subject: "Hello world",
text: "Hello plain world!",
html: "<p>Hello HTML world!</p>",
templateId: "deleted for safety, no worries i fill the right value here",
substitutions: {
name: "Some One",
city: "Denver",
},
};
sgMail.send(msg);
模板:
<html>
<head>
<title></title>
</head>
<body>
Hello {{name}},
<br /><br/>
I'm glad you are trying out the template feature!
<br><br>
I hope you are having a great day in {{city}} :)
<br /><br/>
</body>
</html>
结果:
你好,
很高兴您正在试用模板功能!
我希望你在 :) 过得愉快
Api 密钥是正确的,结果是我收到的邮件。你们能告诉我我错过了什么吗?