tldr; check Trigger Email from FireStore by Firebase
It’s good for decoupling third-party services from your codebase using Firebase Extension and Functions If you were running your infra on GCP.
After setting up the extension you can queue the email easily with Firestore and when you host on the GCP there are no more credentials needed!
To set up the Firebase extension to trigger mail with SendGrid, follow these steps:
- Go to the Firebase Console and select your project.
- In the left sidebar, select "Extensions."
- Select the "SendGrid Email" extension and click "Install."
- Follow the prompts to set up the extension, including providing your SendGrid API key.
- For SMTP: smtp://apikey@smtp.sendgrid.net:587
- For password → Sg…… (DO NOT base64 it when using in this extention)
- Grab it from your SendGrid console (doc from SendGrid)
- Once the extension is installed, you can send emails from your Firebase project without extra secrets or environment variables.
For example, I used in our codebase:
// FirebaseService.ts
import { Service } from "fastify-decorators";
import { initializeApp, App, applicationDefault, } from 'firebase-admin/app';
import { getFirestore, Firestore } from 'firebase-admin/firestore';
@Service()
export class FirebaseService {
public readonly app: App
public readonly firestore: Firestore
constructor() {
this.app = initializeApp({
credential: applicationDefault()
})
this.firestore = getFirestore(this.app)
}
}
// FirebaseMailService.ts
import { Inject, Service } from "fastify-decorators";
import { FirebaseService } from "./firebase.service";
@Service()
export class FirebaseMailService {
@Inject(FirebaseService)
private readonly firebaseService!: FirebaseService;
async send(to: string | string[], subject: string, message: { text?: string, html?: string }) {
return this.firebaseService.firestore.collection("mail")
.add({
to,
message: {
subject,
text: message.text
},
})
.then(() => console.log("Queued email for delivery!"));
}
}
There are also SMS extensions provided by twillo also worth a look.