Repository navigation
feat(selfhost): an SMTP sender behind the EMAIL binding (#291) - #335
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The self-host image now mails invites and password resets (RFC-0047 §6). Set
CLOUDHAL_SMTP_URLandCLOUDHAL_MAIL_FROM, and the admin Worker'sEMAILbinding reaches an SMTP sender Worker in the image. It is the same binding and the samesend()call as on Cloudflare, so no caller changes. WithoutCLOUDHAL_SMTP_URLthere is noEMAILbinding and mail stays off, as before.Design
smtpWorker (smtp-service.ts). Its default entrypoint is aWorkerEntrypointwithsend(message), reached by JS RPC over(name = "EMAIL", service = "smtp").CLOUDHAL_MAIL_FROMandCLOUDHAL_MAIL_FROM_NAME.fromEnvironment(RFC-0047 §2, invariant 3). It is never in the config text or the logs.globalOutbound = "smtp-network"allows public and private addresses (a LAN relay, Mailpit) withtrustBrowserCasplusCLOUDHAL_SMTP_CA_FILE's certificates. No other Worker gets this network.smtp-client.tsholds the protocol and has no imports..jsin the image, orstripTypeScriptTypesof the.tsfrom a checkout. That is the same stripperbuild-packages.tsuses.test/selfhost-smtp.test.ts).The rules from the issue
smtp://is STARTTLS. A server that doesn't offer it fails withE_SMTP_TLS_UNAVAILABLEright after the firstEHLO, 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.E_SMTP_AUTH,E_SMTP_REJECTED) is final.…@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.CLOUDHAL_MAIL_FROMmay send, mirroring Cloudflare'sallowed_sender_addresses.Recipes
mailprofile.mailpit(pinned by digest) requires STARTTLS.mail-tlsone-shot (alpine/openssl, pinned) makes its certificate once./run/cloudhal-mail. The volume is empty without the profile..env.exampleshows the three lines to set.mail.smtpUrlSecret(the URL from a Secret of your own),mail.from(required with it, viarequired),mail.fromName, and an optionalmail.caConfigMap. With defaults the chart renders as before, so the kustomize base is unchanged.CLOUDHAL_SMTP_URL_FILE.CI
scripts/mail-smoke.tsis a new docker.yml step and, like the failover test, refuses to run outside Actions.MP_SMTP_AUTH), so an arrival proves encryption and authentication.invites.deliverycontainingemail.delivery: "email"must arrive with its/invite#chinv_link, and a password reset asked for by email must arrive too.chinv_.--profile mail.mail.fromis missing.Docs
deploy/docker/README.md,deploy/swarm/README.md,deploy/helm/cloudhal/README.mdand AGENTS.md.@cloudhal/climinor.Checked locally
biome check --diagnostic-level=errorandpnpm size:check.tscof@cloudhal/cliand 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()throughsmtp-networkandstartTls().Closes #291
🤖 Generated with Claude Code