RFC 2822 是描述 Email 消息格式的基标准。派生自旧 RFC 822 标准曾被广泛使用,当大多数 Email 仅以 ASCII 字符组成时。 RFC 2822 是假定 Email 仅包含 7 位 ASCII 字符而编写的规范。
当然,随着 Email 全世界部署,它已变得国际化,这样,特定语言字符集现在可以在 Email 消息中使用。基标准仍然要求仅使用 7 位 ASCII 字符传输 Email 消息,因此,已编写大量 RFC 描述如何将包含非 ASCII 字符的 Email 编码成 RFC 2822 兼容格式。这些 RFC 包括 RFC 2045 , RFC 2046 , RFC 2047 ,和 RFC 2231 。 email 包支持这些标准在其 email.header and email.charset 模块。
若想要在 Email 头中包括非 ASCII 字符,请在 Subject or 到 字段,应该使用 头 类和赋值字段在 消息 对象到实例化的 头 而不是使用字符串对于头值。导入 头 类从 email.header 模块。例如:
>>> from email.message import Message >>> from email.header import Header >>> msg = Message() >>> h = Header('p\xf6stal', 'iso-8859-1') >>> msg['Subject'] = h >>> print msg.as_string() Subject: =?iso-8859-1?q?p=F6stal?=
预告,这里想要 Subject 字段包含非 ASCII 字符?做到这通过创建 头 实例并传入被字符集编码的字节字符串。当后续 消息 实例扁平化, Subject 字段被正确 RFC 2047 编码。MIME (多用途 Internet 邮件扩展) 感知邮件阅读器将使用嵌入的 ISO-8859-1 字符展示此头。
New in version 2.2.2.
这里是 头 类描述:
创建可以包含按不同字符集的字符串的 MIME (多用途 Internet 邮件扩展) 兼容头。
可选 s 是初始 Header 头值。若 None (默认),不设置初始头值。可以稍后追加到头采用 append() 方法调用。 s may be a byte string or a Unicode string, but see the append() 文档编制了解语义。
可选 charset 服务于 2 个目的:它拥有的含义如同 charset 自变量到 append() 方法。它还设置默认字符集为所有后续 append() 调用省略 charset 自变量。若 charset 未在构造函数中提供 (默认), us-ascii 字符集将用作 s ‘s initial charset and as the default for subsequent append() 调用。
最大行长度可以明确指定凭借 maxlinelen 。为将首行分割成更短值 (考虑到字段头未包括在 s ,如 Subject ) 传入字段名称在 header_name 。默认 maxlinelen 为 76,且默认值对于 header_name is None ,意味着不考虑长的、分割头的首行。
可选 continuation_ws 必须为 RFC 2822 兼容折叠空白,且通常是空格或硬 Tab 字符。此字符将前置到连续行。 continuation_ws defaults to a single space character (” ”).
可选 errors 被直接传递给 append() 方法。
追加字符串 s 到 MIME (多用途 Internet 邮件扩展) 头。
可选 charset ,若给定,应为 Charset 实例 (见 email.charset ) 或字符集的名称,将被转换成 Charset 实例。值为 None (默认) 意味着 charset 使用在构造函数中给出的。
s may be a byte string or a Unicode string. If it is a byte string (i.e. isinstance(s, str) is true), then charset 是该字节字符串的编码,和 UnicodeError 会被引发若字符串无法采用该字符集解码。
若 s is a Unicode string, then charset is a hint specifying the character set of the characters in the string. In this case, when producing an RFC 2822 -compliant header using RFC 2047 rules, the Unicode string will be encoded using the following charsets in order: us-ascii , charset hint, utf-8 . The first character set to not provoke a UnicodeError 被使用。
可选 errors is passed through to any unicode() or unicode.encode() call, and defaults to “strict”.
Encode a message header into an RFC-compliant format, possibly wrapping long lines and encapsulating non-ASCII parts in base64 or quoted-printable encodings. Optional splitchars is a string containing characters to split long ASCII lines on, in rough support of RFC 2822 ‘s highest level syntactic breaks . This doesn’t affect RFC 2047 encoded lines.
The 头 class 还提供支持标准运算符和内置函数的许多方法。
同义词 Header.encode() . Useful for str(aHeader) .
A helper for the built-in unicode() function. Returns the header as a Unicode string.
The email.header 模块还提供下列方便函数。
Decode a message header value without converting the character set. The header value is in header .
此函数返回列表对于 (decoded_string, charset) pairs containing each of the decoded parts of the header. charset is None for non-encoded parts of the header, otherwise a lower case string containing the name of the character set specified in the encoded string.
这里是范例:
>>> from email.header import decode_header >>> decode_header('=?iso-8859-1?q?p=F6stal?=') [('p\xf6stal', 'iso-8859-1')]
创建 头 instance from a sequence of pairs as returned by decode_header() .
decode_header() takes a header value string and returns a sequence of pairs of the format (decoded_string, charset) where charset is the name of the character set.
This function takes one of those sequence of pairs and returns a 头 实例。可选 maxlinelen , header_name ,和 continuation_ws are as in the 头 构造函数。