Quality IMAP, Part 1: Your Mailbox Is A Database

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

Quality IMAP, Part 1: Your Mailbox Is A Database

Quality IMAP, Part 1: Your Mailbox Is A Database Published on November 16, 2026 19 min reading time 3704 words During the past year, we have significantly improved the reliability and correctness of Aerogramme’s IMAP implementation. This is the first part in a series of blog posts elaborating on why implementing IMAP correctly is not so easy, and the set of techniques we have been using to make our IMAP code as high-quality as we can. In this first part, we focus on one challenging aspect of IMAP: fine-grained retrieval and querying of emails.

IMAP 质量系列,第一部分:你的邮箱就是一个数据库。发布于 2026 年 11 月 16 日,阅读时长 19 分钟,3704 字。在过去的一年中,我们显著提高了 Aerogramme 的 IMAP 实现的可靠性和正确性。这是系列博客文章的第一部分,旨在阐述为什么正确实现 IMAP 并非易事,以及我们为使 IMAP 代码达到尽可能高的质量所采用的一系列技术。在第一部分中,我们重点关注 IMAP 的一个挑战性方面:电子邮件的细粒度检索和查询。

Aerogramme is an experimental email server (& calendar, contacts), designed to be geo-distributed, hosted outside of datacenters, on refurbished hardware, and reliable even in presence of network issues and hardware failures. It is built on top of Garage and shares the same philosophy. Aerogramme is AI-free: we do not wish to use unethical technology and prefer to spend effort on careful craftmanship and community-building.

Aerogramme 是一个实验性的电子邮件服务器(及日历、联系人),旨在实现地理分布式部署,托管在数据中心之外的翻新硬件上,即使在网络问题和硬件故障的情况下也能保持可靠。它构建于 Garage 之上并共享相同的理念。Aerogramme 不含人工智能:我们不希望使用不道德的技术,更倾向于将精力投入到精心的工艺和社区建设中。

Implementing an IMAP server is not easy. Partly because IMAP is an old protocol, with a good number of extensions that has accumulated quite a bit of complexity over time. On top of that IMAP provides - for good reasons - two classes of features that are fundamentally challenging to implement: fine-grained retrieval and querying of email contents and concurrent multi-client multi-session access to mailboxes.

实现 IMAP 服务器并不容易。部分原因是 IMAP 是一个古老的协议,随着时间的推移,它积累了大量扩展,变得相当复杂。此外,IMAP 还提供了两类从根本上难以实现的功能(当然是有充分理由的):电子邮件内容的细粒度检索与查询,以及对邮箱的并发多客户端多会话访问。

This is the first part in a series of posts discussing how we have been approaching IMAP access. In the past year, we have been refining Aerogramme’s IMAP implementation to be “more correct”, in a context where “correctness” is unfortunately not always well specified: IMAP and email are defined in documents that are often vague, and furthermore, real-world implementations do not always follow specifications.

这是讨论我们如何处理 IMAP 访问的系列文章的第一部分。在过去的一年中,我们一直在改进 Aerogramme 的 IMAP 实现,使其“更加正确”。然而在当前的背景下,“正确性”往往没有得到很好的定义:IMAP 和电子邮件的定义文档通常比较模糊,而且现实世界的实现也并不总是遵循规范。

Today, we’ll walk you though our work on the correctness and reliability of Aerogramme for retrieving and querying emails, and explain why your mailbox is, in fact, a database… This work was funded by NLNet under the “Aerogramme 1.0” grant, and could not have been possible without their support. Feel free to join the Aerogramme matrix channel for updates or if you just want to chat!

今天,我们将带您了解我们在 Aerogramme 电子邮件检索和查询的正确性与可靠性方面所做的工作,并解释为什么您的邮箱实际上就是一个数据库……这项工作由 NLNet 通过“Aerogramme 1.0”资助项目提供支持,没有他们的支持,这一切都不可能实现。欢迎加入 Aerogramme 的 Matrix 频道获取更新,或者仅仅是想聊聊天!

Your mailbox is a database. One challenge of the IMAP specification is the rich feature-set it provides for retrieving mailbox and email contents. We may sometimes think of a mailbox as a directory containing plain email files; however, from the point of view of an IMAP server (or client), a mailbox is closer to a database, accessed using a dedicated query language.

你的邮箱就是一个数据库。IMAP 规范的一个挑战在于它为检索邮箱和电子邮件内容提供了丰富的功能集。我们有时可能认为邮箱只是一个包含纯文本电子邮件文件的目录;然而,从 IMAP 服务器(或客户端)的角度来看,邮箱更像是一个使用专用查询语言访问的数据库。

For instance, mobile clients, which have slower and unreliable connectivity, benefit from fine-grained querying of a mailbox contents. User experience is much better if an email client can selectively fetch the subject of new emails (to update the list of emails in the mailbox) without having to first download all of their attachments or doing a sync of the entire mailbox.

例如,连接速度较慢且不稳定的移动客户端,可以从邮箱内容的细粒度查询中受益。如果电子邮件客户端能够有选择地获取新邮件的主题(以更新邮箱中的邮件列表),而无需先下载所有附件或同步整个邮箱,那么用户体验会好得多。

To facilitate the mobile client use-case, IMAP specifies two key mechanisms for querying and (partial) retrieval of emails in a mailbox: SEARCH looks up the emails in a mailbox that satisfy a given “search criteria”; it returns their identifiers. Criteria can select emails based on the contents of specific headers, date, text contents, or flags - metadata attached to emails that can be edited by the client. It can also be any combination of those using OR/NOT/AND boolean operators.

为了促进移动客户端的使用场景,IMAP 规定了两种用于查询和(部分)检索邮箱中电子邮件的关键机制:SEARCH 用于查找邮箱中满足给定“搜索条件”的电子邮件;它会返回这些邮件的标识符。条件可以根据特定标题的内容、日期、文本内容或标记(由客户端编辑的附加到电子邮件的元数据)来选择邮件。它还可以使用 OR/NOT/AND 布尔运算符进行任意组合。

FETCH queries the contents of an email given its identifier. Extremely versatile. On top of fetching the entire email, it allows querying an email based on its high-level structure (emails have a recursive structure with sub-parts containing e.g. attachments or text+html alternative views of the same contents), its raw byte-level representation, or a combination of both.

FETCH 根据标识符查询电子邮件的内容。它极其通用。除了获取整封邮件外,它还允许根据电子邮件的高级结构(电子邮件具有递归结构,子部分包含例如附件或同一内容的文本+HTML 替代视图)、原始字节级表示或两者的组合来查询电子邮件。

It is possible to selectively fetch sub-parts of an email (according to its structure), obtain an “abstract syntax tree” (AST) for the email and the value of some of its headers, and also fetch specific byte ranges for sub-parts. A direct consequence of these features is that an IMAP server actually needs to parse the emails, not just store them. It then needs to (correctly) interpret the (sometimes tricky) query language of SEARCH and FETCH, and internally support a data architecture that enable efficiently searching and querying over the emails.

可以有选择地获取电子邮件的子部分(根据其结构),获取电子邮件的“抽象语法树”(AST)以及某些标题的值,还可以获取子部分的特定字节范围。这些功能的直接后果是,IMAP 服务器实际上需要解析电子邮件,而不仅仅是存储它们。然后,它需要(正确地)解释 SEARCH 和 FETCH 的(有时很棘手的)查询语言,并在内部支持一种能够高效搜索和查询电子邮件的数据架构。

Email querying in Aerogramme. Our implementation of SEARCH and FETCH is backed by the three following components: The logic for SEARCH and FETCH is implemented as an interpreter that takes already parsed emails and walks down their AST to evaluate IMAP-specific queries. This separates email structure (email ASTs do not depend on IMAP concepts) from IMAP-specific business logic. This layer has been significantly reworked in the last months, fixing a good number of logic bugs and edge cases in our implementation of the SEARCH and FETCH IMAP specification.

Aerogramme 中的电子邮件查询。我们对 SEARCH 和 FETCH 的实现由以下三个组件支持:SEARCH 和 FETCH 的逻辑被实现为一个解释器,它接收已经解析的电子邮件并遍历其 AST 以评估 IMAP 特定的查询。这使得电子邮件结构(电子邮件 AST 不依赖于 IMAP 概念)与 IMAP 特定的业务逻辑分离开来。该层在过去几个月中进行了重大重构,修复了我们在实现 SEARCH 和 FETCH IMAP 规范时存在的许多逻辑错误和边缘情况。

Email parsing is done using the eml-codec companion library, that we also develop and maintain. The eml-codec library parses emails into a rich, structured AST while also giving access to raw input slices that correspond to relevant AST nodes; this is required for implementing FETCH. In the past year, eml-codec has evolved from an email parsing library with IMAP-specific quirks to a robust, well-tested, general-purpose email library with support for both parsing and printing of emails. You can read more about the library design choices in our work-in-progress design document.

电子邮件解析使用我们开发和维护的配套库 eml-codec 完成。eml-codec 库将电子邮件解析为丰富、结构化的 AST,同时还提供对对应于相关 AST 节点的原始输入切片的访问;这是实现 FETCH 所必需的。在过去的一年中,eml-codec 已经从一个带有 IMAP 特定怪癖的电子邮件解析库,演变成一个健壮、经过充分测试、支持电子邮件解析和打印的通用电子邮件库。您可以在我们正在编写的设计文档中阅读更多关于该库设计选择的内容。

Aerogramme’s internal data architecture splits mailbox state, email metadata, and email contents. It is briefly described in Aerogramme’s documentation. Mailbox state (emails IDs and flags) and email metadata (parsed headers, date, size) are stored in Aerogramme’s metadata storage using Garage K2V, while email contents are stored in blob storage using Garage S3. This allows SEARCH to be efficient by only reading pre-computed information in metadata storage. At the moment there are no other significant o

Aerogramme 的内部数据架构将邮箱状态、电子邮件元数据和电子邮件内容分离开来。这在 Aerogramme 的文档中有简要描述。邮箱状态(电子邮件 ID 和标记)和电子邮件元数据(解析后的标题、日期、大小)使用 Garage K2V 存储在 Aerogramme 的元数据存储中,而电子邮件内容则使用 Garage S3 存储在 blob 存储中。这使得 SEARCH 可以通过仅读取元数据存储中的预计算信息来保持高效。目前没有其他重要的 o