Skip to content

Email on Cloudflare, phase 1: staging mail - #1903

Merged
neuromechanist merged 3 commits into
devfrom
feature/issue-1893-epic-email-cf
Oct 24, 2026
Merged

neuromechanist merged 3 commits into
devfrom
feature/issue-1893-epic-email-cf

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Summary

Phase 1 of the email epic reaches dev, so the staging Worker deploys with it. The epic continues on its branch afterwards; this PR does not close #1893.

  • Staging (nemar-api-dev) sends through the Cloudflare Email Service send_email binding from test.nemar.org instead of Resend (EMAIL_PROVIDER=cloudflare), with a plain-text part, behind the unchanged non-production delivery fence. Production sets nothing and stays on Resend.
  • An email() handler stores inbound mail for routed test.nemar.org addresses in the private nemar-mail-dev bucket; a Worker without that bucket rejects mail.
  • ADR 0110 records the move, the provider rule, and the soak fallback (only for a Cloudflare refusal that certainly sent nothing).

Phase PR: #1902 (two review rounds, all findings fixed). Part of #1893; #1894 closes after the staging checks below.

Prerequisites (done)

  • Email Sending enabled for test.nemar.org
  • Email Routing enabled, with test.nemar.org as a routing subdomain
  • nemar-mail-dev bucket created, private, 30-day expiry

Test plan

  • Phase CI green on Phase 1: staging mail on Cloudflare Email Service #1902 (unit, integration-dev, api-test, lint)
  • Backend suite 8338 pass, 0 fail
  • Dev deploy succeeds with the new send_email binding
  • Routing rules on test.nemar.org to nemar-api-dev (operator, after deploy)
  • A staging sign-in code arrives from [email protected] with SPF and DKIM passing
  • Mail to support@ and [email protected] lands in nemar-mail-dev; an unrouted address bounces
  • No new staging sends in Resend

🤖 Generated with Claude Code

* feat(email): Cloudflare provider behind a flag

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.

* feat(email): Cloudflare path for broadcasts

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.

* feat(email): store inbound mail in R2

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.

* chore(staging): wire Cloudflare mail for dev

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.

* docs: ADR 0110 and email epic notes

Record the move to the Cloudflare Email Service as a proposed ADR, add the epic working notes, and link them from the context index.

* fix(email): refuse an unknown EMAIL_PROVIDER

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.

* fix(email): bound each broadcast send

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.

* fix(email): byte-clamp inbound metadata, retry put

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.

* feat(email): add a text part on the Cloudflare path

Derive a plain-text alternative from the HTML with a small dependency-free converter; codes and links survive. The Resend request is unchanged.

* docs: align ADR 0110 and epic notes

Match the strict EMAIL_PROVIDER rule and the text part, place the index entry after ADR 0109, and correct the Wrangler pin note.

* fix(email): harden the HTML to text converter

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.
The apex MX moved to Email Routing on 2026-10-23 with the catch-all forwarding as before, and the soak fallback is limited to refusals that certainly sent nothing. Epic phase table matches the issues.
@github-actions

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR1903

Version: 0.10.24-PR1903.c4cc926
Commit: c4cc926


🤖 Updated on each push.

@neuromechanist
neuromechanist merged commit 2391379 into dev Oct 24, 2026
27 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-1893-epic-email-cf branch October 24, 2026 03:04

This branch was previously deployed

1 inactive deployment
npm — c4cc9260 Deployed Oct 24, 2026 by neuromechanist via publish #3814
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