Skip to content

Phase 1: staging mail on Cloudflare Email Service - #1902

Merged
neuromechanist merged 11 commits into
feature/issue-1893-epic-email-cffrom
feature/issue-1894-phase1-staging-mail
Oct 24, 2026
Merged

neuromechanist merged 11 commits into
feature/issue-1893-epic-email-cffrom
feature/issue-1894-phase1-staging-mail

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Summary

Staging (nemar-api-dev) sends its mail through the Cloudflare Email Service send_email binding from test.nemar.org, and receives mail for routed test.nemar.org addresses into R2. Production is unchanged: it sets no EMAIL_PROVIDER and holds no MAIL_INBOUND bucket.

  • email.ts: resolveEmailProvider selects Cloudflare only when EMAIL_PROVIDER=cloudflare; a missing binding throws EmailProviderConfigError and never falls back to Resend. The dev fence still runs before either provider. Definite Cloudflare non-delivery codes map to EmailRejectedError (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). timeoutMs is 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.ts and the email() export: the raw message goes to MAIL_INBOUND at inbound/raw/YYYY/MM/DD/<uuid>.eml with envelope metadata; with no bucket the message is rejected.
  • wrangler-sccn.toml [env.dev] only: the EMAIL binding restricted to noreply@/[email protected], EMAIL_PROVIDER, FROM_EMAIL, REPLY_TO, and the nemar-mail-dev bucket.
  • ADR 0110 and .context/epic_email_cloudflare.md.

The dev RESEND_API_KEY secret stays until the final phase, because several senders check for it before calling sendEmail.

Closes #1894
Part of epic #1893

Test plan

  • bun run typecheck and bun run lint clean; biome check on the changed backend files clean
  • Backend suite: 8319 pass, 0 fail
  • 46 new tests: provider selection, mailbox parsing, every error code, sends through real exported wrappers with the fence and [DEV] wrap, a workerd-bundled send through Miniflare's send_email binding (including a refused sender), broadcasts, and the email() handler through Miniflare with a real R2 bucket
  • wrangler deploy --dry-run --env dev shows env.EMAIL and env.MAIL_INBOUND
  • After the epic reaches dev: a staging sign-in code arrives from [email protected] with SPF and DKIM passing; mail to [email protected] lands in nemar-mail-dev

The 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

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.
@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR1902

Version: 0.10.23-PR1902.3ebd2ca
Commit: 3ebd2ca


🤖 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
neuromechanist merged commit c039eec into feature/issue-1893-epic-email-cf Oct 24, 2026
14 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-1894-phase1-staging-mail branch October 24, 2026 00:24

This branch was previously deployed

1 inactive deployment
npm — 3ebd2ca8 Deployed Oct 23, 2026 by neuromechanist via publish #3810
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.

1 participant