# Every mailbox is an agent: how xstate stores and sends email

> Each mailbox is its own Cloudflare Agent with its own SQLite database. Amazon SES does the SMTP, and R2 keeps every message whole.

2026-11-30 · 6 min · email, architecture, security · xstate

xstate is business email on your own domain. This post is about what is underneath it: where a message goes when it
arrives, where it lives, how it leaves, and what stops one customer's mail from being read by another. The
[architecture page](https://xstate.com/docs/architecture) in the docs is the reference; this is the reasoning.

## SES at the edge, a Cloudflare Agent per mailbox behind it

Email has two very different halves. One is talking SMTP to every other mail server in the world, with the
reputation, retries and abuse handling that come with it. The other is being a good place to keep and find mail.

We gave the first half to Amazon SES. It receives mail for our customers' domains and delivers what they send.
Everything else is ours and runs on Cloudflare: Workers for the API, an Agent per mailbox, R2 for the messages, D1
for accounts and domains, and Queues between the steps.

## What "a mailbox is an agent" means

Each mailbox is one [Cloudflare Agent](https://developers.cloudflare.com/agents/), found by the mailbox's id. An
agent, in Cloudflare's sense, is a small stateful program with its own storage that other code calls by name. It is
not a language model reading your mail. The AI agents people build in xstate are a separate part of the product, and
they are only offered mail from the mailboxes they are pointed at.

The mailbox's agent owns that mailbox's index: senders, subjects, folders, read state, conversations, the numbering
mail apps need, its calendars, and a log of where every message went. All of it sits in one SQLite database that
belongs to that agent alone.

## Why a database per mailbox

The usual design is one large database with a `messages` table and a `mailbox_id` column. We chose the opposite, for
three reasons.

- **Queries stay small.** Listing a folder or searching a mailbox only ever reads that mailbox's rows, because no
  other rows are there. The index never grows with the number of customers.
- **A busy mailbox is its own problem.** Importing years of mail into one mailbox doesn't slow the mailbox next
  to it.
- **A missing `WHERE` clause can't leak mail.** With one table, every query has to remember to filter by mailbox.
  Here, the mail of two mailboxes is never in the same database to begin with.

Search inside a mailbox is two SQLite full-text indexes: one that matches parts of words over senders, recipients
and subjects, so half an address finds its mail, and one over message text that stores words without their
positions. That second choice makes the index about a tenth of the size, and costs us exact-phrase search.

## Every message is one file

The agent holds the index, not the mail. Each message is kept whole, as it arrived or left, as one file in the
organization's own R2 bucket. One bucket per organization.

The index records where each attachment sits inside its message, so opening an attachment reads only those bytes of
the file. Deleting a message is one row and one file. Exporting it gives you exactly what was sent.

## Receiving: from MX record to the mailbox's agent

The part we spent the most time on is what happens when a step fails halfway. A message can be 40 MB, a Worker has
128 MB of memory, and any step can be retried. So:

- **Every step is safe to repeat.** A retried job looks the message up by SES's id for it and stops if it is
  already delivered. A message can't arrive twice.
- **The original outlives the attempt.** The copy SES saved is deleted only after delivery, and kept 7 days
  otherwise, so a job that fails five times still has something to deliver once we've fixed the cause.
- **Slow work can't hold mail back.** Filing attachments, indexing for search and offering mail to AI agents happen
  on a second queue. If a model or the search index is having a bad hour, the inbox doesn't notice.
- **Unknown addresses are dropped, never bounced.** By then SES has accepted the message, so a bounce would go to
  whoever the sender claimed to be. That is how forged mail turns a mail service into a spam source.

## Sending: one way out

Mail can be written in the web, mobile and desktop apps, by the email API, by a connected AI app, or in a mail app
over SMTP. Every one of them ends in the same function, and every email leaves through it. That is what makes the
rules enforceable: the access check, the list of addresses that bounced before, the attachment rules and
the limits are applied in one place, so there is no forgotten path around them.

We also write the MIME message ourselves, and read every message we build back with our own parser in tests. A
library would have been faster to start with; owning it means that when a mail app shows a message wrongly, the
answer is in our code.

## How security is handled

The full list is on the [security page](https://xstate.com/docs/security). The parts that come from the architecture:

- **Isolation by construction.** A mailbox's index is its own database, and an organization's messages are in its
  own bucket.
- **One access check.** Every client goes through the same check before it reaches a mailbox. If you can't open a
  mailbox, xstate tells you it doesn't exist.
- **Keys that can each do one thing.** We talk to Amazon with three identities: one can only read and delete
  received mail, one can only send, one can only manage domains. The key that sends can't read.
- **Nothing is trusted on arrival.** Notifications from Amazon are accepted only from our own topics, with a valid
  signature. Every received message carries SES's SPF, DKIM and DMARC verdicts, and those feed one Inbox-or-Junk
  decision along with what the mailbox knows about the sender and a set of phishing checks.
- **Reading is sandboxed.** A message opens in a frame where its scripts never run, with remote images off until
  you ask for them.
- **A shared reputation is protected.** Each mailbox and organization has hourly and daily sending limits, counted
  in one transaction so two sends at the same moment can't both slip under. Sending is suspended automatically when
  bounces or complaints climb, long before Amazon would act.

## IMAP and SMTP without a TCP port

Apple Mail and Outlook speak IMAP and SMTP, which need a raw network connection. Cloudflare Workers can't accept
one yet.

So a small bridge listens on the mail ports, ends TLS, and passes each connection's bytes to Cloudflare over a
WebSocket. It doesn't understand either protocol and writes nothing to disk. On the other side, each connection gets
its own session object that speaks IMAP or SMTP and answers from the same mailbox agent and the same files as the
web app. There is one copy of your mail, so every client agrees about it.

## What this design costs us

- **We depend on Amazon SES.** Its reputation is ours, and its outage would be ours. The limits and automatic
  suspension exist because of that.
- **Mail is not end-to-end encrypted.** Messages are encrypted at rest by Cloudflare and in transit, but xstate has
  to read a message to index it, thread it and check it for spam.
- **Not everything is per mailbox.** Accounts, domains and sending limits live in one shared database, and semantic
  search uses one shared index, partitioned per organization.
- **An agent does one thing at a time.** That is what makes it simple to reason about, and it is also a ceiling on
  how fast a single mailbox can be written to.
- **The bridge is a server.** It is the one part that isn't serverless, and it goes away when Workers can accept
  inbound connections.

## What's next

A mailbox that is already an addressable program with its own storage is a short step from a mailbox that can do
work on a schedule or on arrival. That is the direction we find most interesting, and the reason we built it this
way.

If you want the step-by-step version, it is in the [docs](https://xstate.com/docs/architecture).
