Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,24 @@ 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")
```

</TabItem> </Tabs>

### 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

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 9 additions & 3 deletions src/content/docs/email-service/get-started/route-emails.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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<void> {
Expand All @@ -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",
Expand Down Expand Up @@ -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:
Expand Down