How to Send Emails with Firebase Cloud Functions
Trigger transactional emails from Firestore document changes, Firebase Authentication hooks, or HTTP Cloud Functions in seconds.
npm --prefix functions install mailoflyGet Up and Running in 3 Easy Steps
Install inside functions directory
In your Firebase project, run `npm --prefix functions install mailofly` to add the SDK to your Cloud Functions dependencies.
Define a Firestore or Auth trigger
Listen to `onDocumentCreated('users/{id}')` or `beforeUserCreated` to automatically fire welcome emails and verification codes.
Deploy your Firebase Functions
Run `firebase deploy --only functions` to deploy your email trigger to Google Cloud with zero infrastructure maintenance.
Production-Ready Code Example
Copy and paste into your project. Fully typed and tested.
import { onDocumentCreated } from "firebase-functions/v2/firestore";
import { Mailofly } from "@mailofly/node";
const mailofly = new Mailofly({
apiKey: process.env.MAILOFLY_API_KEY!,
});
export const onNewUserCreated = onDocumentCreated(
{
document: "users/{userId}",
secrets: ["MAILOFLY_API_KEY"],
},
async (event) => {
const snapshot = event.data;
if (!snapshot) return;
const userData = snapshot.data();
await mailofly.emails.send({
from: "Welcome <hello@yourdomain.com>",
to: [userData.email],
subject: `Welcome to our platform, ${userData.displayName || "friend"}!`,
html: "<p>We're thrilled to have you here. Your account is verified.</p>",
tags: [{ name: "source", value: "firebase_auth" }],
});
}
);Engineered for Firebase
Automated Firestore Event Triggers
Send emails automatically whenever orders, invitations, or notifications are created in Cloud Firestore.
Secret Manager Integration
Store your Mailofly API key securely in Google Cloud Secret Manager with native `secrets: ['MAILOFLY_API_KEY']` support.
Sub-2s Execution
Fast HTTP execution ensures your Firebase Cloud Functions stay well under CPU timeout limits, reducing GCP costs.
Explore Mailofly Infrastructure Capabilities
Build resilient, high-deliverability email systems with our core developer tools.
Frequently Asked Questions about Firebase
Start Sending from Firebase in Minutes
3,000 free emails every month. Copy your API key, run the code snippet, and watch your delivery logs in real time.