我在通过Firebase Cloud Functions从Sendgrid发送的电子邮件中添加substitutions数据时遇到麻烦。

这是我的function

exports.firestoreEmail = functions.firestore
.document('users/{id}')
  .onCreate(snap => {
    const user = snap.data();
    const msg = {
      to: user.email,
      from: '[email protected]',
      subject: `${user.firstName}, please Verify Your Email Address`,
      templateId: 'templateID',
      substitutionWrappers: ['{{', '}}'],
      substitutions: {
        firstName: user.firstName,
        email: user.email,
        id: user.id
      }
    };
    return sgMail
      .send(msg)
      .then(() => console.log('email sent!'))
      .catch(err => console.log(err));
  });


并且templateId的事务模板是

<html>
  <head></head>
  <body>{{firstName}} - {{email}} - {{id}}</body>
</html>


这将按预期方式将电子邮件返回到user.email,但在substitutions数据应保留空白的地方。

按照文档和用例here,我也尝试添加

sgMail.setSubstitutionWrappers('{{', '}}');


到全局setSubstitutionWrappers。仍然不起作用。

我还有console.log(user),它返回要传递到控制台中substitutions的数据。

我想念什么?数据可用,电子邮件格式正确,并且功能完全符合SendGrid案例。

最佳答案

经过数小时的努力,我设法弄清楚了这一点,意识到substitutionssubstitutionWrappers是用于Legacy Transactional Templates的。

对于v3 API,您应该使用dynamic_template_data而不是substitutions,并且substitutionWrappers似乎设置为车把{{ }}

  dynamic_template_data: {
    firstName: user.firstName,
    email: user.email,
    id: user.id
  }


下次,我将确保阅读且不会浏览文档……很可能不会。

关于javascript - 替代无法通过Firebase功能在发送网格中工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52933914/

10-09 14:23