sendgrid 替换不起作用

Ric*_*d G 2 sendgrid sendgrid-templates

我只是想为 SendGrid 设置一个试用电子邮件,这是我第一次使用它,所以我确定这很简单,但我似乎无法替换占位符数据。

我正在使用这样的 NodeJS 库:

sgMail.setApiKey(mailConfig.apiKey);

const msgConfig = {
  to: email,
  from: mailConfig.defaults.from,
  templateId: mailConfig.templates.registrationConfirmation,
  substitutions: {
    '--displayName--': original.displayName,
    '--companyName--': 'Hello world'
  }
};

console.log('Sending: ', msgConfig);

// now send the registration confirmation email.
return sgMail.send(msgConfig).then(() => {
  console.log('done.');
})
.catch((err) => {
  console.error(JSON.stringify(err));
});
Run Code Online (Sandbox Code Playgroud)

在模板中有一个我使用可视化编辑器添加的文本块:

Hello --displayName--

We would love to take this opportunity to welcome you to the store.

from

--companyName--
Run Code Online (Sandbox Code Playgroud)

但是,当我运行测试以发送电子邮件时,它可以正常发送邮件,但不会替换占位符。

我在这里缺少什么?

cam*_*epo 5

Sendgrid 文档中并不清楚,但我认为它缺少这一行:

sgMail.setSubstitutionWrappers('--', '--'); // Configure the substitution tag wrappers globally
Run Code Online (Sandbox Code Playgroud)

然后删除替换对象键中的破折号

看这个链接:Sendgrid

所以,你的代码应该是:

sgMail.setApiKey(mailConfig.apiKey);

// Configure the substitution tag wrappers globally
sgMail.setSubstitutionWrappers('--', '--');

const msgConfig = {
  to: email,
  from: mailConfig.defaults.from,
  templateId: mailConfig.templates.registrationConfirmation,
  substitutions: {
    'displayName': original.displayName,
    'companyName': 'Hello world'
  }
};
...
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助你!


小智 5

尝试将“替换”更改为“dynamicTemplateData”。看起来他们在新版本中更改了名称。

我是怎么想出来的:https : //github.com/sendgrid/sendgrid-nodejs/blob/master/packages/mail/USE_CASES.md