Receiving email for free with a Cloudflare Worker
Every alias on my domain lands in one small Worker that decides what to do, seals the message and stores it, on Cloudflare's free plan. No mail server, no file storage, no card on file.

Keyhaven gives me a new email address for every site. That means it has to receive email, and I really didn’t want to run a mail server.
Cloudflare has a free service called Email Routing. Most people use it to forward mail from their domain to Gmail. But it can also hand every message to a Worker, a small piece of code that runs on Cloudflare’s network. That’s the whole mail server.
One rule catches everything
Email Routing has a catch-all rule: any address on the domain that no other rule matches. I point it at the Worker, and that’s the only rule there is.
So anything@aliases.example reaches the same Worker. A new alias needs no DNS change and no new rule. It’s one row in the database, and the Worker checks that row when mail arrives.

Five decisions per message
For every message, the Worker decides one of five things before doing any real work:
| The mail | What happens |
|---|---|
| over 10 MB | rejected with 552, so the sender gets a bounce (Email Routing itself stops at 25 MiB) |
| to an address that isn’t an alias | rejected with 550 No such address |
| to an alias I turned off | dropped silently: no bounce, so the sender can’t tell the address is off |
| from a sender I blocked | dropped the same way (new in 1.3.0: “Block this sender” on any mail) |
| everything else | sealed and stored |
There’s one optional twist: a setting that makes an alias on its first mail, for addresses on my own domain. It’s off by default.
A rejection happens while the sending server is still connected, so it gets a proper error back. And the Worker’s logs only ever say “stored”, “rejected” or “dropped”, never an address and never any content.
Blocked senders are a small puzzle of their own: the Worker has to compare the sender against my block list before it seals anything, but I didn’t want that list readable on the server. So it keeps only a hash of each blocked address or domain, and the readable label I see in Settings is encrypted with my vault key.
Where the mail goes: D1, in pieces
The natural place for big blobs on Cloudflare is R2, its file storage. But R2 wants a card on file, even on its free tier, and my rule was free plans only. So mail goes into D1, Cloudflare’s SQLite database.
D1 has its own limit: a row can hold at most 2 MB. A mail with attachments can be much bigger. So the sealed body is cut into 1.5 MB chunks, each in its own row of a chunks table.
The order matters:
- the chunks go in first, a few per batch
- the mail row goes in last: who it’s for, when, how big, plus the sealed subject and sender for the inbox list
- if anything fails halfway, the chunks already written are deleted again
Because the mail row is written last, the inbox can never show half a message.
The batching isn’t for speed. On the free plan a Worker can run 50 database queries per run. A 10 MB mail is 7 chunks, written in batches of 4: that’s 2 batches, plus the mail row and a couple of lookups. Comfortably inside.
The 10 ms budget
A free Worker gets about 10 ms of CPU per run. Waiting for the database doesn’t count; only real work does. Sealing a normal mail is one key exchange, one key derivation and one pass of AES-GCM over the bytes, which is far below the limit. A very big mail, with many megabytes of attachments, might run out of time, and Email Routing would then bounce it. For a personal inbox, I can live with that.
Reading mail has the same budget, so the server does as little as possible there too. When my device asks for a body, the database itself turns the chunks into text with SQLite’s hex() function, and the server just passes that along. No byte-by-byte work on the server.
Everything else happens on my device: it opens the seal, then takes the message apart (multipart, quoted-printable, base64, encoded subjects, character sets, attachments, inline images) and looks for sign-up codes and verification links.
On a free plan, the server’s job is to decide, store and pass along. The thinking happens on my device.
Clean-up once a day
The same Worker has a second job. Once a day, at 03:17 UTC, a scheduled run deletes mail older than the number of days I set (zero means keep forever). Sent mail too. Mail I delete by hand goes to a mail trash first, with an undo.
Since 1.14.0 there is also a short run every 15 minutes, only to move one-time inboxes to the Trash and delete notes and secrets whose self-destruct time has passed.
And since 1.15.0 the daily run only empties the Trash on a time I chose. Mail in the Trash no longer goes after 30 days, and ended shared links no longer go after 7. They stay until I delete them or choose a time.
What it costs
One Worker run per mail, plus one clean-up run a day and 96 of those short runs. The free plan allows 100,000 a day. A personal inbox gets nowhere near that.
The real limit is space: a free D1 database holds 500 MB. That’s a lot of passwords, but not a lot of mail with big attachments. That’s what the “delete mail after N days” setting is for.
How the Worker seals each message so that even the database can’t read it is its own post: Sealing mail so my own server can’t read it.