How to write an email trigger using Firebase

An email trigger is an automated action that sends an email based on a specific event or behavior. It’s usually set up in email marketing or customer relationship management software to help businesses engage with their customers in a timely and relevant way. For example, an email trigger could be triggered by a customer subscribing to a newsletter, making a purchase, etc. When the trigger is activated, the system sends a pre-defined email to the customer, such as a welcome message or a purchase confirmation.

You can automatically send emails using the firestore-send-email trigger email extension. This extension sends emails based on the documents in the Cloud Firestore collection. The extension is automatically triggered whenever a new document is added to the collection and an email is sent.

How it works

After installing and setting up the extension from your Firebase Console, this extension will keep a check on all new documents being added and will send an email accordingly. The email is based on the fields of the document. The fields of the document are divided into the following two sections:

  • The top-level fields specify the emails of the sender and receiver. These fields also specify the to, cc, and bcc options.

  • The subject and the body of the email are specified by the message field of the document.

Code example

Let’s look at the code below:

admin.firestore().collection('mail').add({
from: 'myname@example.com',
to: 'someone@example.com',
cc: 'cc@example.com',
bcc: ['bcc1@example.com', 'bcc2@example.com'],
replyTo: 'reply-to@example.com',
toUids: ['user-id-1', 'user-id-2'],
ccUids: ['user-id-3', 'user-id-4'],
bccUids: ['user-id-5', 'user-id-6'],
headers: {
'X-Custom-Header': 'Hello, world!',
'X-Another-Header': 'foo',
'X-Yet-Another-Header': 'bar',
},
message: {
messageID: '1234567890@educative.io',
subject: 'Welcome to Educative!',
text: 'To get started, create your account at educative.io',
html: '<p>To get started, create your account at <a href="https://www.educative.io/">Educative</a>.</p>',
attachments: [
{
filename: 'document.pdf',
content: 'base64-encoded-file-contents',
contentType: 'application/pdf'
},
{
filename: 'image.jpg',
content: 'base64-encoded-file-contents',
contentType: 'image/jpeg'
}
],
amp: '<!doctype html><html ⚡4email><head><meta charset="utf-8"><style amp4email-boilerplate>body{visibility:hidden}</style><script async src="https://cdn.ampproject.org/v0.js"></script><script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script></head><body><p>This is an AMP email!</p></body></html>'
},
});

Sender and receiver fields

All of the information about the sender and the receiver of the email is determined from the top-level fields of the document. The available fields are as follows:

  • from (optional): This is the email of the sender. If this field is not specified, the configured parameter Default FROM address is used.
  • to: This is the receiver/receivers of the email.
  • cc (optional): This is the receiver/receivers of the email.
  • bcc (optional): This is the receiver/receivers of the email.
  • replyTo (optional): This is the reply-to email. If this field is not specified, the configured parameter Default REPLY-TO address is used.
  • toUids: This is an array of receiver UIDs.
  • ccUids (optional): This is an array of the cc receiver UIDs.
  • bccUids (optional): This is an array of the bcc receiver UIDs.
  • headers (optional): This field specifies additional headers.

Message field

This field contains the information in the body of the email. The properties of the message field are:

  • messageId (optional): This is the ID for the email.
  • subject: This is the subject of the email.
  • text: This is the text of the email.
  • HTML: This is the HTML content of the email.
  • attachments: This is the array of the attachments.
  • amp: This is the AMP4EMAIL content of the email.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved