EmailProvidersMailgun

Mailgun

Send emails using the Mailgun API with @visulima/email

Mailgun Provider

The Mailgun provider allows you to send emails through the Mailgun API, a developer-friendly email API service.

Runtime Support: Universal (Node.js, Deno, Bun, Cloudflare Workers)

Setup

import { createMail } from "@visulima/email";
import { mailgunProvider } from "@visulima/email/providers/mailgun";

const mail = createMail(
    mailgunProvider({
        apiKey: process.env.MAILGUN_API_KEY!,
        domain: "mg.example.com",
    }),
);

Configuration

The MailgunConfig interface extends BaseConfig with the following options:

OptionTypeRequiredDefaultDescription
apiKeystringYes-Your Mailgun API key
domainstringYes-Your Mailgun sending domain
endpointstringNohttps://api.mailgun.netAPI endpoint (use https://api.eu.mailgun.net for EU accounts)
debugbooleanNofalseEnable debug logging
loggerConsoleNo-Custom logger instance
retriesnumberNo3Number of retry attempts on failure
timeoutnumberNo30000Request timeout in milliseconds

Basic Usage

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

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

const result = await mail.send(message);

Provider-Specific Email Options

The Mailgun provider supports additional options through MailgunEmailOptions:

import type { MailgunEmailOptions } from "@visulima/email/providers/mailgun";

const options: MailgunEmailOptions = {
    from: { email: "sender@mg.example.com" },
    to: { email: "user@example.com" },
    subject: "Hello",
    html: "<h1>Hello</h1>",
    tags: ["welcome", "onboarding"],
    clickTracking: true,
    openTracking: true,
    testMode: false,
    template: "welcome-template",
    templateVariables: { name: "John" },
};
OptionTypeDescription
tagsstring[]Tags for categorization
templatestringMailgun template name
templateVariablesRecord<string, unknown>Template variables
campaignIdstringCampaign ID for tracking
deliveryTimestring | numberDelivery time (RFC 2822 date or Unix timestamp)
clickTrackingbooleanEnable/disable click tracking
openTrackingbooleanEnable/disable open tracking
unsubscribeTrackingbooleanEnable/disable unsubscribe tracking
testModebooleanTest mode - emails will not actually be sent
requireTlsbooleanRequire TLS for delivery
skipVerificationbooleanSkip TLS certificate verification

EU Region

For EU-based Mailgun accounts, use the EU endpoint:

const mail = createMail(
    mailgunProvider({
        apiKey: process.env.MAILGUN_API_KEY!,
        domain: "mg.example.com",
        endpoint: "https://api.eu.mailgun.net",
    }),
);

Supported Features

FeatureSupported
AttachmentsYes
Batch SendingYes
Custom HeadersYes
HTMLYes
Reply-ToYes
SchedulingYes
TaggingYes
TemplatesYes
TrackingYes

Additional Methods

Retrieve Email

Retrieve email event details by message ID:

const provider = mailgunProvider({ apiKey: "xxx", domain: "mg.example.com" });
const result = await provider.getEmail("message-id");
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