通常,通过将文件或一些文本传递给剖析器 (剖析文本并返回根消息对象) 获得消息对象结构。不管怎样,也可以从新构建完整消息结构,或者甚至单个 消息 对象通过手动。事实上,也可以采用现有结构和添加新的 消息 对象,移动它们,等等。这为切片和切块 MIME 消息提供了非常方便的接口。
可以创建新的对象结构通过创建 消息 实例,手动添加附件和所有合适 Header 头。但对于 MIME (多用途 Internet 邮件扩展) 消息, email 包提供了一些使事情变得更容易的方便子类。
这里是类:
模块: email.mime.base
这是基类对于所有 MIME 特定子类的 消息 。通常不会专门创建实例化的 MIMEBase ,虽然可以。 MIMEBase 首要作为更具体的 MIME 感知子类的方便基类而提供。
_maintype 是 Content-Type 主要类型 (如 text or image ),和 _subtype 是 Content-Type 次要类型 (如 plain or gif ). _params 是参数键/值字典,且被直接传递给 Message.add_header .
The MIMEBase 类始终添加 Content-Type 头 (基于 _maintype , _subtype ,和 _params ),和 MIME-Version 头 (始终设为 1.0 ).
模块: email.mime.nonmultipart
子类化的 MIMEBase ,这是 MIME 消息的中间基类不是 multipart 。此类的首要目的是防止使用 attach() 方法, 才有意义对于 multipart 消息。若 attach() 被调用, MultipartConversionError 异常被引发。
New in version 2.2.2.
模块: email.mime.multipart
子类化的 MIMEBase , this is an intermediate base class for MIME messages that are multipart 。可选 _subtype 默认为 mixed , but can be used to specify the subtype of the message. A Content-Type header of multipart/_subtype will be added to the message object. A MIME-Version header will also be added.
可选 boundary is the multipart boundary string. When None (the default), the boundary is calculated when needed (for example, when the message is serialized).
_subparts is a sequence of initial subparts for the payload. It must be possible to convert this sequence to a list. You can always attach new subparts to the message by using the Message.attach 方法。
额外参数对于 Content-Type header are taken from the keyword arguments, or passed into the _params argument, which is a keyword dictionary.
New in version 2.2.2.
模块: email.mime.application
子类化的 MIMENonMultipart , MIMEApplication class is used to represent MIME message objects of major type application . _data is a string containing the raw byte data. Optional _subtype specifies the MIME subtype and defaults to octet-stream .
可选 _encoder is a callable (i.e. function) which will perform the actual encoding of the data for transport. This callable takes one argument, which is the MIMEApplication instance. It should use get_payload() and set_payload() to change the payload to encoded form. It should also add any Content-Transfer-Encoding or other headers to the message object as necessary. The default encoding is base64. See the email.encoders 模块了解内置编码器的列表。
_params 被直接传递给基类构造函数。
2.5 版新增。
模块: email.mime.audio
子类化的 MIMENonMultipart , MIMEAudio class is used to create MIME message objects of major type audio . _audiodata is a string containing the raw audio data. If this data can be decoded by the standard Python module sndhdr , then the subtype will be automatically included in the Content-Type header. Otherwise you can explicitly specify the audio subtype via the _subtype parameter. If the minor type could not be guessed and _subtype was not given, then TypeError 被引发。
可选 _encoder is a callable (i.e. function) which will perform the actual encoding of the audio data for transport. This callable takes one argument, which is the MIMEAudio instance. It should use get_payload() and set_payload() to change the payload to encoded form. It should also add any Content-Transfer-Encoding or other headers to the message object as necessary. The default encoding is base64. See the email.encoders 模块了解内置编码器的列表。
_params 被直接传递给基类构造函数。
模块: email.mime.image
子类化的 MIMENonMultipart , MIMEImage class is used to create MIME message objects of major type image . _imagedata is a string containing the raw image data. If this data can be decoded by the standard Python module imghdr , then the subtype will be automatically included in the Content-Type header. Otherwise you can explicitly specify the image subtype via the _subtype parameter. If the minor type could not be guessed and _subtype was not given, then TypeError 被引发。
可选 _encoder is a callable (i.e. function) which will perform the actual encoding of the image data for transport. This callable takes one argument, which is the MIMEImage instance. It should use get_payload() and set_payload() to change the payload to encoded form. It should also add any Content-Transfer-Encoding or other headers to the message object as necessary. The default encoding is base64. See the email.encoders 模块了解内置编码器的列表。
_params 被直接传递给 MIMEBase 构造函数。
模块: email.mime.message
子类化的 MIMENonMultipart , MIMEMessage class is used to create MIME objects of main type message . _msg is used as the payload, and must be an instance of class 消息 (or a subclass thereof), otherwise a TypeError 被引发。
可选 _subtype 设置消息的子类型;默认为 rfc822 .
模块: email.mime.text
子类化的 MIMENonMultipart , MIMEText 类用于创建 MIME (多用途 Internet 邮件扩展) 对象对于主要类型 text . _text 是负载字符串。 _subtype 是次要类型且默认为 plain . _charset is the character set of the text and is passed as a parameter to the MIMENonMultipart 构造函数;默认为 us-ascii 。若 _text is unicode, it is encoded using the output_charset of _charset , otherwise it is used as-is.
2.4 版改变: The previously deprecated _encoding argument has been removed. Content Transfer Encoding now happens implicitly based on the _charset 自变量。
除非 _charset parameter is explicitly set to None ,创建的 MIMEText 对象将拥有 Content-Type 头采用 charset 参数,和 Content-Transfer-Endcoding 头。这意味着后续 set_payload 调用不会产生编码负载,即使字符集被传入 set_payload 命令。可以 reset 此行为通过删除 Content-Transfer-Encoding 头,之后 set_payload 调用将自动编码新的负载 (和添加新的 Content-Transfer-Encoding 头)。