A bit about email
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
A bit about email
A bit about email 2026-11-06 Before you torch and pitchfork me, I’m aware most of this is simplified and doesn’t go into edge cases, even though the internet is composed entirely of edge cases. Otherwise, I’d be writing a whole book. First, some terminology; A typical email will past through many different components on it’s way to the recipient: 1. Sender’s MUA [Via SMTP on port 465] 2. Sending MSA 3. Sending MTA [Via SMTP on port 25] 4. Receiving MTA 5. Receiving LDA and message store [Via IMAP] 7. Receiver’s MUA (These aren’t all necessarily different machines or even different programs, but it is helpful to give a name for each step.)
关于电子邮件 2026-11-06 在你对我口诛笔伐之前,我得说明,大部分内容为了简化而略去了边缘情况,尽管互联网完全是由边缘情况组成的。否则,我就得写一整本书了。首先,介绍一些术语;一封典型的电子邮件在到达收件人之前会经过许多不同的组件:1. 发件人的 MUA [通过端口 465 上的 SMTP] 2. 发送方 MSA 3. 发送方 MTA [通过端口 25 上的 SMTP] 4. 接收方 MTA 5. 接收方 LDA 和消息存储 [通过 IMAP] 7. 收件人的 MUA(这些并不一定都是不同的机器甚至不同的程序,但为每个步骤命名是有帮助的。)
The mail user agent (MUA) runs on the user’s computer and is what they use to send or read messages. Let’s say they send something: that message will be uploaded to to the submission agent (MSA) which checks the user’s password before handing off to the mail transfer agent (MTA). The transfer agent’s job is to talk to the other mail servers and get the message to the receiving side’s MTA. Once received, it’s given to the local delivery agent (LDA) to be stored until the recipient checks their email. The name “LDA” is non-standard, but I use it to avoid having four different abbreviations differing by a single letter.
邮件用户代理 (MUA) 在用户的计算机上运行,是用户用来发送或阅读邮件的工具。假设用户发送了一封邮件:该消息会被上传到提交代理 (MSA),它会在移交给邮件传输代理 (MTA) 之前检查用户的密码。传输代理的工作是与其它邮件服务器通信,并将消息发送到接收方的 MTA。一旦接收到,消息会被交给本地投递代理 (LDA) 进行存储,直到收件人查收邮件。“LDA”这个名称并非标准用法,但我使用它是为了避免出现四个仅有一个字母之差的缩写。
The reason for this structure is that when the system was being designed (around 1980), most organizations could only afford a single server. Maintenance was a full time job and updates often required multiple days of downtime. A well-connected computer might have dedicated single-digits megabit/second link that worked half the time. This forced the MTA and MUA distinction: if the recipient’s server isn’t up when the email is set, the sending MTA will save the message and re-attempt delivery later. This way, short outages don’t cause bounced mail. Since the mail client is an interactive program the user runs, a separate message store is needed to keep the email until it can be read.
这种结构的原因在于,当系统被设计时(大约 1980 年左右),大多数组织只能负担得起一台服务器。维护是一项全职工作,更新往往需要数天的停机时间。一台连接良好的计算机可能拥有专用的个位数兆比特/秒的链路,而且还只能有一半时间正常工作。这迫使人们区分了 MTA 和 MUA:如果收件人的服务器在邮件发送时未开启,发送方的 MTA 会保存该消息并在稍后重试投递。这样,短暂的故障就不会导致邮件被退回。由于邮件客户端是用户运行的交互式程序,因此需要一个单独的消息存储空间来保存邮件,直到用户读取。
Most MTAs are configured to continue retrying a message for five days, usually with exponential backoff: mine will wait 5 minutes after the first failure, and increase the interval to one hour during prolonged outages. The MSA and LDA have always existed in some capacity, but have gotten a lot more complicated when people started accessing email from their own computers instead or remotely logging onto the server.
大多数 MTA 被配置为持续重试投递消息五天,通常采用指数退避算法:我的服务器在第一次失败后会等待 5 分钟,并在长时间故障期间将间隔增加到一小时。MSA 和 LDA 在某种程度上一直存在,但当人们开始从自己的计算机访问电子邮件,或者远程登录到服务器时,它们变得复杂多了。
SMTP Most communication happens over the Simple Mail Transfer Protocol, including MTA-MTA delivery. To narrow it down a bit, I’ll pretend do be an MTA sending email to blog@maurycyz.com. The first step is to look up the receiving MTA for that domain: # dig MX maurycyz.com [snip] maurycyz.com. 300 IN MX 1 smallcrow.com. [snip] # dig A smallcrow.com [snip] smallcrow.com. 300 IN A 65.109.172.162 [snip] Email is old enough to have it’s own dedicated DNS record instead of using subdomain hacks or TXT abuse. The number is a priority (smaller is higher), which is used if multiple hosts are specified: a big email provider will have multiple servers to ensure timely delivery.
SMTP 大多数通信通过简单邮件传输协议 (SMTP) 进行,包括 MTA 到 MTA 的投递。为了缩小范围,我将假装自己是一个向 blog@maurycyz.com 发送邮件的 MTA。第一步是查找该域名的接收方 MTA:# dig MX maurycyz.com [省略] maurycyz.com. 300 IN MX 1 smallcrow.com. [省略] # dig A smallcrow.com [省略] smallcrow.com. 300 IN A 65.109.172.162 [省略]。电子邮件的历史足够悠久,拥有自己专用的 DNS 记录,而无需使用子域名黑客手段或滥用 TXT 记录。数字代表优先级(越小优先级越高),当指定了多个主机时会用到它:大型电子邮件提供商会有多台服务器以确保及时投递。
SMTP is a relatively straightforward text based protocol. The one catch is that most mail servers will require a CRLF as a data terminator. This can often be sent with ctrl-m, but using a tool that automatically inserts cartridge-return bytes is easier: # telnet telnet> set crlf Will send carriage returns as telnet
SMTP 是一种相对直观的基于文本的协议。唯一需要注意的是,大多数邮件服务器要求使用 CRLF 作为数据终止符。这通常可以通过 ctrl-m 发送,但使用能自动插入回车字节的工具会更容易:# telnet telnet> set crlf 将以 telnet
Each line sent is a command, to which the remote server responds with a three digit number followed by human readable text. 200 responses mean it worked, 300 is “carry on…”, 400 is temporary errors which tell the sender to try again later, 500 permanent errors mean the email should be returned as un-deliverable. (If this seems similar to HTTP, that’s no coincidence. A lot of the web, including its header syntax was lifted from SMTP)
发送的每一行都是一个命令,远程服务器会以一个三位数字后跟人类可读文本的方式进行响应。200 响应表示成功,300 表示“继续…”,400 表示临时错误,告诉发件人稍后再试,500 表示永久错误,意味着邮件应被退回为不可投递。(如果这看起来像 HTTP,那并非巧合。网络的很多部分,包括其头部语法,都是从 SMTP 借鉴来的。)
The first command is always HELO (or EHLO), followed the sending MTA’s hostname. This is usually not the same hostname as used in email addresses, but many provider require it to match the sender’s IP address. Mine doesn’t, so if you want to try this, put something random that’s not a resolvable domain. The next two, MAIL FROM and RCPT TO specify the address to deliver to, and address to forward any bounce notifications to. Bounce messages are usually generated one step up from where the error occurred: in most cases the receiving MTA has a problem and sends an error code to the sending MTA, but by that time MSA has already, forcing a un-delivered mail notification. Those notification themselves use an empty MAIL FROM to avoid infinite loops.
第一个命令始终是 HELO(或 EHLO),后跟发送方 MTA 的主机名。这通常与电子邮件地址中使用的域名不同,但许多提供商要求它与发件人的 IP 地址匹配。我的服务器不需要,所以如果你想尝试,可以随便填一个无法解析的域名。接下来的两个命令,MAIL FROM 和 RCPT TO,分别指定了投递地址以及转发退信通知的地址。退信消息通常在错误发生位置的上一步生成:在大多数情况下,接收方 MTA 出现问题并向发送方 MTA 发送错误代码,但此时 MSA 已经处理完毕,从而被迫发出未投递邮件通知。这些通知本身使用空的 MAIL FROM 以避免无限循环。
Once the address has been specified and accepted, a DATA command is sent, followed by the actual email message. This at a minimum, contains the “From” and “To” headers along with some text. The message is terminated with a lone period to which the server indicates that the message is handed off. Final hop MTA’s should forward the email to the LDA before sending back a status code. This ensures that they never accept a message which they can’t deliver which would result in a error notification. This “backscatter” message interacts badly with spammers that forge MAIL FROM: it turns their obviously bogus message into one that is likely to be delivered and cause annoyance. There is no requirement for the addresses inside the email to match the ones in MAIL FROM and RCPT TO (also known as the envelope). The most common cause of this is a mail that has been forwarded. For example, a mailing list will set the envelope destination to match each member’s email, but keep the original “From” header intact so that
一旦地址被指定并接受,就会发送 DATA 命令,随后是实际的电子邮件内容。这至少包含“From”和“To”头部以及一些文本。消息以一个单独的句点结束,服务器据此表示消息已移交。最后一跳的 MTA 应在返回状态码之前将邮件转发给 LDA。这确保了它们永远不会接受无法投递的消息,从而避免产生错误通知。这种“反向散射”消息与伪造 MAIL FROM 的垃圾邮件发送者交互不良:它将他们明显的虚假消息变成了可能被投递并造成困扰的消息。电子邮件内部的地址没有要求必须与 MAIL FROM 和 RCPT TO(也称为信封)中的地址匹配。这种情况最常见的原因是邮件被转发了。例如,邮件列表会将信封目标地址设置为匹配每个成员的电子邮件,但保持原始的“From”头部完整,以便…