Skip to content

feat(driver): add Cloudflare Email Service (Email Sending) - #101

Open
jd-solanki wants to merge 1 commit into
productdevbook:mainfrom
jd-solanki:feat/cloudflare-email-service-driver
Open

feat(driver): add Cloudflare Email Service (Email Sending)#101
jd-solanki wants to merge 1 commit into
productdevbook:mainfrom
jd-solanki:feat/cloudflare-email-service-driver

Conversation

@jd-solanki

Copy link
Copy Markdown

Closes #100. Opened as a draft because one design question is still open — see Shape below.

What

Adds unemail/driver/cloudflare-email-service for Cloudflare's Email Service (Email Sending), alongside the existing cloudflare-email driver.

The two speak different APIs on the same send_email binding:

cloudflare-email (existing) cloudflare-email-service (new)
API Email Routing Email Service / Email Sending
Payload raw RFC 5322 via buildMime structured fields
Needs cloudflare:email yes (EmailMessage) no
Recipients per send 1 (normalizeAddresses(msg.to)[0]) many (up to 50 across to/cc/bcc)

Practical effects: no ambient global and no virtual-module import (so no "cloudflare:email" could not be resolved warning in Vite dev), no _smtp/mime in the built module, and display names come through as structured from: { email, name } rather than depending on a MIME header.

Binding errors arrive as Errors carrying an E_* code. Those are mapped onto the existing EmailErrorCode taxonomy, so retry middleware treats E_RATE_LIMIT_EXCEEDED as retryable and E_VALIDATION_ERROR / E_SENDER_NOT_VERIFIED as not. Unrecognised codes fall through to toEmailErrorPROVIDER, as before.

Shape

The issue offered either a separate driver or a mode flag on the existing one, and left the choice to you. This PR takes the separate-driver route because it's non-breaking — cloudflare-email is untouched for Email Routing users. Happy to reshape it into a mode flag on the existing driver instead if you'd prefer; that's the reason this is a draft.

Correction to the issue

The issue proposed omitting the attachments flag. That was wrong — I re-checked the current Workers binding docs and Email Service does accept structured attachments, so the driver supports them and maps contentTypetype, cidcontentId. The issue's sketch also listed cc/bcc in flags, which aren't keys of DriverFlags; the actual flags are html, text, attachments, customHeaders, replyTo.

Included

  • the driver
  • 9 tests (test/driver/cloudflare-email-service.test.ts)
  • docs/drivers.md matrix row + a section on the Routing-vs-Service distinction
  • package.json and jsr.json export entries

Verification

pnpm lint, pnpm typecheck, vitest run (281 tests, 56 files), pnpm build and pnpm bundle-budget all pass locally.

One caveat, stated plainly: this surface was validated against Cloudflare's current documentation and the unemail source, and is covered by unit tests against a mock binding — but I have not exercised the send path against a live Cloudflare binding. Worth a second pair of eyes on the payload field names before release.

Add unemail/driver/cloudflare-email-service for Cloudflare's Email Service
API. The existing cloudflare-email driver targets Email Routing: it builds
raw RFC 5322 and needs the Workers-only EmailMessage class from the virtual
cloudflare:email module. Email Service takes structured fields on the same
send_email binding, so the new driver needs no ambient global, no virtual
module and no MIME builder, and sends to multiple recipients in one call.

Binding E_* errors are mapped onto the EmailErrorCode taxonomy so retry
middleware sees rate limits as retryable and validation failures as not.

Includes tests, package + jsr exports, and a drivers.md matrix row.

Closes productdevbook#100

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@jd-solanki
jd-solanki marked this pull request as ready for review July 22, 2026 10:05
@jd-solanki

Copy link
Copy Markdown
Author

Hi @productdevbook

This is created by AI so I highly recommend you check out any issues or refactoring you may prefer.

If possible, can you make a new release after merging?

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.

feat(driver): add Cloudflare Email Service (Email Sending)

1 participant