Firebase

Firebasearrow-up-right is Google’s open-source mobile and web app development platform that provides a suite of tools and services for building, managing, and scaling web and mobile applications.

In this article, you’ll learn how to install the Mailtrap Firebase Extension, which allows you to send transactional emails from your Firebase project via Mailtrap without building or maintaining your own cloud functions.

Prerequisites:

Step 1. Install the Firebase extension

To install the Firebase extension, you have two options. Namely:

chevron-rightOption 1: Firebase consolehashtag

Use the quick installarrow-up-right or visit the official Firebase Extension Marketplace page and click Install.

chevron-rightOption 2: Firebase CLIhashtag

With the Firebase CLIarrow-up-right installed, run the following command in your terminal:

firebase ext:install mailtrap/mailtrap-email --project=YOUR_PROJECT_ID

Step 2. Configure Firebase

During installation, you'll be prompted to configure the following parameters:

MAILTRAP_API_TOKEN

Your Mailtrap API token. It will be stored securely in Google Secret Manager.

MAIL_COLLECTION

The Firestore collection to watch for new documents. Default: mail

DEFAULT_FROM_EMAIL

The default sender email address.

DEFAULT_FROM_NAME

The default sender name (optional).

Step 3. How to trigger an email

To trigger an email, add a document to your configured Firestore collection. The extension will pick it up and send it through Mailtrap automatically. Here are some code snippets you can use:

Furthermore, you can create and manage templates directly in the Mailtrap UI without any coding required, and they’ll be accessible to your whole team.

Step 4. Check whether the email has been delivered

In Firestore

After the extension processes the document, it automatically updates it with a delivery field:

Possible states of the delivery field:

  • delivery.state – PENDING, PROCESSING, SUCCESS, ERROR

  • delivery.error – Error message if failed

  • delivery.messageIds – Mailtrap message IDs on success

In Mailtrap

Go to Email Logs in your Mailtrap account to see full delivery details, open and click tracking, and the complete event history for each message.

Configuring Firestore Security Rules

You must pay attention to Security Rules so that email delivery is appropriately secure. Almost certainly you want the mail collection to be forbidden to users and only accessible to functions. Then you trigger it from your backend code.

To control who can add documents to your mail collection, you can set up Firestore Security Rules by copying the following code snippet into the firestore.rules file or directly in the console under Firestore DatabaseRules:

circle-info

Note that if you do make it accessible to users, they could trigger email delivery as much as they want, so it's up to you to develop a suitable authorization model.

Billing

This extension uses the following Firebase services which may have associated charges:

  • Cloud Firestore

  • Cloud Functions (2nd gen)

  • Secret Manager

This extension also uses Mailtrap's email sending service, which has its own pricingarrow-up-right.

Last updated

Was this helpful?