EmailProvidersFailover

Failover

Automatic failover between multiple email providers with @visulima/email

Failover Provider

The Failover provider automatically falls back to the next provider when the current one fails. This ensures high availability for email sending.

Runtime Support: Depends on the wrapped providers

Setup

import { createMail } from "@visulima/email";
import { failoverProvider } from "@visulima/email/providers/failover";
import { resendProvider } from "@visulima/email/providers/resend";
import { sendGridProvider } from "@visulima/email/providers/sendgrid";
import { smtpProvider } from "@visulima/email/providers/smtp";

const mail = createMail(
    failoverProvider({
        mailers: [
            resendProvider({ apiKey: process.env.RESEND_API_KEY! }),
            sendGridProvider({ apiKey: process.env.SENDGRID_API_KEY! }),
            smtpProvider({ host: "smtp.example.com", port: 587 }),
        ],
    }),
);

Configuration

The FailoverConfig interface extends BaseConfig with the following options:

OptionTypeRequiredDefaultDescription
mailersunknown[]Yes-Array of provider instances or provider factory functions
retryAfternumberNo60Time in milliseconds to wait before trying the next provider
debugbooleanNofalseEnable debug logging
loggerConsoleNo-Custom logger instance

How It Works

  1. The failover provider tries to send the email using the first provider in the mailers array.
  2. If the first provider fails, it tries the second provider, and so on.
  3. If all providers fail, the error from the last provider is returned.
  4. The retryAfter option controls the delay between retries.

Basic Usage

import { MailMessage } from "@visulima/email";

const message = new MailMessage()
    .to("user@example.com")
    .from("sender@example.com")
    .subject("Hello with Failover")
    .html("<h1>Hello World</h1>");

const result = await mail.send(message);

Mixing Runtimes

When using failover in non-Node.js environments, ensure all providers support your runtime:

// Safe for Cloudflare Workers
const mail = createMail(
    failoverProvider({
        mailers: [
            resendProvider({ apiKey: "re_xxx" }),
            sendGridProvider({ apiKey: "SG.xxx" }),
            // Do NOT include smtpProvider here - it requires Node.js
        ],
    }),
);

Supported Features

The failover provider supports whatever features the underlying providers support. Feature availability depends on which provider ultimately sends the email.

Support

Contribute to our work and keep us going

Community is the heart of open source. The success of our packages wouldn't be possible without the incredible contributions of users, testers, and developers who collaborate with us every day.Want to get involved? Here are some tips on how you can make a meaningful impact on our open source projects.

Ready to help us out?

Be sure to check out the package's contribution guidelines first. They'll walk you through the process on how to properly submit an issue or pull request to our repositories.

Submit a pull request

Found something to improve? Fork the repo, make your changes, and open a PR. We review every contribution and provide feedback to help you get merged.

Good first issues

Simple issues suited for people new to open source development, and often a good place to start working on a package.
View good first issues