← All @molecule/* packages · App templates
@molecule/api-emails-mailgunProvider bond · email · API (Node) · v1.0.1 · Apache-2.0
Mailgun email provider for molecule.dev.
npm install @molecule/api-emails-mailgunnpm · Source on GitHub · Implements @molecule/api-emails
@molecule/api-emails-mailgun is a provider bond on the API (Node) side: it implements the email core interface (@molecule/api-emails) with a concrete vendor or library behind it.
Your code calls the core; you wire this provider once at startup. Swapping vendors later is one line in that wiring, not a rewrite.
import { setTransport } from '@molecule/api-emails'
import { provider } from '@molecule/api-emails-mailgun'
setTransport(provider)Works with: @molecule/api-bond, @molecule/api-emails, @molecule/api-secrets
Secrets: MAILGUN_API_KEY, MAILGUN_DOMAIN, MAILGUN_API_HOST (optional)
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
Mailgun email provider for molecule.dev.
import { setTransport } from '@molecule/api-emails'
import { provider } from '@molecule/api-emails-mailgun'
setTransport(provider)
provider
npm install @molecule/api-emails-mailgun @molecule/api-bond @molecule/api-emails @molecule/api-secrets nodemailer nodemailer-mailgun-transport
npm install -D @types/nodemailer @types/nodemailer-mailgun-transport
EmailMessageEmail message options.
interface EmailMessage {
/**
* Sender address.
*/
from: string | EmailAddress
/**
* Recipient(s).
*/
to: string | EmailAddress | (string | EmailAddress)[]
/**
* CC recipient(s).
*/
cc?: string | EmailAddress | (string | EmailAddress)[]
/**
* BCC recipient(s).
*/
bcc?: string | EmailAddress | (string | EmailAddress)[]
/**
* Reply-to address.
*/
replyTo?: string | EmailAddress
/**
* Email subject.
*/
subject: string
/**
* Plain text body.
*/
text?: string
/**
* HTML body.
*/
html?: string
/**
* File attachments.
*/
attachments?: EmailAttachment[]
/**
* i18n key for the subject (for client-side translation).
*/
subjectKey?: string
/**
* i18n key for the plain text body (for client-side translation).
*/
textKey?: string
/**
* i18n key for the HTML body (for client-side translation).
*/
htmlKey?: string
}
EmailSendResultResult of sending an email.
interface EmailSendResult {
/**
* Whether the email was accepted for delivery.
*/
accepted: string[]
/**
* Addresses that were rejected.
*/
rejected: string[]
/**
* Message ID from the provider.
*/
messageId?: string
/**
* Raw response from the provider.
*/
response?: string
}
EmailTransportEmail transport interface.
All email providers must implement this interface.
interface EmailTransport {
/**
* Sends an email message.
* @returns The send result.
*/
sendMail(message: EmailMessage): Promise<EmailSendResult>
}
sendMail(message)Sends an email message via the Mailgun API, with automatic test-mode handling for sandbox domains.
function sendMail(message: EmailMessage): Promise<EmailSendResult>
message — The email message (to, from, subject, text/html, attachments).Returns: Send result with accepted/rejected addresses and message ID.
email (deprecated)Raw nodemailer transport alias.
const email: { sendMail: (msg: nodemailer.SendMailOptions) => Promise<any> }
mailgunSecretDefinitionsSecret definitions required by the Mailgun email bond.
const mailgunSecretDefinitions: SecretDefinition[]
providerThe Mailgun email provider implementing the EmailTransport interface.
const provider: EmailTransport
transport (deprecated)Raw nodemailer transport for direct access.
const transport: { sendMail: (msg: nodemailer.SendMailOptions) => Promise<any> }
Implements @molecule/api-emails interface.
Setup function to register this provider with the core interface:
import { setTransport } from '@molecule/api-emails'
import { provider } from '@molecule/api-emails-mailgun'
export function setupEmailsMailgun(): void {
setTransport(provider)
}
Peer dependencies:
@molecule/api-bond ^1.0.1@molecule/api-emails ^1.0.1@molecule/api-secrets ^1.0.1MAILGUN_API_KEY (required) — Mailgun API key
MAILGUN_DOMAIN (required) — Mailgun sending domain
mg.example.comMAILGUN_API_HOST (optional) — Mailgun API host
api.eu.mailgun.net@molecule/api-bond
@molecule/api-emails
@molecule/api-secrets
nodemailer
nodemailer-mailgun-transport
EU-region Mailgun accounts must set MAILGUN_API_HOST=api.eu.mailgun.net
(optional env; defaults to Mailgun's US endpoint). Without it every send
fails upstream with 401 even though the key is valid — wrong region, not
wrong key.
Sandbox domains auto-enable Mailgun test mode: when MAILGUN_DOMAIN
matches sandbox*.mailgun.org (or MAILGUN_TEST_MODE=true), sends carry
o:testmode=yes — Mailgun accepts, validates, and assigns a message id
but NEVER delivers. A sandbox 403 for an unauthorized recipient is
reported as a synthetic success (response: 'sandbox-test-mode'). "Send
succeeded but no email arrived" in dev is this behavior, not a bug.
Credentials are read lazily on first send and fail fast with a tagged
config.notConfigured error naming the missing key (MAILGUN_API_KEY,
then MAILGUN_DOMAIN). On success accepted echoes the message's own
recipients (Mailgun's transport returns no per-recipient verdict).
The from address's domain must equal MAILGUN_DOMAIN (Mailgun sends
through, and signs SPF/DKIM for, that verified domain). A from on any other
domain — a hardcoded noreply@example.com, noreply@store.com, etc. — is
rejected or unsigned (spam). Default the sender to the sending domain:
process.env.EMAIL_FROM ?? `no-reply@${process.env.MAILGUN_DOMAIN}` — never
a literal placeholder domain.
Integration checklist — drive the real UI (live preview, no mocks). The
sandbox CAPTURES outbound email instead of sending — read each message with
the read_activity tool (filter type 'email'); the verification/reset link
is in its payload. Never mock the send or modify production code to expose
it. Adapt each item to this app's actual screens/flows, and check every box
off one by one. A box you can't check is an integration bug to fix — not a
skip:
undefined placeholders).