From bd560bf9725602af9502e55f42a1d0b97de8e4ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Pereira?= Date: Mon, 30 Nov 2026 12:25:59 +0000 Subject: [PATCH] [Email Service] Fix Python email handler, raw MIME thread replies, and Worker routing quickstart Fixes #35755 Fixes #35756 Fixes #35765 --- .../api/route-emails/email-handler.mdx | 19 ++++++++++++------- .../examples/email-routing/email-storage.mdx | 4 ++-- .../get-started/route-emails.mdx | 12 +++++++++--- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/content/docs/email-service/api/route-emails/email-handler.mdx b/src/content/docs/email-service/api/route-emails/email-handler.mdx index 599bfa45c70..27caf2a76c3 100644 --- a/src/content/docs/email-service/api/route-emails/email-handler.mdx +++ b/src/content/docs/email-service/api/route-emails/email-handler.mdx @@ -36,7 +36,8 @@ export default { from workers import WorkerEntrypoint class Default(WorkerEntrypoint): - async def email(self, message, env, ctx): + async def email(self, message): + # Access bindings and execution context through self.env and self.ctx await message.forward("destination@example.com") ``` @@ -44,11 +45,15 @@ class Default(WorkerEntrypoint): ### Parameters -| Parameter | Type | Description | -| --------- | ------------------------- | --------------------------------------------- | -| `message` | `ForwardableEmailMessage` | The incoming email message | -| `env` | `object` | Worker environment bindings (KV, EMAIL, etc.) | -| `ctx` | `object` | Execution context with `waitUntil` function | +ES module handlers receive `message`, `env`, and `ctx` as arguments. `WorkerEntrypoint` subclasses — including Python `WorkerEntrypoint` classes and JavaScript or TypeScript class entrypoints — receive only `message`. Access bindings and execution context through `self.env` and `self.ctx` in Python, or `this.env` and `this.ctx` in JavaScript and TypeScript. + +The `email()` handler uses the following values: + +| Parameter | Type | Description | +| --------- | ------------------------- | --------------------------------------------------------------------- | +| `message` | `ForwardableEmailMessage` | The incoming email message | +| `env` | `object` | Worker environment bindings, such as KV namespaces and email bindings | +| `ctx` | `object` | Execution context with `waitUntil()` | ## `ForwardableEmailMessage` interface @@ -355,8 +360,8 @@ export default { const reply = createMimeMessage(); if (messageId) { + // Email Service populates References automatically to preserve the full thread chain reply.setHeader("In-Reply-To", messageId); - reply.setHeader("References", messageId); } reply.setSender(message.to); reply.setRecipient(message.from); diff --git a/src/content/docs/email-service/examples/email-routing/email-storage.mdx b/src/content/docs/email-service/examples/email-routing/email-storage.mdx index 47c1df1df4c..156af263680 100644 --- a/src/content/docs/email-service/examples/email-routing/email-storage.mdx +++ b/src/content/docs/email-service/examples/email-routing/email-storage.mdx @@ -81,8 +81,8 @@ async function handleSupportEmail(message, env, emailId) { const messageId = message.headers.get("Message-ID"); const reply = createMimeMessage(); if (messageId) { + // Email Service populates References automatically to preserve the full thread chain reply.setHeader("In-Reply-To", messageId); - reply.setHeader("References", messageId); } reply.setSender(message.to); reply.setRecipient(message.from); @@ -181,8 +181,8 @@ export default { const messageId = message.headers.get("Message-ID"); const reply = createMimeMessage(); if (messageId) { + // Email Service populates References automatically to preserve the full thread chain reply.setHeader("In-Reply-To", messageId); - reply.setHeader("References", messageId); } reply.setSender(message.to); reply.setRecipient(message.from); diff --git a/src/content/docs/email-service/get-started/route-emails.mdx b/src/content/docs/email-service/get-started/route-emails.mdx index 95f9e6461f8..65d33499645 100644 --- a/src/content/docs/email-service/get-started/route-emails.mdx +++ b/src/content/docs/email-service/get-started/route-emails.mdx @@ -84,6 +84,10 @@ Verify that your routing rule is working: Use Workers to process emails with custom logic before forwarding or responding. +### Prerequisites + +Because this Worker calls `message.forward()`, the forwarding address must be a verified destination address on your Cloudflare account. Before you deploy or test the Worker, add and verify the destination address in the Cloudflare dashboard. To add and verify an address, refer to [Add a destination address](/email-service/configuration/destination-addresses/#add-a-destination-address). + ### Create an email processing Worker 1. Create a new Worker project: @@ -126,7 +130,7 @@ Use Workers to process emails with custom logic before forwarding or responding. // Configuration - Update these values // ============================================ const YOUR_DOMAIN = "yourdomain.com"; // Replace with your verified domain - const FORWARD_TO_EMAIL = "your-team@example.com"; // Replace with where you want emails forwarded + const FORWARD_TO_EMAIL = "your-team@example.com"; // Replace with a verified destination address on your account export default { async email(message, env, ctx): Promise { @@ -144,8 +148,8 @@ Use Workers to process emails with custom logic before forwarding or responding. const msg = createMimeMessage(); const messageId = message.headers.get("Message-ID"); if (messageId) { + // Email Service populates References automatically to preserve the full thread chain msg.setHeader("In-Reply-To", messageId); - msg.setHeader("References", messageId); } msg.setSender({ name: "Support Team", @@ -187,7 +191,9 @@ Use Workers to process emails with custom logic before forwarding or responding. :::caution[Update configuration] Before deploying, update the constants at the top of the file: - `YOUR_DOMAIN`: Your verified domain from the Cloudflare dashboard - - `FORWARD_TO_EMAIL`: The email address where you want to receive forwarded emails + - `FORWARD_TO_EMAIL`: A verified destination address on your Cloudflare account + + Calls to `message.forward()` fail if `FORWARD_TO_EMAIL` is not verified on your account. To add and verify an address, refer to [Add a destination address](/email-service/configuration/destination-addresses/#add-a-destination-address). ::: 5. Deploy your Worker: