Process Cloudflare Email Routing messages in a Worker
Route a dedicated address into an email handler, parse the single-use MIME stream, and persist work before declaring processing successful.
Reviewed
Email Routing can deliver mail to a Worker’s email(message, env, ctx) handler or a verified forwarding destination. This is separate from the outbound send_email binding and from an HTTP webhook receiving parsed JSON.
Configure receiving deliberately
- Select a dedicated receiving domain/subdomain/address and configure the required Routing DNS/rule.
- Point the rule to the intended Worker and restrict routing behavior to the application workflow.
- If forwarding, verify the destination through Cloudflare first; do not assume arbitrary destinations are authorized.
- Install a maintained MIME parser such as
postal-mimewhen the application needs parsed content.
Within the email handler, buffer/parse the raw stream once. This excerpt preserves bytes for a separately designed durable-storage path:
import PostalMime from "postal-mime";
const raw = await new Response(message.raw).arrayBuffer();
const parsed = await PostalMime.parse(raw);
// Persist authorized work before completing the handler; do not log mail bodies.
The excerpt is not a complete queue/storage implementation. Check message size before buffering and apply attachment/content limits. Do not read message.raw twice and expect the same stream again.
Verify routing and failures
Send a controlled message to the exact original recipient and verify the matched rule, handler execution, and durable outcome. Exercise processing failures and duplicate handling without exposing private bodies in Worker logs.
Use Cloudflare’s email-handler reference for forward/reply/reject semantics. If posting onward to your own HTTP service, design that authentication yourself; the request does not become a MailKite-signed event automatically.