<p>Python提供了一个电子邮件包,可以为您完成这些低级任务,但是如果您想通过艰苦的方式学习电子邮件头,那么可以参考<a href="https://tools.ietf.org/html/rfc5322" rel="nofollow">RFC5322</a>(格式为RFC822)</p>
<p>在其他合理的信息中,您可以找到标题字段的定义:</p>
<blockquote>
<p>Header fields are lines beginning with a field name, followed by a
colon (":"), followed by a field body, and terminated by CRLF. A
field name MUST be composed of printable US-ASCII characters (i.e.,
characters that have values between 33 and 126, inclusive), except
colon. A field body may be composed of printable US-ASCII characters
as well as the space (SP, ASCII value 32) and horizontal tab (HTAB,
ASCII value 9) characters (together known as the white space
characters, WSP). A field body MUST NOT include CR and LF except
when used in "folding" and "unfolding"</p>
</blockquote>
<p>折叠的定义如下:</p>
<blockquote>
<p>the field body portion of a header field can be split into a
multiple-line representation; this is called "folding". The general
rule is that wherever this specification allows for folding white
space (not simply WSP characters), a CRLF may be inserted before any
WSP.</p>
</blockquote>
<p>这意味着:</p>
<ul>
<li>当一行不以WSP开头(在regex中,<code>\s</code>)时,列的起始行是头名称。在</li>
<li>当一行以WSP开头时,它是一个延续行。在</li>
</ul>
<p>所以这个regex就足够了:<code>'([\x21-\x7e]+?):'</code></p>