如何替换Sendgrid模板变量?(在node-js中)

Kyl*_*ong 3 javascript email templates node.js sendgrid

它似乎是一个我想念的简单参数,但我无法弄清楚究竟是什么.

这是我用'@ sendgrid/mail'发送的请求:

email.js:

const sgMail = require('@sendgrid/mail');

function emailRequest() {
    msg = {
      to: 'test+10@gmail.com
      from: 'info@owner.io',
      subject: 'Receipt for Business Expenses',
      template_id: 'db6d11ae-41e4-4e1a-a71b-f5368eb21c9c',
      personalizations: [
        {
          to: 'test+10@gmail.com,
          from: 'info@ownr.io,
          subject: 'Receipt for Business Expenses,
          template_id: 'db6d11ae-41e4-4e1a-a71b-f5368eb21c9c',
          substitutions: {
            ':firstname': 'Bobba',
            ':ordernumber': 'WHAAA',
            ':orderdate': 'today',
            ':ordertime': 'NOW!',
          },
          custom_args: {
            ':firstname': 'Bobba',
            ':ordernumber': 'WHAAA',
            ':orderdate': 'today',
            ':ordertime': 'NOW!',
          },
        },
      ],
      sub: {
        ':firstname': 'Bobba',
        ':ordernumber': 'WHAAA',
        ':orderdate': 'today',
        ':ordertime': 'NOW!',
      },
      substitutions: {
        ':firstname': 'Bobba',
        ':ordernumber': 'WHAAA',
        ':orderdate': 'today',
        ':ordertime': 'NOW!',
      },
    };

  sgMail.setApiKey(process.env.SENDGRID_API_KEY);

  return sgMail
    .send(msg)
    .then(response => {
      return response;
    })
    .catch(err => {
      throw err;
    });
}
Run Code Online (Sandbox Code Playgroud)

电子邮件发送,但我仍然得到未取消的模板:

在此输入图像描述

sendgrid-nodejs mail.js的源代码似乎说只要有'替换',它就会用这些替换来初始化邮件类,但是它不起作用:

https://github.com/sendgrid/sendgrid-nodejs/blob/master/packages/helpers/classes/mail.js

https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html

你如何正确地将变量替换为模板?我使用了错误的包装吗?

Kyl*_*ong 9

经过一番挖掘后,我在他们的github的问题部分找到了答案.我错过了'substitutionWrappers'.为了使它工作,我所要做的就是在消息中添加'substitutionWrappers'以及'替换':

const msg = {
    to: 'test@email.com'
    from: 'info@gmail.io',
    subject: 'Receipt for Business Expenses',
    template_id: 'da6db3ae-41e4-4e1a-a71b-f5368ab41c9c',
    substitutionWrappers: [':', ''],
    substitutions: {
      firstname: 'Bobba',
      ordernumber: 'WHAAA',
      orderdate: 'today',
      ordertime: 'NOW!',
    },
  };
Run Code Online (Sandbox Code Playgroud)