{"url":"https://xstate.com/blog/every-mailbox-is-an-agent","title":"Every mailbox is an agent: how xstate stores and sends email","summary":"Each mailbox is its own Cloudflare Agent with its own SQLite database. Amazon SES does the SMTP, and R2 keeps every message whole.","date":"2026-11-30","updated":null,"author":"xstate","tags":["email","architecture","security"],"minutes":6,"markdown":"https://xstate.com/blog/every-mailbox-is-an-agent.md","content_markdown":"xstate is business email on your own domain. This post is about what is underneath it: where a message goes when it\narrives, where it lives, how it leaves, and what stops one customer's mail from being read by another. The\n[architecture page](https://xstate.com/docs/architecture) in the docs is the reference; this is the reasoning.\n\n## SES at the edge, a Cloudflare Agent per mailbox behind it\n\nEmail has two very different halves. One is talking SMTP to every other mail server in the world, with the\nreputation, retries and abuse handling that come with it. The other is being a good place to keep and find mail.\n\nWe gave the first half to Amazon SES. It receives mail for our customers' domains and delivers what they send.\nEverything else is ours and runs on Cloudflare: Workers for the API, an Agent per mailbox, R2 for the messages, D1\nfor accounts and domains, and Queues between the steps.\n\n## What \"a mailbox is an agent\" means\n\nEach mailbox is one [Cloudflare Agent](https://developers.cloudflare.com/agents/), found by the mailbox's id. An\nagent, in Cloudflare's sense, is a small stateful program with its own storage that other code calls by name. It is\nnot a language model reading your mail. The AI agents people build in xstate are a separate part of the product, and\nthey are only offered mail from the mailboxes they are pointed at.\n\nThe mailbox's agent owns that mailbox's index: senders, subjects, folders, read state, conversations, the numbering\nmail apps need, its calendars, and a log of where every message went. All of it sits in one SQLite database that\nbelongs to that agent alone.\n\n## Why a database per mailbox\n\nThe usual design is one large database with a `messages` table and a `mailbox_id` column. We chose the opposite, for\nthree reasons.\n\n- **Queries stay small.** Listing a folder or searching a mailbox only ever reads that mailbox's rows, because no\n  other rows are there. The index never grows with the number of customers.\n- **A busy mailbox is its own problem.** Importing years of mail into one mailbox doesn't slow the mailbox next\n  to it.\n- **A missing `WHERE` clause can't leak mail.** With one table, every query has to remember to filter by mailbox.\n  Here, the mail of two mailboxes is never in the same database to begin with.\n\nSearch inside a mailbox is two SQLite full-text indexes: one that matches parts of words over senders, recipients\nand subjects, so half an address finds its mail, and one over message text that stores words without their\npositions. That second choice makes the index about a tenth of the size, and costs us exact-phrase search.\n\n## Every message is one file\n\nThe agent holds the index, not the mail. Each message is kept whole, as it arrived or left, as one file in the\norganization's own R2 bucket. One bucket per organization.\n\nThe index records where each attachment sits inside its message, so opening an attachment reads only those bytes of\nthe file. Deleting a message is one row and one file. Exporting it gives you exactly what was sent.\n\n## Receiving: from MX record to the mailbox's agent\n\nThe part we spent the most time on is what happens when a step fails halfway. A message can be 40 MB, a Worker has\n128 MB of memory, and any step can be retried. So:\n\n- **Every step is safe to repeat.** A retried job looks the message up by SES's id for it and stops if it is\n  already delivered. A message can't arrive twice.\n- **The original outlives the attempt.** The copy SES saved is deleted only after delivery, and kept 7 days\n  otherwise, so a job that fails five times still has something to deliver once we've fixed the cause.\n- **Slow work can't hold mail back.** Filing attachments, indexing for search and offering mail to AI agents happen\n  on a second queue. If a model or the search index is having a bad hour, the inbox doesn't notice.\n- **Unknown addresses are dropped, never bounced.** By then SES has accepted the message, so a bounce would go to\n  whoever the sender claimed to be. That is how forged mail turns a mail service into a spam source.\n\n## Sending: one way out\n\nMail can be written in the web, mobile and desktop apps, by the email API, by a connected AI app, or in a mail app\nover SMTP. Every one of them ends in the same function, and every email leaves through it. That is what makes the\nrules enforceable: the access check, the list of addresses that bounced before, the attachment rules and\nthe limits are applied in one place, so there is no forgotten path around them.\n\nWe also write the MIME message ourselves, and read every message we build back with our own parser in tests. A\nlibrary would have been faster to start with; owning it means that when a mail app shows a message wrongly, the\nanswer is in our code.\n\n## How security is handled\n\nThe full list is on the [security page](https://xstate.com/docs/security). The parts that come from the architecture:\n\n- **Isolation by construction.** A mailbox's index is its own database, and an organization's messages are in its\n  own bucket.\n- **One access check.** Every client goes through the same check before it reaches a mailbox. If you can't open a\n  mailbox, xstate tells you it doesn't exist.\n- **Keys that can each do one thing.** We talk to Amazon with three identities: one can only read and delete\n  received mail, one can only send, one can only manage domains. The key that sends can't read.\n- **Nothing is trusted on arrival.** Notifications from Amazon are accepted only from our own topics, with a valid\n  signature. Every received message carries SES's SPF, DKIM and DMARC verdicts, and those feed one Inbox-or-Junk\n  decision along with what the mailbox knows about the sender and a set of phishing checks.\n- **Reading is sandboxed.** A message opens in a frame where its scripts never run, with remote images off until\n  you ask for them.\n- **A shared reputation is protected.** Each mailbox and organization has hourly and daily sending limits, counted\n  in one transaction so two sends at the same moment can't both slip under. Sending is suspended automatically when\n  bounces or complaints climb, long before Amazon would act.\n\n## IMAP and SMTP without a TCP port\n\nApple Mail and Outlook speak IMAP and SMTP, which need a raw network connection. Cloudflare Workers can't accept\none yet.\n\nSo a small bridge listens on the mail ports, ends TLS, and passes each connection's bytes to Cloudflare over a\nWebSocket. It doesn't understand either protocol and writes nothing to disk. On the other side, each connection gets\nits own session object that speaks IMAP or SMTP and answers from the same mailbox agent and the same files as the\nweb app. There is one copy of your mail, so every client agrees about it.\n\n## What this design costs us\n\n- **We depend on Amazon SES.** Its reputation is ours, and its outage would be ours. The limits and automatic\n  suspension exist because of that.\n- **Mail is not end-to-end encrypted.** Messages are encrypted at rest by Cloudflare and in transit, but xstate has\n  to read a message to index it, thread it and check it for spam.\n- **Not everything is per mailbox.** Accounts, domains and sending limits live in one shared database, and semantic\n  search uses one shared index, partitioned per organization.\n- **An agent does one thing at a time.** That is what makes it simple to reason about, and it is also a ceiling on\n  how fast a single mailbox can be written to.\n- **The bridge is a server.** It is the one part that isn't serverless, and it goes away when Workers can accept\n  inbound connections.\n\n## What's next\n\nA mailbox that is already an addressable program with its own storage is a short step from a mailbox that can do\nwork on a schedule or on arrival. That is the direction we find most interesting, and the reason we built it this\nway.\n\nIf you want the step-by-step version, it is in the [docs](https://xstate.com/docs/architecture)."}