Firebase Functions - Trigger Email

0

By Ron Royston

Originally Published 5/21/2018
Updated 5/21/2018

Introduction

This article gets you up and running as quickly and easily as possible. We are going to trigger an email to a 3rd party when a new sale document is added to a sales collection in our Firestore database. The idea is to notify the customer (the 3rd party) that their purchase has been initiated. Sendgrid will send the email as if it came from your domain and it will be prepopulated with arguments that are passed to sendgrid by Cloud Functions. So, let's jump right in.

Prerequisites

  1. Firebase Account w Billing Enabled
  2. Sendgrid Account
  3. Firestore Enabled
  4. Node.js and Node Package Manager, NPM, Installed

Install Required Firebase SDK's and Command Line Tools

From your root directory:

npm install firebase-functions@latest --save
npm install firebase-admin@latest --save
npm install -g firebase-tools
firebase init functions

Now cd functions and from your functions directory:

npm install @sendgrid/mail --save

Setup Sendgrid

Get your Sendgrid API key from your dashboard on their website. Save your sendgrid API key in the cloud functions environment by issuing the following command from the functions directory.

firebase functions:config:set sendgrid.key=YOUR_API_KEY

Back on the sendgrid dashboard, whitelist your app's domain and update your DNS server per sendgrid instructions (optional but recommended). Create a simple transactional template with the following text

Hello {{name}}, we are processing your order.
<%body%>

Make note of your template id, you will need it in the next step.

Create Your Firebase Cloud Function Script

The script below uses the 1.0v SDK. For the updated documentation, see Firebase SDK for Cloud Functions Migration Guide: Beta to version 1.0

var functions = require('firebase-functions');
var sendgrid = require("@sendgrid/mail");
var admin = require('firebase-admin');
admin.initializeApp();
var SENDGRID_API_KEY = functions.config().sendgrid.key;
sendgrid.setApiKey(SENDGRID_API_KEY);
exports.newSale = functions.firestore.document("sales/{sale}").onCreate(function (snap, context) {
    var sale = snap.data();
    var msg = {
        to: sale.email,
        from: "connect@hightechtele.com",
        subject: "New Order",
        templateId: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
        substitutionWrappers: ["{{", "}}"],
        substitutions: {
            name: sale.displayName
        }
    };
    return sendgrid.send(msg);
});

Wrapping Up and Testing

Deploy to Firebase.

firebase deploy

To test, create a new sale document. This document must be created in a "sales" collection at the root of your Firestore database. If you are working with a web app, from a browser console create the doc like below:

var o = {};
o.email = "you@youremail.com";
o.displayName = "Bob Customer";
firebase.firestore().collection("sales").doc("fakesaleid").set(o);

Automate a workflow or process today with standards based, library free JavaScript in the browser, in the cloud with NodeJS, or both. High Tech Telecom can do that! See our services page for more details.

Dialog Title

Cell Phone Number

Your order ID is . The grand total is , or bitcoin.

This action cannot be undone. Continue?

Changes will be lost. Continue?

Delete location?






A signin link will be sent to your email address.

You privacy is top priority. Email addresses are never shared with a third party. For more information see our privacy policy.

Find a bug? Experience an error? How can we do better? We appreciate your feedback.

Delete order? This action cannot be undone.

Delete order? This action cannot be undone.