Cloud Functions for Firebase•Google Cloud & Mobile Backend

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 mailofly
Setup Tutorial

Get Up and Running in 3 Easy Steps

01

Install inside functions directory

In your Firebase project, run `npm --prefix functions install mailofly` to add the SDK to your Cloud Functions dependencies.

02

Define a Firestore or Auth trigger

Listen to `onDocumentCreated('users/{id}')` or `beforeUserCreated` to automatically fire welcome emails and verification codes.

03

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.

functions/src/index.ts
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" }],
    });
  }
);
Why Developers Love It

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.

Got Questions?

Frequently Asked Questions about Firebase

The standard Firebase Trigger Email extension relies on third-party SMTP servers which often suffer from connection resets and cold starts in serverless functions. Mailofly provides a direct HTTP REST API and official SDK with 30-day logs and multi-provider failover.

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.