Repository navigation
Phase 1: staging mail on Cloudflare Email Service - #1902
Merged
neuromechanist merged 11 commits intoOct 24, 2026
Merged
neuromechanist merged 11 commits into
neuromechanist merged 11 commits into
Conversation
Add EMAIL_PROVIDER=cloudflare and a send_email binding path in sendEmail behind the unchanged dev fence. Resend stays the default. Cloudflare error codes map to EmailRejectedError or a plain Error. Tested with a binding recorder and Miniflare's send_email binding.
With EMAIL_PROVIDER=cloudflare, sendBroadcast sends one message per allowed recipient through sendEmail; the per-recipient fence and result accounting are unchanged and the Resend batch path stays for production. Tests use the shared binding recorder.
Add the Worker email() handler. A message routed to the Worker is stored raw at inbound/raw/YYYY/MM/DD/<uuid>.eml with envelope metadata; with no MAIL_INBOUND binding it is rejected. Adds the EMAIL, EMAIL_PROVIDER and MAIL_INBOUND binding types and the ForwardableEmailMessage lint global. Tested against Miniflare R2 and the bundled Worker.
Add the dev send_email binding restricted to the staging senders, the nemar-mail-dev inbound bucket, and EMAIL_PROVIDER, FROM_EMAIL and REPLY_TO for test.nemar.org. Only [env.dev] changes; production stays on Resend.
Record the move to the Cloudflare Email Service as a proposed ADR, add the epic working notes, and link them from the context index.
📦 Package PreviewYour package is ready to test! bun install -g nemar-cli@PR1902Version: 🤖 Updated on each push. |
An unknown provider value now throws EmailProviderConfigError instead of using Resend, and that error counts as a certain non-delivery. The resolver returns the provider with its binding, used by sendEmail and broadcast.
On the Cloudflare provider each per-recipient send in sendBroadcast is bounded, so a hung binding fails that recipient instead of holding the admin request.
Clamp the three header-derived metadata values by UTF-8 bytes without splitting a character, and retry a failed put once without metadata so the message is kept. The body stays buffered because the retry needs the bytes again.
Derive a plain-text alternative from the HTML with a small dependency-free converter; codes and links survive. The Resend request is unchanged.
Match the strict EMAIL_PROVIDER rule and the text part, place the index entry after ADR 0109, and correct the Wrangler pin note.
Decode each link label, url and text exactly once and never re-strip decoded text, look entities up in a Map, read unquoted href, cap the input at 256 KiB, and scan blocks and links forward once so unclosed tags cost linear time.
neuromechanist
merged commit Oct 24, 2026
c039eec
into
feature/issue-1893-epic-email-cf
14 checks passed
This was referenced Oct 24, 2026
Merged
This branch was previously deployed
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.
Summary
Staging (
nemar-api-dev) sends its mail through the Cloudflare Email Servicesend_emailbinding fromtest.nemar.org, and receives mail for routedtest.nemar.orgaddresses into R2. Production is unchanged: it sets noEMAIL_PROVIDERand holds noMAIL_INBOUNDbucket.email.ts:resolveEmailProviderselects Cloudflare only whenEMAIL_PROVIDER=cloudflare; a missing binding throwsEmailProviderConfigErrorand never falls back to Resend. The dev fence still runs before either provider. Definite Cloudflare non-delivery codes map toEmailRejectedError(429 for the two limit codes, 422 otherwise);E_INTERNAL_SERVER_ERROR, unknown codes and code-less errors stay plain errors (may have been accepted).timeoutMsis honoured by a race because the binding takes no abort signal.broadcast.ts: on the Cloudflare path each allowed recipient is sent through the single-send path, with the existing fence and accounting.inbound-mail.tsand theemail()export: the raw message goes toMAIL_INBOUNDatinbound/raw/YYYY/MM/DD/<uuid>.emlwith envelope metadata; with no bucket the message is rejected.wrangler-sccn.toml[env.dev]only: theEMAILbinding restricted tonoreply@/[email protected],EMAIL_PROVIDER,FROM_EMAIL,REPLY_TO, and thenemar-mail-devbucket..context/epic_email_cloudflare.md.The dev
RESEND_API_KEYsecret stays until the final phase, because several senders check for it before callingsendEmail.Closes #1894
Part of epic #1893
Test plan
bun run typecheckandbun run lintclean;biome checkon the changed backend files clean[DEV]wrap, a workerd-bundled send through Miniflare'ssend_emailbinding (including a refused sender), broadcasts, and theemail()handler through Miniflare with a real R2 bucketwrangler deploy --dry-run --env devshowsenv.EMAILandenv.MAIL_INBOUNDdev: a staging sign-in code arrives from[email protected]with SPF and DKIM passing; mail to[email protected]lands innemar-mail-devThe root suite's only failure,
test/account-copy-parity.test.ts, compares against a local website checkout and is unrelated to this diff.🤖 Generated with Claude Code