tmail.sh

A mail server whose native protocol is gRPC.

Tern is a mail server written from scratch in Rust, with its own storage engine made for spinning disks. Its apps, called tmail, speak one typed API to it: mail.v1, 48 calls over HTTP/2. IMAP and LMTP are served too, for everything else.

Read the protocol See it against Cyrus IMAP

Folders with their counts, and the first page of the inbox

IMAP, as a mail API asks Cyrus

  1. a001 LOGIN ann@example.org ••••••••
  2. a002 LIST "" "*"
  3. a003 STATUS "INBOX" (MESSAGES UNSEEN)
  4. a004 STATUS "Archive" (MESSAGES UNSEEN)
  5. a005 STATUS "Drafts" (MESSAGES UNSEEN)
  6. a006 STATUS "Sent" (MESSAGES UNSEEN)
  7. a007 STATUS "Trash" (MESSAGES UNSEEN)
  8. a008 STATUS "Junk" (MESSAGES UNSEEN)
  9. a009 STATUS "Receipts" (MESSAGES UNSEEN)
  10. a010 STATUS "Travel" (MESSAGES UNSEEN)
  11. a011 STATUS "Work" (MESSAGES UNSEEN)
  12. a012 STATUS "Work/Contracts" (MESSAGES UNSEEN)
  13. a013 STATUS "Work/Hiring" (MESSAGES UNSEEN)
  14. a014 STATUS "Work/Invoices" (MESSAGES UNSEEN)
  15. a015 STATUS "Lists/dev" (MESSAGES UNSEEN)
  16. a016 STATUS "Lists/announce" (MESSAGES UNSEEN)
  17. a017 STATUS "Lists/security" (MESSAGES UNSEEN)
  18. a018 STATUS "Lists/users" (MESSAGES UNSEEN)
  19. a019 STATUS "Family" (MESSAGES UNSEEN)
  20. a020 STATUS "Friends" (MESSAGES UNSEEN)
  21. a021 STATUS "Bank" (MESSAGES UNSEEN)
  22. a022 STATUS "Taxes" (MESSAGES UNSEEN)
  23. a023 STATUS "Insurance" (MESSAGES UNSEEN)
  24. a024 STATUS "Health" (MESSAGES UNSEEN)
  25. a025 STATUS "House" (MESSAGES UNSEEN)
  26. a026 STATUS "Car" (MESSAGES UNSEEN)
  27. a027 STATUS "School" (MESSAGES UNSEEN)
  28. a028 STATUS "Orders" (MESSAGES UNSEEN)
  29. a029 STATUS "Newsletters" (MESSAGES UNSEEN)
  30. a030 STATUS "Notifications" (MESSAGES UNSEEN)
  31. a031 STATUS "Projects" (MESSAGES UNSEEN)
  32. a032 STATUS "Projects/2025" (MESSAGES UNSEEN)
  33. a033 STATUS "Projects/2026" (MESSAGES UNSEEN)
  34. a034 STATUS "Notes" (MESSAGES UNSEEN)
  35. a035 SELECT "INBOX"
  36. a036 STATUS "INBOX" (MESSAGES UNSEEN)
  37. a037 SORT (REVERSE ARRIVAL) UTF-8 ALL
  38. a038 FETCH 36898,36897,… (FLAGS ENVELOPE)

38 commands sent one at a time, each after the last was answered

gRPC, as tmail asks Tern

  1. Mail.ListFolders { }
  2. ← 32 folders, each with its messages and unseen, and the mailbox's state
  3. Mail.ListConversations { folder: 1 limit: 50 }
  4. ← 50 conversations as a list shows them: subject, senders, count, unread, starred, labels; all read at one state of the mailbox, with where the next page begins

2 calls on one HTTP/2 connection

The IMAP side is what a mail API in front of Cyrus sends today: a STATUS for each of 32 folders, then a SORT of all 36,898 messages of the inbox to show 50. The login is counted because that backend logs in for each request; a gRPC call carries its token with it. With LIST-STATUS it comes to 6 commands, and Tern's own IMAP answers that as well; the sort of the whole folder stays.

gRPC is the protocol, not a gateway in front of one.

No tmail app speaks IMAP. Everything a client can do is a call in one file, mail.proto, and the server is built so that each call reads what its reply needs and no more.

Five services: Mail, Accounts, Settings, Push and Sessions. Unary calls for what fits in a reply, streams for what does not: a part, a whole message, the rows of a query, the changes to a mailbox as they happen.

The protocol, call by call

Excerpts from mail/v1/mail.proto
// What a request costs is part of this contract. Each call
// reads what its reply needs and no more; fields that cost
// further reads are asked for.

package mail.v1;

service Mail {
  // Folders, with their counts, in one request.
  rpc ListFolders(ListFoldersRequest)
      returns (ListFoldersResponse);
  // A page of a folder's conversations, newest first.
  // One state of the mailbox answers the page.
  rpc ListConversations(ListConversationsRequest)
      returns (ListConversationsResponse);
  rpc GetPart(GetPartRequest) returns (stream Chunk);
  rpc Query(QueryRequest) returns (stream QueryReply);
  rpc Changes(ChangesRequest) returns (ChangesResponse);
  rpc Watch(WatchRequest) returns (stream ChangesResponse);
  // … 21 calls in this service, 48 in all
}
One stream carries every change to a mailbox, from every device
  1. Mail.Watch { after: { state: 48211 } }
  2. ← state 48212 changed_ids [403758]new mail, labelled Inbox
  3. ← state 48213 changed_ids [403758]read on the phone
  4. ← state 48214 changed_ids [403741, 403755]archived on the laptop
  5. ← state 48215 deleted [403702]emptied from Trash
  6. ← state 48216 settings_revision 91a signature edited
  7. … the stream stays open

An illustration of Watch, with made-up numbers. A client that reconnects passes the last position it was given, and misses nothing.

A mailbox has a state, and every change moves it
The state is the number of changes a mailbox has seen. Changes gives what happened after a position, Watch streams it. An app that was away for 160 changes catches up on a mailbox of 403,757 messages in 7 calls and 41 KiB.
Streams where mail is large
An attachment leaves in 64 KiB chunks, each read from the store when the client has taken the one before. A slow client holds no thread, and about 3 MiB of memory at the most.
The cost of a call is written down
A page of a folder is 4 seeks on a cold disk, a message with its text is 1. A query's last reply says what it read: lists, candidates, rows.
Two doors, one API
At the public door a device presents its own token over TLS, and each call needs a scope. At the services' door, on loopback, a trusted service may name any user.

The same mailbox, on a fifth of the disk.

One real mailbox, 403,757 messages in 32 folders, 11.3 GiB as it arrived, loaded into Cyrus IMAP 3.10.2, Stalwart 1.2.1 and Tern. What each keeps on disk, drawn to scale:

Disk used for the 11.3 GiB mailbox, in GiB as allocated. Measured 2026-11-27.

  • Message bodies
  • Metadata and index
  • Search index
Cyrus IMAP 3.10.2, with Xapian
22.3 1.97× the mail
Stalwart 1.2.1, RocksDB
10.1 0.90×
Tern
4.2 0.37×
The same figures as a table
Cyrus IMAPStalwartTern
Message bodies, GiB12.07.44.1
Metadata and index, GiB1.22.20.08
Search index, GiB9.2about 0.40.06
Total, GiB22.310.14.2
Files on disk403,90742331,280

Not like for like: the search indexes of Cyrus and Stalwart also cover headers, and Cyrus's keeps where each word stands, for phrases. Tern's has the words of bodies and subjects, and names. Tern has no replication yet. The whole comparison, with what it leaves out

Every message is taken apart, and put back byte for byte.

A message is split into its skeleton and its parts. Attachments are stored decoded, so base64 no longer costs a third more, and once each by their hash, in a directory or an S3-like store. Everything else is compressed into rows, twenty or so messages to a 256 KiB block.

Before a delivery is acknowledged the message is put together again and compared with what arrived. On the example mailbox, all 12.08 GB were read back through IMAP and compared, whole and in 2,172,296 sections. Nothing differs.

Underneath is an engine written for this: immutable sorted runs in two keyspaces, one log, and one disk sync shared by every write in flight.

How the storage engine works

What a request reads from a cold disk, counted in seeks
RequestSeeks
A message, with its structure and text1
Folders with their counts2
A page of a folder, newest 504.0
Changes, the last 50 messages4
A thread5.3

Averages on the 403,757-message mailbox as first delivered, with nothing cached. A seek on a spinning disk is about 10 ms.

Timed on spinning disks, from cold.

Fast storage hides a bad layout, so Tern is timed where it cannot: three spinning disks in ZFS raidz1, the caches dropped and the server started anew before each flow.

Opening a folder, a page of it, a thread or a message takes a third of a second from cold. A word in the text of 36,898 messages takes 0.6.

What is still slow is said too: the header fields of every message of a 36,898-message folder take 12 to 25 seconds from cold, because a folder's messages lie among all of the user's. Why, and what would mend it

The 403,757-message mailbox over IMAP, in milliseconds
FlowColdWarm
Folder list with counts, one command0.80.8
Open the inbox, 36,898 messages9135
A page of it: open, sort by arrival, fetch 5020561
One message, whole1151.4
A thread in the inbox284132
A word in the text of the inbox590125
Open the largest folder, 166,143 messages22780

Delivering the whole mailbox onto the same disks, with every word indexed as it arrives: 524 seconds, 770 messages a second (the engine's own cost: the large parts were in the blob store already).

tmail is the apps.

Labels, conversations and a split inbox, driven from the keyboard. Each app is native to its system and all of them share one core in Rust, which holds the connection and keeps a copy of the mailbox up to date with Changes and Watch.

Linux

GTK 4 and libadwaita, in Rust, calling the core directly. x86_64 and aarch64.

  • The whole mailbox is on the computer: a list opens from the copy, at once
  • libadwaita's widgets, with the system's font, accent, and light or dark
  • New mail told of for the labels chosen; a burst is told as one
  • Handles mailto: links, and can be the desktop's mail program
  • A file opened with it becomes a new message or a note to self
  • Asks before quitting while mail waits in the outbox

iPhone

SwiftUI over the core. iOS 27, in TestFlight.

  • The whole mailbox on the phone; the first run brings the labels, the Inbox and the last 30 days first
  • Swipes: to the right Done, then Star; to the left read or unread, then Label and Trash
  • Push notifications for the labels chosen, saying who and what about
  • Widgets, Spotlight and Shortcuts, which show who and what about and never a body
  • The share sheet sends by itself, with Undo
  • The assistant can be talked to; in the car, by voice through CarPlay, with Approve and Decline on the car's screen

Mac

SwiftUI and AppKit over the core. macOS 27 on Apple silicon, sandboxed, in TestFlight.

  • One window, worked from the keyboard: a conversation takes the list's place
  • The assistant in a panel down the right side, beside the mail
  • A message written in a window of its own
  • Widgets, Spotlight, Shortcuts and the Share menu
  • Attached files opened in Quick Look
  • What is being written is saved before the app quits

Webmail

Svelte 5 and TypeScript, with a small server in Rust that runs the core for each person signed in.

  • Keeps no mailbox state: every list, count and conversation is asked of the server
  • HTML mail in a sandboxed frame, with no scripts and not the page's origin
  • What the page changes shows at once, and is read back when the server tells of it
  • The session is a cookie no script can read, ended after 30 days unused
  • The assistant's keys are sealed on the host and never sent back to the browser
  • Every request says how long it took, in Server-Timing

tmail, for agents

A command, in Rust. The mailbox for Claude Code and other agents, with the in-app assistant's tools.

  • One command for each tool: search, read, label, draft, send, rules, the away reply
  • --dry-run on every command that changes anything, saying what would change
  • Carries its own skill: tmail skill --install
  • Keeps no copy: every command asks the server
  • Signs in once, in a browser; the tokens go in the keyring
  • A draft sent twice goes once, under the same Message-ID

Any other mail client

IMAP, on the same store as everything else.

  • SORT, QRESYNC, IDLE and LIST-STATUS
  • A password made for each device, shown once
  • Each label shows as a mailbox
  • Checked against Cyrus on every message of the example mailbox
  • Passes 100 checks written with JavaMail
  • Dovecot's imaptest, ten clients at once, reports nothing in its plain stress run

What the apps do

The inbox

  • A split inbox: tabs with unread counts, each split a saved search
  • What a split would take, shown before it is saved
  • Labels, several to a conversation, with favourites
  • Views: any search kept in the sidebar
  • Boards: labels as columns, cards moved between them
  • Changes made on another device show at once

Reading

  • Conversations, with earlier messages and quoted text folded
  • HTML mail without scripts; remote images only when asked
  • The sender's recent mail and files beside the conversation
  • A reply written at the conversation's foot
  • A message's raw source, copied or saved as .eml

Triage

  • Done, Star, read and unread, from the keyboard
  • Undo of Done and of Delete
  • Several conversations at once
  • Done goes on to the next conversation

Writing

  • Undo after Send; an outbox with Send now and Edit
  • Send from any of your addresses, each with its signature
  • Drafts saved as they are written
  • A file uploaded once, with its progress shown
  • Waiting: a label put on the conversation once sent
  • Notes to self: text, a link, files

Search and keys

  • The query language, with results as it is typed
  • Fields and labels suggested while typing
  • A command palette, and a key for every command
  • A search saved as a split, a view or a board

Files

  • Files of the mailbox found by name and sender
  • The shelf: a file kept aside to attach later
  • Files dropped on the app kept as a note to self

Rules and settings

  • Rules that read as sentences, edited by a form
  • The away reply
  • Settings that follow the person to every device
  • The labels that notify, chosen by the person

The assistant

  • A chat in the app, its answers streamed
  • Approval cards before it changes anything, and Undo of its steps
  • Asked about the conversation that is open
  • Spoken to, on the iPhone and the Mac
  • The engine, the model and the key are the person's choice

Account

  • Passkeys; after the first, the password lets nobody in
  • The account's devices listed, and ended one by one
  • Passwords made for other mail programs, one a device
  • Sign out removes the copy, and names unsent mail first

In the Linux, iPhone and Mac apps as of 2026-12-04. The webmail has most of it; it keeps no copy, so it does not work without the server.

Coming from Cyrus IMAP.

cyrus-migrate reads a Cyrus spool as it lies on disk: index files of versions 12 to 20, flags, quota, the Sieve script. Folders become labels and the script becomes the person's rules.

It can be run again and again. A second run over a mailbox already moved takes 1.5 seconds, and verify compares every message, byte for byte, with the one Cyrus holds.

Moving a mailbox from Cyrus

A trial on a copy of a Cyrus spool: 37 folders, 403,917 messages, 13.3 GB
# first run
appended   403,911 messages   1 h 54 min
refused          1            its file is missing in the copy

# second run
appended         0            1.5 s

# verify, with the message bytes compared
missing          1            the same message
extra, labels, flags, bytes, arrivals,
script, quota                all 0       132.7 s

# the store afterwards
on disk    4.87 GB            of 13.3 GB in Cyrus's files

Not released yet. The numbers are real.

Tern is in development, and mail.v1 is still declared unstable. The timings and sizes on this site come from runs on one real mailbox, and the docs say how each was taken and what it leaves out.

Not built yet: replication, ranking of search results, shared folders, the text of attachments.

Read the docs