Setup Firebase Trigger Email & Cloud Functions

Ayesha Iftikhar
4 min readDec 26, 2023

Firebase provides a range of services and features that can help you build and manage your mobile and web applications. Two key components of Firebase are Cloud Functions and some extensions such as Trigger Emails. By combining these two services, you can create powerful and automated workflows that respond to events in your application and send emails to users.

Steps to follow:

  • Create a project if you haven’t already here.
  • To use Trigger Email from Firestoreextension and cloud functions, you need to upgrade the project to BLAZE Plan.
  • Go on and set-up firestore database and storage. This is necessary for both extension and functions to work.

Configure Extension

  • Click on Extensions panel under Build.
  • Find Trigger Email from Firestoreextension and click on install.
  • Click install > Select Project to install extension.
  • Click on Extensions panel under Build.
  • Next > enable services if any of the mentioned is not already enabled and click next.
  • Configure extension. Specially the origin (it is always preferable to select the same origin as of your firebase project to work for it smoothly.

URI

If the mail I’m linking is xyz123@gmail.com, this will be your SMTPS format:

smtps://xyz123@gmail.com@smtp.gmail.com:465

Use this in the SMTPS connection URI field.

Password

This is a little hectic step.

  • Enable 2 factor Authorization in your Gmail here.
  • Now you would need to create an App Password.
  • You have to enter this password in the SMTP password field and click Create secret.
  • After it’s done, Your screen will look like this.
  • After that add a default from email address.
  • You could keep the same Gmail for Default Reply-To address as the original mail, or one of your choice. (Note: you may have to add a reply-to email address otherwise it will not work properly.)
  • Let Email documents collection be the same.
  • Click on Install Extension.

This will take 3–5 minutes.

Send a test email!

  • To send a email, you have to create a collection mail to send emails to user in cloud firestore. Find official documentation here.
to: ['someone@example.com'],
message: {
subject: 'Hello from Appname!',
text: 'Body of the email being sent.',
html: 'This is the <code>HTML</code> section of the email body. if you want to attach any html.',
}

The collection will have to as array data type. message will be a map having subject, text & html field.

Manual Collection Example:

  • Create a collection and document.
  • If done correctly, within few seconds, you’ll see the document automatically update with more fields like attempts etc.
  • Check your mail for the email.

Writing a Cloud Function

  • Setup Firebase CLI.
  • Download and installnode.js from here, if not download already.
  • Download and install Firebase CLI by following the steps here..
  • Login to firebase cli using the above doc.
  • Open your project in code editor, and type firebase init in terminal.
  • Select project and add functions support. It’ll create a new folder functions.
  • I’ve written a function that sends a welcome email when a new user is created.
const functions = require("firebase-functions");
const admin = require("firebase-admin");

admin.initializeApp();

// sends mail if new user is regestired
exports.userWelcomeMail = functions.region('europe-central2').auth.user().onCreate((user) => {
admin.firestore().collection("mail").add({
"to": [user.email],
"message": {
"subject": "Welcome to PurePairing! Explore functionalities here.",
"text": `Hi, ${user.displayName}. \n\nIt's nice to have you on-board.`,
},
})
.then((result) => {
console.log(
"onboarding email result: ", result,
"\ntime-stamp: ", Date.now);
});
});

Additional Resources

  • Learn firebase cloud functions here. really recommend this channel.
  • Official Trigger Email from Firestore docs.
  • Firebase CLI docs.
  • Firebase Cloud Functions docs.

--

--

Ayesha Iftikhar

I am professional software engineer with experience of around 4 years in Mobile Application Development using Flutter and overall 5 years of experience in IT.