Runtime API
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.
EmailEvent can be handled in Workers functions written using the ES modules format by adding an email function to your module's exported handlers:
export default { async email(message, env, ctx) { await message.forward("<YOUR_EMAIL>"); },};-
messageForwardableEmailMessage -
envobject- An object containing the bindings associated with your Worker using ES modules format, such as KV namespaces and Durable Objects.
-
ctxobject- An object containing the context associated with your Worker using ES modules format. Currently, this object just contains the
waitUntilfunction.
- An object containing the context associated with your Worker using ES modules format. Currently, this object just contains the
EmailEvent can be handled in Workers functions written using the Service Worker syntax by attaching to the email event with addEventListener:
addEventListener("email", async (event) => { await event.message.forward("<YOUR_EMAIL>");});-
event.messageForwardableEmailMessage
interface ForwardableEmailMessage<Body = unknown> { readonly from: string; readonly to: string; readonly headers: Headers; readonly raw: ReadableStream; readonly rawSize: number;
public constructor(from: string, to: string, raw: ReadableStream | string);
setReject(reason: string): void; forward(rcptTo: string, headers?: Headers): Promise<void>; reply(message: EmailMessage): Promise<void>;}An email message that is sent to a consumer Worker and can be rejected/forwarded.
-
fromstringEnvelope Fromattribute of the email message.
-
tostringEnvelope Toattribute of the email message.
-
headersHeaders -
rawReadableStream- Stream of the email message content.
-
rawSizenumber- Size of the email message content.
-
setReject(reasonstring): void- Reject this email message by returning a permanent SMTP error back to the connecting client, including the given reason.
-
forward(rcptTostring, headersHeadersoptional): Promise- 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.
- 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
-
reply(EmailMessage): 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.
interface EmailMessage { readonly from: string; readonly to: string;}An email message that can be sent from a Worker.
-
fromstringEnvelope Fromattribute of the email message.
-
tostringEnvelope Toattribute of the email message.
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Products
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark