Files from bitbaum/fleet `templates/package` that this repository does not have yet: - `.github/workflows/release-notify-tagged.yml` Only missing files are added — no file this repository already has is changed. `release.yml` is what lets the package owner release a new version from Loki (the package page: "Release vX.Y.Z"), or from the Actions tab. Opened by bitbaum/fleet `scripts/packages/sync-package-template.mjs` (`.github/workflows/package-repos.yml`). It merges itself when this repository's CI is green. Co-authored-by: bitbaum-fleet <fleet@bitbaum.invalid>
@bitbaum/mail-kit
The fleet's one email layer. One env contract, one call shape, over
Resend (the default) or the app's own mail server (SMTP). Zero runtime
dependencies for Resend; SMTP uses nodemailer, an optional peer.
Exists because the fleet grew ~12 bespoke email implementations across three provider styles, and a dead SMTP credential silenced two apps' outbound mail for months without anyone noticing. Uniformity is the fix.
Install
pnpm add @bitbaum/mail-kit
Use
import { sendMail, isMailConfigured, mailHealth, conventionalFrom } from "@bitbaum/mail-kit";
const result = await sendMail({
to: "person@example.ch",
subject: "Your invoice",
html: "<p>…</p>",
attachments: [{ filename: "invoice.pdf", content: pdfBuffer, contentType: "application/pdf" }],
});
if (!result.sent) {
// result.error, result.status?, result.retryable — sendMail NEVER throws.
logger.error("invoice email failed", result);
}
sendMailnever throws. A thrown email error inside a server action reaches nobody and destroys the form; a{ sent: false }you must look at is deliverable. Callers where delivery matters check.sent.isMailConfigured()— gate flows that depend on an email arriving (password reset, invoices). A placeholder key and a production sandbox sender (@resend.dev) count as unconfigured: both look configured and deliver to nobody.mailHealth()— key-validity + domain-status probe for/api/health. A passing probe does not prove delivery; only a real send does.options.idempotencyKey— pass from crons/queues so a retried job cannot double-send (Resend dedupes for 24h).
Resend or the app's own mail server
Resend is an HTTP email service: one API key, the fleet's shared verified domain. SMTP is the standard protocol for handing mail to a mail server, so an app that already sends through its own server keeps doing exactly that.
Resend stays the default. SMTP is chosen explicitly, never guessed from which variables happen to be set:
MAIL_TRANSPORT=smtp
SMTP_HOST=mail.example.ch
SMTP_PORT=587 # default 587 (STARTTLS); 465 means implicit TLS
SMTP_SECURE= # optional; defaults to true on 465, false otherwise
SMTP_USER=app@example.ch
SMTP_PASS=… # USER and PASS together, or neither
SMTP_FROM="App <app@example.ch>"
pnpm add nodemailer # only apps that choose SMTP
An app whose settings already live under other names passes them instead of renaming its env:
await sendMail(message, {
transport: "smtp",
smtp: { host: process.env.EMAIL_HOST!, port: Number(process.env.EMAIL_PORT), user: process.env.EMAIL_USER, pass: process.env.EMAIL_PASS },
});
The call, isMailConfigured, mailHealth and the result shape are the same
for both. What differs, honestly:
- Sender: SMTP reads
SMTP_FROM, notRESEND_FROM— the shared Resend domain is not your mail server's to send as.conventionalFrom()is a Resend convention. - Result:
{ sent: true, id }carries the SMTP Message-ID. A server that accepts no recipient issent: false. A 4xx reply or a dropped connection isretryable: true; a 5xx reply or a refused login is not.statusis an HTTP status, so it is absent over SMTP; the reply code is inerror. idempotencyKeyhas no SMTP equivalent and is ignored there.mailHealth()logs in without sending (no domain list).- Accepted is not delivered. A relay can answer
250and drop mail from a sender domain it has not authenticated. Check the sender domain's SPF/DKIM. - nodemailer missing →
{ sent: false, error: "SMTP transport needs the nodemailer package…" }. The import carrieswebpackIgnore/turbopackIgnore, so bundlers leave it to Node at send time instead of failing the build of a Resend-only app.
One layout for every email: renderMail
An app supplies a brand and content; the layout, dark mode, Outlook button, plain-text twin and footer rules live here, once.
import { renderMail, sendMail, listUnsubscribeHeaders, signUnsubscribe } from "@bitbaum/mail-kit";
const brand = { name: "Heidi", url: "https://heidi.orangecat.ch", accent: "#d9232d", onAccent: "#ffffff", accentDark: "#ff5a62" };
const { html, text } = renderMail(brand, {
lang: "de-CH",
preheader: "Kurz bevor Sie es vergessen würden: 8 Fragen, etwa 3 Minuten.",
heading: "Heute sind **8 Fragen** fällig",
blocks: [
{ type: "paragraph", text: "Drei Minuten jetzt, und sie bleiben ein paar Tage länger." },
{ type: "stats", items: [{ value: "8", label: "fällig" }, { value: "3", label: "Tage in Folge" }] },
{ type: "button", label: "Jetzt üben", url: "https://heidi.orangecat.ch/practice" },
],
footer: {
reason: "Sie bekommen das, weil Sie Erinnerungen eingeschaltet haben.",
unsubscribe: { label: "Nicht mehr erinnern", url: unsubscribeUrl },
preferences: { label: "Einstellungen", url: settingsUrl },
},
});
await sendMail({ to, subject, html, text, headers: listUnsubscribeHeaders(unsubscribeUrl) });
- Content is plain text. Every string is escaped; the only markup is
**bold**. A link that is nothttp(s)/mailtothrows. - Blocks:
paragraph,button,link,code,stats,list,quote,note. - Dark mode follows the reader's client (
prefers-color-scheme); giveaccentDarkif your accent is too dark to read on black. - The footer is a required key.
reasonis always said.unsubscribeis{label, url}ornull, andnullis only for mail the person asked for this minute (a sign-in link). - System fonts, deliberately: webfonts fail in most mail clients.
- Logo:
brand.logoUrlmust be a PNG (≥2× its shown 28px height); SVG does not render in Gmail or Outlook.
Unsubscribe that people do not hate
signUnsubscribe(secret, {user, list}) / verifyUnsubscribe make a signed, non-expiring token for a link that works without signing in. listUnsubscribeHeaders(url) adds List-Unsubscribe + List-Unsubscribe-Post: List-Unsubscribe=One-Click (RFC 8058), which Gmail and Yahoo require for bulk senders. The URL must act on a POST without a page, a login or a confirmation.
One address per app: hello@<host>
Every app has one address, on its own host, that a customer can guess:
hello@<app>.orangecat.ch on the box, and hello@murphys-bar.ch once a site
has its own domain. appAddress(url) builds it; appAddressFromEnv() reads MAIL_ADDRESS
or derives it from APP_URL / NEXT_PUBLIC_SITE_URL / AUTH_URL.
sendMail puts that address in Reply-To on every message (unless the call
sets its own, or already sends from it). Mail still leaves through the shared
sender until each host can sign its own — but every answer reaches the app.
Mail to an app
The fleet's mail gateway receives for every app host, checks SPF/DKIM/DMARC,
and POSTs an InboundMail to /api/mail/inbound, signed with
MAIL_INBOUND_SECRET (x-mail-signature: t=…,v1=hmac). The app side is one
line:
// app/api/mail/inbound/route.ts
import { inboundHandler } from "@bitbaum/mail-kit";
export const POST = inboundHandler({ onMail: (mail) => saveAndTriage(mail) });
Unsigned, tampered, stale (> 5 min) or oversized deliveries are refused before
your code runs; a thrown onMail answers 500 so the gateway retries.
AI triage
triageMail(mail, complete, context) asks a model (any complete() — the
ai-forms / ai-kit shape) for kind, urgency, a one-line summary, the
sender's language and needsHuman. It never throws: no model, or nonsense
back, and the answer comes from plain rules, marked by: "rules". Instructions
inside an email are described, never followed.
Env contract (the whole of it)
| Var | Meaning |
|---|---|
MAIL_TRANSPORT |
resend (default) or smtp |
RESEND_API_KEY |
required to send through Resend (a fleet key: /opt/fleet/env) |
RESEND_FROM |
sender — Name <addr> or bare address (optional if every call passes from) |
SMTP_HOST … |
SMTP_HOST, SMTP_PORT, SMTP_SECURE, SMTP_USER, SMTP_PASS, SMTP_FROM — only with MAIL_TRANSPORT=smtp |
MAIL_ADDRESS |
the app's own address; optional, derived from the app's URL |
MAIL_INBOUND_SECRET |
verifies mail from the gateway (a fleet key: /opt/fleet/env) |
Fleet conventions
- The shared Resend account is on the free tier: 1 verified domain,
100 emails/day, 3,000/month. Hitting the daily cap surfaces as
{ sent: false, status: 429, retryable: true }. - Only
loki.orangecat.chis verified, so every app sends as<app>@fleetcrown.orangecat.ch—conventionalFrom("My App")builds it. - A daily canary on the box sends one real email and alerts Telegram when it fails — that, not this package, is what proves delivery keeps working.
What this is not
- Not a template engine — apps own their HTML.
- Not a queue —
retryabletells a caller whether a retry can help; scheduling one is the caller's decision. - Not for bulk — newsletters go through Listmonk.
Part of bitbaum — AI-native products on open infrastructure, built in Zürich. Every package here lists the apps that use it: which apps use mail-kit.