Skip to content

Commit bd560bf

Browse files
committed
[Email Service] Fix Python email handler, raw MIME thread replies, and Worker routing quickstart
Fixes #35755 Fixes #35756 Fixes #35765
1 parent 966ec55 commit bd560bf

3 files changed

Lines changed: 23 additions & 12 deletions

File tree

‎src/content/docs/email-service/api/route-emails/email-handler.mdx‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,24 @@ export default {
3636
from workers import WorkerEntrypoint
3737

3838
class Default(WorkerEntrypoint):
39-
async def email(self, message, env, ctx):
39+
async def email(self, message):
40+
# Access bindings and execution context through self.env and self.ctx
4041
await message.forward("destination@example.com")
4142
```
4243

4344
</TabItem> </Tabs>
4445

4546
### Parameters
4647

47-
| Parameter | Type | Description |
48-
| --------- | ------------------------- | --------------------------------------------- |
49-
| `message` | `ForwardableEmailMessage` | The incoming email message |
50-
| `env` | `object` | Worker environment bindings (KV, EMAIL, etc.) |
51-
| `ctx` | `object` | Execution context with `waitUntil` function |
48+
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.
49+
50+
The `email()` handler uses the following values:
51+
52+
| Parameter | Type | Description |
53+
| --------- | ------------------------- | --------------------------------------------------------------------- |
54+
| `message` | `ForwardableEmailMessage` | The incoming email message |
55+
| `env` | `object` | Worker environment bindings, such as KV namespaces and email bindings |
56+
| `ctx` | `object` | Execution context with `waitUntil()` |
5257

5358
## `ForwardableEmailMessage` interface
5459

@@ -355,8 +360,8 @@ export default {
355360

356361
const reply = createMimeMessage();
357362
if (messageId) {
363+
// Email Service populates References automatically to preserve the full thread chain
358364
reply.setHeader("In-Reply-To", messageId);
359-
reply.setHeader("References", messageId);
360365
}
361366
reply.setSender(message.to);
362367
reply.setRecipient(message.from);

‎src/content/docs/email-service/examples/email-routing/email-storage.mdx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ async function handleSupportEmail(message, env, emailId) {
8181
const messageId = message.headers.get("Message-ID");
8282
const reply = createMimeMessage();
8383
if (messageId) {
84+
// Email Service populates References automatically to preserve the full thread chain
8485
reply.setHeader("In-Reply-To", messageId);
85-
reply.setHeader("References", messageId);
8686
}
8787
reply.setSender(message.to);
8888
reply.setRecipient(message.from);
@@ -181,8 +181,8 @@ export default {
181181
const messageId = message.headers.get("Message-ID");
182182
const reply = createMimeMessage();
183183
if (messageId) {
184+
// Email Service populates References automatically to preserve the full thread chain
184185
reply.setHeader("In-Reply-To", messageId);
185-
reply.setHeader("References", messageId);
186186
}
187187
reply.setSender(message.to);
188188
reply.setRecipient(message.from);

‎src/content/docs/email-service/get-started/route-emails.mdx‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ Verify that your routing rule is working:
8484

8585
Use Workers to process emails with custom logic before forwarding or responding.
8686

87+
### Prerequisites
88+
89+
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).
90+
8791
### Create an email processing Worker
8892

8993
1. Create a new Worker project:
@@ -126,7 +130,7 @@ Use Workers to process emails with custom logic before forwarding or responding.
126130
// Configuration - Update these values
127131
// ============================================
128132
const YOUR_DOMAIN = "yourdomain.com"; // Replace with your verified domain
129-
const FORWARD_TO_EMAIL = "your-team@example.com"; // Replace with where you want emails forwarded
133+
const FORWARD_TO_EMAIL = "your-team@example.com"; // Replace with a verified destination address on your account
130134

131135
export default {
132136
async email(message, env, ctx): Promise<void> {
@@ -144,8 +148,8 @@ Use Workers to process emails with custom logic before forwarding or responding.
144148
const msg = createMimeMessage();
145149
const messageId = message.headers.get("Message-ID");
146150
if (messageId) {
151+
// Email Service populates References automatically to preserve the full thread chain
147152
msg.setHeader("In-Reply-To", messageId);
148-
msg.setHeader("References", messageId);
149153
}
150154
msg.setSender({
151155
name: "Support Team",
@@ -187,7 +191,9 @@ Use Workers to process emails with custom logic before forwarding or responding.
187191
:::caution[Update configuration]
188192
Before deploying, update the constants at the top of the file:
189193
- `YOUR_DOMAIN`: Your verified domain from the Cloudflare dashboard
190-
- `FORWARD_TO_EMAIL`: The email address where you want to receive forwarded emails
194+
- `FORWARD_TO_EMAIL`: A verified destination address on your Cloudflare account
195+
196+
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).
191197
:::
192198

193199
5. Deploy your Worker:

0 commit comments

Comments
 (0)