简体   繁体   中英

Send transactional email with SendGrid in cloud functions Firebase

I followed this tutorial: https://angularfirebase.com/lessons/sendgrid-v3-nodejs-transactional-email-cloud-function/ to send transantional emails. The next function was working normally but with the new update of the google cloud functions https://firebase.google.com/docs/functions/beta-v1-diff#cloud-firestore has stopped working. What should I change?

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))


});

Following the release of Cloud Functions 1.0.x , the following has changed for Firestore, as you can deduce form the doc :

Before (<= v0.9.1)

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

Now (v1.0.0)

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

So, in your case, that means your Cloud Function shall be changed to:

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 => {...
        .....

Cloud Functions Documentation

SendGrid Tutorial

This tutorial demonstrates using Cloud Functions to send emails through the SendGrid platform, receive SendGrid analytics data via webhooks, and load the analytics data into Google BigQuery for analysis.

Objectives

  • Create a SendGrid account.
  • Write and deploy two HTTP Cloud Functions.
  • Write and deploy one Background Cloud Function.
  • Send an email from the deployed function via SendGrid.
  • Receive analytics data from SendGrid via webhooks.
  • Load SendGrid analytics data into BigQuery for analysis.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM