Firebase Functions — Trigger Email

Table of Contents

Ronnie Royston
2 min readJan 4, 2023

Introduction
Prerequisites
Install Required Firebase SDK’s and Command Line Tools
Setup Sendgrid
Create Your Firebase Cloud Function Script
Wrapping Up and Testing

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

--

--

Ronnie Royston

Delivering refined solutions via vigorous practice. Tulane ('97), Cisco CCIE# 6824, Google Certified Professional Cloud Architect, and USPA Master Skydiver