Skip to main content
Agents can receive and process emails using Cloudflare’s Email Routing. This guide covers how to route inbound emails to your Agents and handle replies securely.

Prerequisites

  1. A domain configured with Cloudflare Email Routing
  2. An Email Worker configured to receive emails
  3. 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.
Routing logic: 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.
When your agent sends an email with 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:
When to use: If your agent initiates email conversations and you need replies to route back to the same agent instance securely.

createCatchAllEmailResolver

For single-instance routing. Routes all emails to a specific agent instance regardless of the recipient address.
When to use: When you have a single agent instance that handles all emails (e.g., a shared inbox).

Combining Resolvers

You can combine resolvers to handle different scenarios:

Handling Emails in Your Agent

The AgentEmail Interface

When your agent’s onEmail 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

Use isAutoReplyEmail() to detect auto-reply emails and avoid mail loops:
This checks for standard RFC 3834 headers (Auto-Submitted, X-Auto-Response-Suppress, Precedence) that indicate an email is an auto-reply.

Replying to Emails

Use this.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

  1. Outbound: When you call replyToEmail() with a secret, the agent signs the routing headers (X-Agent-Name, X-Agent-ID) using HMAC-SHA256
  2. Inbound: createSecureReplyEmailResolver verifies the signature before routing
  3. Enforcement: If an email was routed via the secure resolver, replyToEmail() requires a secret (or explicit null to opt-out)

Setup

  1. Add a secret to your wrangler.jsonc:
For production, use Wrangler secrets instead:
  1. Use the combined resolver pattern:
  1. Sign outbound emails:

Enforcement Behavior

When an email is routed via createSecureReplyEmailResolver, the replyToEmail() method enforces signing:

Complete Example

Here’s a complete email agent with secure reply routing:

API Reference

routeAgentEmail

Routes an incoming email to the appropriate Agent based on the resolver’s decision.

createSecureReplyEmailResolver

Creates a resolver for routing email replies with signature verification.

signAgentHeaders

Manually sign agent routing headers. Returns an object with 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.