【问题标题】:Send transactional email with SendGrid in cloud functions Firebase在云功能 Firebase 中使用 SendGrid 发送交易电子邮件
【发布时间】:2018-10-16 17:44:57
【问题描述】:

我按照本教程:https://angularfirebase.com/lessons/sendgrid-v3-nodejs-transactional-email-cloud-function/ 发送跨国电子邮件。下一个功能正常工作,但随着谷歌云功能的新更新https://firebase.google.com/docs/functions/beta-v1-diff#cloud-firestore 已停止工作。我应该改变什么?

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

admin.initializeApp(functions.config().firebase);
const SENDGRID_API_KEY = functions.config().sendgrid.key

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

exports.firestoreEmail = functions.firestore
.document('mensajes/{mensajeId}')
.onCreate(event => {

    const mensajeId = event.params.mensajeId;

    const db = admin.firestore()

    return db.collection('mensajes').doc(mensajeId)
        .get()
        .then(doc => {

            const mensaje = doc.data()

            const msg = {
                to: 'xx@xx.com',
                from: 'zz@zz.com',
                subject: 'Subject',
                templateId: 'myTemplateID',
                substitutionWrappers: ['{{', '}}'],
                substitutions: {

                    nombre: mensaje.nombre,
                    telefono: mensaje.telefono,
                    email: mensaje.email,
                    mensaje: mensaje.mensaje

                }
            };

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


});

【问题讨论】:

    标签: firebase google-cloud-firestore google-cloud-functions sendgrid


    【解决方案1】:

    随着 Cloud Functions 1.0.x 的发布,Firestore 的以下内容发生了变化,您可以从 doc 中推断出:

    之前 (

    exports.dbCreate = functions.firestore.document('notes/{noteId}').onCreate((event) => {
        const newData = event.data.data();
        const param = event.params.noteId;
    });
    

    现在 (v1.0.0)

    exports.dbCreate = functions.firestore.document('notes/{noteId}').onCreate((snap, context) => {
      const newData = snap.data(); 
      const param = context.params.noteId;
    });
    

    因此,就您而言,这意味着您的云功能应更改为:

    exports.firestoreEmail = functions.firestore
    .document('mensajes/{mensajeId}')
    .onCreate(event => {
    
        const mensajeId = context.params.mensajeId;  // <- Here is the change
    
        const db = admin.firestore()
    
        return db.collection('mensajes').doc(mensajeId)
            .get()
            .then(doc => {...
            .....
    

    【讨论】:

      【解决方案2】:

      Cloud Functions Documentation

      SendGrid 教程

      本教程演示如何使用 Cloud Functions 通过 SendGrid 平台发送电子邮件,通过 webhook 接收 SendGrid 分析数据,并将分析数据加载到 Google BigQuery 中进行分析。

      目标

      • 创建一个 SendGrid 帐户。
      • 编写和部署两个 HTTP 云函数。
      • 编写和部署一个后台云函数。
      • 通过 SendGrid 从部署的函数发送电子邮件。
      • 通过 webhook 从 SendGrid 接收分析数据。
      • 将 SendGrid 分析数据加载到 BigQuery 中进行分析。

      【讨论】:

        猜你喜欢
        • 2021-06-18
        • 2019-08-08
        • 2019-05-21
        • 2017-12-03
        • 2020-11-22
        • 2018-11-15
        • 2022-01-19
        • 2018-08-24
        • 1970-01-01
        相关资源
        最近更新 更多