Email Notifications With Firebase Cloud Functions

Zalmy Muskal
3 min readSep 25, 2020

If you haven’t set up Cloud Functions yet check out my other blog here. If you have set up Cloud Functions, then you may have realized that Cloud Functions doesn’t have any built in ways to send out emails. So for this tutorial we will use Nodemailer with Gmail. When using Gmail, keep in mind that they do have an email sending quota per day (the trade off is that you don’t have to worry about getting charged on your Firebase plan).

Install Nodemailer

In your Cloud-Functions/functions directory enter
npm install nodemailer .

Next make sure to require it on the top of your index.js file.

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

Setting up Gmail

There are several steps to setting up your Gmail account.

  1. Enable Access to less secure apps.
  2. Allow access to your Google account with Display Unlock Captcha.
  3. If you have two step verification enabled you will need to follow this extra step to Generate an App Password. You can do this by going to your Google Account and select Security. Go to Signing in to Google and select App Passwords. It might ask you to sign in.
    On the bottom you should see Select app. Choose the app you are using. For Select device choose the device you are using. Click Generate.
    Follow the instructions on how to enter the App Password. The password should be the 16 character code in the yellow bar on your device.
    Click Done.

Cloud Environment Variables

Now it is time to set up the Cloud Function environment variables. We will use this to set up the email we will be sending from and the password for that email. If you have two step verification set up you can use this to also store the App Password.

firebase functions:config:set gmail.email="myusername@gmail.com" gmail.password="secretpassword"

Now for the code

For this tutorial I will use a welcome email that is sent to users when they sign into your app. But the same principles apply for any other notifications you want to send.

In order to do this we will use the Cloud Function trigger onCreate. We will also turn it into an async function.
functions.auth.user().onCreate(async (user) => {})

There are three parts to sending out the email.

  1. Usenodemailer.createTransport() to create a transporter (How you are sending the email) for either SMTP or some other transporter.
  2. Set up Message Options (Who is sending, to whom your are sending and the what you are sending)
  3. Sending out the email with sendMail (method of the transporter you created.)

Now to see it all put together:

const functions = require('firebase-functions');
const nodemailer = require("nodemailer")
exports.addUser = functions.auth.user().onCreate(async (user) => {
// Get the email and password from the variables we created
const gmailEmail = functions.config().gmail.email;
const gmailPassword = functions.config().gmail.password;
// Create the transporter
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: gmailEmail,
pass: gmailPassword,
},
});
// Create the Message options
const messageOptions = {
from: gmailEmail,
to: user.email,
subject: 'Welcome To My App!',
text: `Hey ${user.displayName || ''}! Welcome to My App. I hope you will enjoy our service.`
};
// Send out the email
await transporter.sendMail(messageOptions)
return null
})

And that’s it! I hope this helps you get started and happy coding!

--

--