【问题标题】:Firestore Cloud Function with SendGrid - Substitutions doesn't show data带有 SendGrid 的 Firestore 云功能 - 替换不显示数据
【发布时间】:2019-08-10 12:50:17
【问题描述】:

在一些帮助下,我成功地创建了一个云函数,它在创建另一个文档时发送一封电子邮件。我的意图是这封电子邮件显示文档中的数据。我的方法是在 SendGrid 模板中使用替换并将它们绑定到 Firestore 中文档的数据。

Firestore 将其数据存储在 /requests 下,每个文档都有一个随机创建的 ID。每次提交表单时都会创建一个文档。

目前我已成功收到一封电子邮件,但没有所需的数据/替换。

const functions = require('firebase-functions');

const admin = require('firebase-admin');
admin.initializeApp();

const SENDGRID_API_KEY = functions.config().sendgrid.key;

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

exports.firestoreRequest = functions.firestore.document('requests/{requestId}')
  .onCreate((snap, context) => {

    const requestId = snap.id; // get the id
    const db = admin.firestore();

    return db.collection('requests').doc(requestId)
      .get()
      .then(doc => {
        const request = doc.data();
        const msg = {
          to: 'fuhr@gmx.net',
          from: 'fuhr@gmx.net',

          templateId: 'd-3cd6b40a74f34b53d1633702107d2',
          substitutionWrappers: ['{{', '}}'],
          substitutions: {
            name: request.name,
            lastname: request.lastname,
            email: request.email,
            package: request.package,
            date: request.date,
            text: request.text
            // and other custom properties here
          }
        };

        return sgMail.send(msg)
      })
      .then(() => console.log('email sent!') )
      .catch(err => console.log(err) )
  });

SendGrid 模板:

    <html>
    <head>
      <title>Request</title>
    </head>
    <body>
      Name: {{name}}<br>
      Family: {{lastname}}<br>
      Email: {{email}}<br>
      Package: {{package}}<br>
      Date: {{date}}<br>
      Text: {{text}}<br>
    </body>
    </html>

【问题讨论】:

    标签: angular firebase google-cloud-firestore sendgrid


    【解决方案1】:

    我发现我指的是错误的 SendGrid 模板类型。如果我错了,请纠正我,但 SendGrid 不再支持旧版电子邮件模板,而这正是我所指的替换模板类型。

    此设置是指我们最初的电子邮件模板。我们现在支持功能更全面的事务模板,支持多个模板、版本控制等。

    我不得不使用替代替换:

              dynamic_template_data: {
                name: request.name,
                lastname: request.lastname,
                email: request.email,
                package: request.package,
                date: request.date,
                text: request.text
                // and other custom properties here
              }
    

    【讨论】:

    • 我和你做的一样,但是我收到了错误:来自 Cloud Functions 的错误请求,知道吗?你还做了什么吗?
    猜你喜欢
    • 2018-11-07
    • 2021-11-18
    • 2019-08-08
    • 2020-08-02
    • 2018-06-30
    • 1970-01-01
    • 2018-05-09
    • 2022-12-20
    • 2018-10-22
    相关资源
    最近更新 更多