Prerequisites
- A domain configured with Cloudflare Email Routing
- An Email Worker configured to receive emails
- An Agent to process emails
Quick Start
Resolvers
Resolvers determine which Agent instance receives an incoming email. Choose the resolver that matches your use case.createAddressBasedEmailResolver
Recommended for inbound mail. Routes emails based on the recipient address.
The sub-address format (
agent+id@domain) allows routing to different agent namespaces and instances from a single email domain.
Agent class names in the recipient address are matched case-insensitively. Email infrastructure often lowercases addresses, so
NotificationAgent+user123@example.com and notificationagent+user123@example.com both route to the NotificationAgent class.createSecureReplyEmailResolver
For reply flows with signature verification. Verifies that incoming emails are authentic replies to your outbound emails, preventing attackers from routing emails to arbitrary agent instances.replyToEmail() and a secret, it signs the routing headers with a timestamp. When a reply comes back, this resolver verifies the signature and checks that it hasn’t expired before routing.
Options:
createCatchAllEmailResolver
For single-instance routing. Routes all emails to a specific agent instance regardless of the recipient address.Combining Resolvers
You can combine resolvers to handle different scenarios:Handling Emails in Your Agent
The AgentEmail Interface
When your agent’sonEmail method is called, it receives an AgentEmail object:
Parsing Email Content
Use a library like postal-mime to parse the raw email:Detecting Auto-Reply Emails
UseisAutoReplyEmail() to detect auto-reply emails and avoid mail loops:
Auto-Submitted, X-Auto-Response-Suppress, Precedence) that indicate an email is an auto-reply.
Replying to Emails
Usethis.replyToEmail() to send a reply:
Forwarding Emails
Rejecting Emails
Secure Reply Routing
When your agent sends emails and expects replies, use secure reply routing to prevent attackers from forging headers to route emails to arbitrary agent instances.How It Works
- Outbound: When you call
replyToEmail()with asecret, the agent signs the routing headers (X-Agent-Name,X-Agent-ID) using HMAC-SHA256 - Inbound:
createSecureReplyEmailResolververifies the signature before routing - Enforcement: If an email was routed via the secure resolver,
replyToEmail()requires a secret (or explicitnullto opt-out)
Setup
- Add a secret to your
wrangler.jsonc:
- Use the combined resolver pattern:
- Sign outbound emails:
Enforcement Behavior
When an email is routed viacreateSecureReplyEmailResolver, the replyToEmail() method enforces signing:
Complete Example
Here’s a complete email agent with secure reply routing:API Reference
routeAgentEmail
createSecureReplyEmailResolver
signAgentHeaders
X-Agent-Name, X-Agent-ID, X-Agent-Sig, and X-Agent-Sig-Ts headers.
Useful when sending emails through external services while maintaining secure reply routing. The signature includes a timestamp and will be valid for 30 days by default.