Skip to content

[Email Worker] Update type #22204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 7, 2025
Merged
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
82 changes: 54 additions & 28 deletions src/content/docs/email-routing/email-workers/runtime-api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,6 @@ sidebar:

An `EmailEvent` is the event type to programmatically process your emails with a Worker. You can reject, forward, or drop emails according to the logic you construct in your Worker.

***

## Syntax: Service Worker

`EmailEvent` can be handled in Workers functions written using the Service Worker syntax by attaching to the `email` event with `addEventListener`:

```js
addEventListener("email", async (event) => {
await event.message.forward("<YOUR_EMAIL>");
});
```

### Properties



* `event.message` EmailMessage

* An [`EmailMessage` object](#emailmessage-definition).



***

## Syntax: ES modules
Expand All @@ -50,9 +28,9 @@ export default {



* `message` EmailMessage
* `message` ForwardableEmailMessage

* An [`EmailMessage` object](#emailmessage-definition).
* A [`ForwardableEmailMessage` object](#forwardableemailmessage-definition).

* `env` object

Expand All @@ -65,10 +43,38 @@ export default {

***

## `EmailMessage` definition
## Syntax: Service Worker

:::caution[Service Workers are deprecated]

Service Workers are deprecated but still supported. We recommend using [Module Workers](/workers/reference/migrate-to-module-workers/) instead. New features may not be supported for Service Workers.

:::

`EmailEvent` can be handled in Workers functions written using the Service Worker syntax by attaching to the `email` event with `addEventListener`:

```js
addEventListener("email", async (event) => {
await event.message.forward("<YOUR_EMAIL>");
});
```

### Properties



* `event.message` ForwardableEmailMessage

* An [`ForwardableEmailMessage` object](#forwardableemailmessage-definition).



***

## `ForwardableEmailMessage` definition

```ts
interface EmailMessage<Body = unknown> {
interface ForwardableEmailMessage<Body = unknown> {
readonly from: string;
readonly to: string;
readonly headers: Headers;
Expand All @@ -83,7 +89,7 @@ export default {
}
```


An email message that is sent to a consumer Worker and can be rejected/forwarded.

* `from` string

Expand Down Expand Up @@ -114,7 +120,27 @@ export default {
* Forward this email message to a verified destination address of the account. If you want, you can add extra headers to the email message. Only `X-*` headers are allowed.
* When the promise resolves, the message is confirmed to be forwarded to a verified destination address.

* <code>reply(messageEmailMessage)</code> : Promise
* <code>reply(EmailMessage)</code> : Promise

* Reply to the sender of this email message with a new EmailMessage object.
* When the promise resolves, the message is confirmed to be replied.


## `EmailMessage` definition

```ts
interface EmailMessage {
readonly from: string;
readonly to: string;
}
```

An email message that can be sent from a Worker.

* `from` string

* `Envelope From` attribute of the email message.

* `to` string

* `Envelope To` attribute of the email message.