Skip to content

feat(selfhost): an SMTP sender behind the EMAIL binding (#291) - #335

Merged
Madraka merged 1 commit into
mainfrom
claude/selfhost-smtp-291
Nov 29, 2026
Merged

Madraka merged 1 commit into
mainfrom
claude/selfhost-smtp-291

Conversation

@Madraka

@Madraka Madraka commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

The self-host image now mails invites and password resets (RFC-0047 §6). Set CLOUDHAL_SMTP_URL and CLOUDHAL_MAIL_FROM, and the admin Worker's EMAIL binding reaches an SMTP sender Worker in the image. It is the same binding and the same send() call as on Cloudflare, so no caller changes. Without CLOUDHAL_SMTP_URL there is no EMAIL binding and mail stays off, as before.

Design

  • smtp Worker (smtp-service.ts). Its default entrypoint is a WorkerEntrypoint with send(message), reached by JS RPC over (name = "EMAIL", service = "smtp").
    • The admin Worker also gets CLOUDHAL_MAIL_FROM and CLOUDHAL_MAIL_FROM_NAME.
    • The URL, which may carry credentials, reaches the Worker only through fromEnvironment (RFC-0047 §2, invariant 3). It is never in the config text or the logs.
  • Its own network. globalOutbound = "smtp-network" allows public and private addresses (a LAN relay, Mailpit) with trustBrowserCas plus CLOUDHAL_SMTP_CA_FILE's certificates. No other Worker gets this network.
  • smtp-client.ts holds the protocol and has no imports.
    • The config embeds it as a Worker module: the compiled .js in the image, or stripTypeScriptTypes of the .ts from a checkout. That is the same stripper build-packages.ts uses.
    • It is unit-tested against a scripted server (test/selfhost-smtp.test.ts).

The rules from the issue

  • STARTTLS or implicit TLS only.
    • smtp:// is STARTTLS. A server that doesn't offer it fails with E_SMTP_TLS_UNAVAILABLE right after the first EHLO, before AUTH or anything else.
    • smtps:// is TLS from the first byte. There is no plain mode, and any other scheme is refused at start.
    • AUTH PLAIN (or LOGIN) is sent only after TLS.
  • Bounded retries.
    • Each reply has 10 s and each attempt 20 s.
    • A connection error, a timeout or a 4xx reply is retried after 1 s and 3 s, so three attempts at most.
    • A 5xx reply (E_SMTP_AUTH, E_SMTP_REJECTED) is final.
  • Logs. One line per attempt: the recipient's domain only (…@example.org), the server, the outcome and the first line of a refusal. Never the body, its links or the credentials. Start-up errors about the URL never echo it.
  • Hardening.
    • Only CLOUDHAL_MAIL_FROM may send, mirroring Cloudflare's allowed_sender_addresses.
    • Addresses are checked for line breaks and brackets.
    • Headers are RFC 2047-encoded and bodies base64, with dot-stuffing.

Recipes

  • Compose mail profile.
    • mailpit (pinned by digest) requires STARTTLS.
    • A mail-tls one-shot (alpine/openssl, pinned) makes its certificate once.
    • Cloudhal mounts that certificate read-only at /run/cloudhal-mail. The volume is empty without the profile.
    • .env.example shows the three lines to set.
  • Helm. mail.smtpUrlSecret (the URL from a Secret of your own), mail.from (required with it, via required), mail.fromName, and an optional mail.caConfigMap. With defaults the chart renders as before, so the kustomize base is unchanged.
  • Swarm. The README shows the URL as a Docker secret with CLOUDHAL_SMTP_URL_FILE.

CI

  • scripts/mail-smoke.ts is a new docker.yml step and, like the failover test, refuses to run outside Actions.
    • Mailpit runs with a certificate made on the runner and requires both STARTTLS and SMTP AUTH (MP_SMTP_AUTH), so an arrival proves encryption and authentication.
    • Studio must report invites.delivery containing email.
    • An invite with delivery: "email" must arrive with its /invite#chinv_ link, and a password reset asked for by email must arrive too.
    • The log must name both sends and hold neither the SMTP password nor chinv_.
  • The compose check also validates --profile mail.
  • The chart lint renders the mail values, and fails if mail.from is missing.

Docs

  • Self-host guide: a new Mail section, Environment rows, and the Limits section no longer says there is no mail.
  • Also updated: RFC-0047 §6's binding map, deploy/docker/README.md, deploy/swarm/README.md, deploy/helm/cloudhal/README.md and AGENTS.md.
  • Changeset: @cloudhal/cli minor.

Checked locally

  • biome check --diagnostic-level=error and pnpm size:check.
  • tsc of @cloudhal/cli and of the smoke script.
  • actionlint.
  • vitest run test/selfhost-smtp.test.ts test/selfhost.test.ts: 23 passed.

There were no Docker, compose or helm runs locally. This PR's docker.yml run is the first time the Worker meets workerd: the RPC entrypoint, connect() through smtp-network and startTls().

Closes #291

🤖 Generated with Claude Code

With CLOUDHAL_SMTP_URL (or _FILE) and CLOUDHAL_MAIL_FROM, the generated
workerd config gets an smtp Worker whose default entrypoint has the
send_email binding's send(): the admin Worker's EMAIL binding points at it,
so invites and password resets are mailed with no caller change. Unset,
there is no EMAIL binding and mail stays off.

- smtp:// is STARTTLS and a server without it is refused before anything
  else is sent; smtps:// is TLS from the first byte; no plain mode. AUTH
  PLAIN or LOGIN only after TLS. Certificates: browser CAs plus
  CLOUDHAL_SMTP_CA_FILE.
- Each reply has 10 s, each attempt 20 s; connection errors, timeouts and
  4xx are retried after 1 s and 3 s, 5xx is final.
- One log line per attempt (recipient domain, server, outcome); never the
  body, its links or the credentials. The URL reaches the Worker only via
  fromEnvironment and is never printed.
- Only CLOUDHAL_MAIL_FROM may send; addresses are checked for line breaks.
- The Worker's sockets go through its own smtp-network (public and private).

smtp-client.ts has no imports: the config embeds it as a Worker module
(compiled .js in the image, type-stripped .ts from a checkout), and its
unit tests drive it against a scripted server.

Recipes: compose's mail profile (Mailpit requiring STARTTLS, a mail-tls
one-shot that makes its certificate), mail.* Helm values, Swarm docs.
CI: scripts/mail-smoke.ts sends an invite and a password reset through
Mailpit with STARTTLS and AUTH required and checks the log; the compose
check covers the mail profile, the chart check its mail values.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Madraka <8898640+Madraka@users.noreply.github.com>
@Madraka
Madraka merged commit 3d0ae25 into main Nov 29, 2026
12 checks passed
@Madraka
Madraka deleted the claude/selfhost-smtp-291 branch November 29, 2026 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Self-host: an SMTP sender behind the EMAIL binding (CLOUDHAL_SMTP_URL)

1 participant