Overview
Cloudflare Agents provides utilities for routing incoming emails to Agent instances with support for address-based routing, secure reply flows, and catch-all patterns.routeAgentEmail()
Route an email to the appropriate Agent.ForwardableEmailMessage
required
The email to route (from Email Workers)
Env
required
Environment containing Agent bindings
EmailRoutingOptions<Env>
required
Promise<void>
Email Resolvers
createAddressBasedEmailResolver()
Route based on email address (sub-address or local part).string
required
Default agent name to use if email doesn’t contain sub-address
support+ticket123@example.com→SupportAgentinstanceticket123support@example.com→SupportAgentinstancesupportagent+room@example.com→agentinstanceroom
EmailResolver<Env>
createSecureReplyEmailResolver()
Route secure reply emails with signature verification.string
required
Secret key for HMAC verification (must match signAgentHeaders)
SecureReplyResolverOptions
EmailResolver<Env>
Use
signAgentHeaders() when sending outbound emails to enable secure reply routing.createCatchAllEmailResolver()
Route all emails to a single Agent instance.string
required
Agent class name
string
required
Agent instance name
EmailResolver<Env>
Combining Resolvers
Try multiple resolvers in sequence:Secure Reply Flow
Signing Outbound Emails
UsesignAgentHeaders() to sign emails for secure reply routing:
string
required
Secret key for HMAC signing (store in environment variables)
string
required
Agent class name (kebab-case)
string
required
Agent instance name
Promise<Record<string, string>>
replyToEmail()
Reply to an email from within an Agent:AgentEmail
required
The email to reply to
ReplyOptions
required
string
required
Sender name
string
Email subject (defaults to “Re: original subject”)
string
required
Email body
string
default:"text/plain"
MIME content type
Record<string, string>
Additional headers
string | null
Secret for signing headers. Required if email was routed via createSecureReplyEmailResolver. Pass
null to opt out.Promise<void>
Email Utilities
isAutoReplyEmail()
Check if an email is an auto-reply (to avoid reply loops).EmailHeader[]
required
Headers array from postal-mime or similar
boolean
Checks for:
Auto-Submittedheader (RFC 3834)X-Auto-Response-SuppressheaderPrecedence: bulk/junk/listheader
Email Handler
onEmail()
Override to handle incoming emails in your Agent:AgentEmail Type
TheAgentEmail object passed to onEmail():
string
required
Sender email address
string
required
Recipient email address
Headers
required
Email headers
number
required
Size of the raw email in bytes
() => Promise<Uint8Array>
required
Get the raw email content
(options) => Promise<void>
required
Send a reply (use
replyToEmail() instead for automatic header signing)(rcptTo: string, headers?: Headers) => Promise<void>
required
Forward the email to another address
(reason: string) => void
required
Reject the email with a reason
Full Example
wrangler.jsonc Configuration
Security
Signature Verification
Signatures prevent attackers from spoofing email headers to route emails to arbitrary agents:Signature Expiration
Signatures expire aftermaxAge (default: 30 days):
Secret Management
Store secrets in environment variables:.dev.vars for local development:
Best Practices
Use Secure Resolvers
Check Auto-Replies
Handle No Route
Sign Replies
Related
- Agent Class - Email lifecycle hooks
- Email Workers - Cloudflare Email Workers
- Routing - HTTP request routing