Firebase Trigger Email Extension

Sergei Sevriugin
2 min readJan 8, 2021

What we need is love and good email email service :) that we can use to send email to our users. Firebase is providing Trigger Email Extension that could be a good start if you are using Firebase for mobile app development. The problem here that description of the extension is very short and does not really explain how to use it. So, I have managed to understand and would like to share with you to save you time reading through the source code provided by Google.

First of all we need to create collection that will store information needed to create each email. As a default this collection is called ‘email’. From documentation example we know that we could add new document to this collection to trigger email sending process and the document could be very short like this one:

{
to: ‘someone@example.com’,
message: {
subject: ‘Hello from Firebase!’,
html: ‘This is an <code>HTML</code> email body.’,
},
}

but what other parameters we can add here? The answer give us the following snippet of the function source code:

to: string[]; 
toUids?: string[];
cc: string[];
ccUids?: string[];
bcc: string[];
bccUids?: string[];
from?: string;
replyTo?: string;
headers?: any;

So, you could see that we can use arrays of string to add to, cc and bcc email parameters and we also can use uids as the values in this case you need to provide user collection and each user document should have email field.

Most interesting part is about templates. We can use template field in mail document to ask extension to create email based on template name. In this case our email document should look like this one:

{
to: ‘someone@example.com’,
template: {
name: ‘client001’,
data: {
fullName: 'Sergei Sevriugin
},
}

to make Firebase extension to create email based on this document we shout also provide the extension during configuration stage the name of templates collection where the documents should looks like this:

Firebase Template Collection for Trigger Email Extension

Please note that the documents IDs must match the name value in the template parameter of email collection document.

The whole list of the fields that we could use in the template document is here:

subject?: string; 
html?: string;
text?: string;
amp?: string;
partial?: boolean;

There is the link to GitHub with the extension source code provided by Google Firebase https://github.com/firebase/extensions/tree/master/firestore-send-email/functions/src

Enjoy !

--

--