代码之家  ›  专栏  ›  技术社区  ›  Darren

通过firebase函数在发送网格中不工作的替换

  •  1
  • Darren  · 技术社区  · 6 年前

    我在增加 substitutions 通过FireBase Cloud功能从sendgrid发送的电子邮件数据。

    这是我的 function

    exports.firestoreEmail = functions.firestore
    .document('users/{id}')
      .onCreate(snap => {
        const user = snap.data();
        const msg = {
          to: user.email,
          from: 'example@example.com',
          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 如预期,但空白处 替代 数据应该是。

    遵循文档和用例 here 我也试过增加

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

    到全球 setSubstitutionWrappers . 还是不行。

    我也有 console.log(user) 返回要传递给 替代 在控制台中。

    我错过了什么?数据可用,电子邮件格式正确,功能与sendgrid案例完全一致。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Darren    6 年前

    我花了好几个小时才想出来,意识到 substitutions substitutionWrappers 用于 Legacy Transactional Templates .

    而是为了 v3 API 你应该使用 dynamic_template_data 而不是 替代 以及 替换包装纸 似乎被设置为把手 {{ }} .

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

    下一次,我一定要阅读而不是浏览文档…很可能不会。