email.mime : 从新创建 Email 和 MIME 对象

源代码: Lib/email/mime/


此模块属于传统 ( Compat32 ) 电子邮件 API。其功能被部分替换由 contentmanager 在新 API 中,但在某些应用程序中这些类可能仍然很有用,即使在非传统代码中。

通常,通过将文件或一些文本传递给剖析器 (剖析文本并返回根消息对象) 获得消息对象结构。不管怎样,也可以从新构建完整消息结构,或者甚至单个 Message 对象通过手动。事实上,也可以采用现有结构和添加新的 Message 对象,移动它们,等等。这为切片和切块 MIME 消息提供了非常方便的接口。

可以创建新的对象结构通过创建 Message 实例,手动添加附件和所有合适 Header 头。但对于 MIME (多用途 Internet 邮件扩展) 消息, email 包提供了一些使事情变得更容易的方便子类。

这里是类:

class email.mime.base. MIMEBase ( _maintype , _subtype , * , policy = compat32 , ** _params )

模块: email.mime.base

这是基类对于所有 MIME 特定子类的 Message 。通常不会专门创建实例化的 MIMEBase ,虽然可以。 MIMEBase 首要作为更具体的 MIME 感知子类的方便基类而提供。

_maintype Content-Type 主要类型 (如 text or image ),和 _subtype Content-Type 次要类型 (如 plain or gif ). _params 是参数键/值字典,且被直接传递给 Message.add_header .

policy 有指定,(默认为 compat32 策略) 将传递给 Message .

MIMEBase 类始终添加 Content-Type 头 (基于 _maintype , _subtype ,和 _params ),和 MIME-Version 头 (始终设为 1.0 ).

3.6 版改变: 添加 policy 仅关键词参数。

class email.mime.nonmultipart. MIMENonMultipart

模块: email.mime.nonmultipart

子类化的 MIMEBase ,这是 MIME 消息的中间基类不是 multipart 。此类的首要目的是防止使用 attach() 方法, 才有意义对于 multipart 消息。若 attach() 被调用, MultipartConversionError 异常被引发。

class email.mime.multipart. MIMEMultipart ( _subtype = 'mixed' , boundary = None , _subparts = None , * , policy = compat32 , ** _params )

模块: 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 方法。

可选 policy 自变量默认为 compat32 .

额外参数对于 Content-Type header are taken from the keyword arguments, or passed into the _params argument, which is a keyword dictionary.

3.6 版改变: 添加 policy 仅关键词参数。

class email.mime.application. MIMEApplication ( _data , _subtype = 'octet-stream' , _encoder = email.encoders.encode_base64 , * , policy = compat32 , ** _params )

模块: 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 模块了解内置编码器的列表。

可选 policy 自变量默认为 compat32 .

_params 被直接传递给基类构造函数。

3.6 版改变: 添加 policy 仅关键词参数。

class email.mime.audio. MIMEAudio ( _audiodata , _subtype = None , _encoder = email.encoders.encode_base64 , * , policy = compat32 , ** _params )

模块: 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 as au, wav, aiff, or aifc, then the subtype will be automatically included in the Content-Type header. Otherwise you can explicitly specify the audio subtype via the _subtype argument. 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 模块了解内置编码器的列表。

可选 policy 自变量默认为 compat32 .

_params 被直接传递给基类构造函数。

3.6 版改变: 添加 policy 仅关键词参数。

class email.mime.image. MIMEImage ( _imagedata , _subtype = None , _encoder = email.encoders.encode_base64 , * , policy = compat32 , ** _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 type can be detected (jpeg, png, gif, tiff, rgb, pbm, pgm, ppm, rast, xbm, bmp, webp, and exr attempted), then the subtype will be automatically included in the Content-Type header. Otherwise you can explicitly specify the image subtype via the _subtype argument. 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 模块了解内置编码器的列表。

可选 policy 自变量默认为 compat32 .

_params 被直接传递给 MIMEBase 构造函数。

3.6 版改变: 添加 policy 仅关键词参数。

class email.mime.message. MIMEMessage ( _msg , _subtype = 'rfc822' , * , policy = compat32 )

模块: 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 Message (or a subclass thereof), otherwise a TypeError 被引发。

可选 _subtype 设置消息的子类型;默认为 rfc822 .

可选 policy 自变量默认为 compat32 .

3.6 版改变: 添加 policy 仅关键词参数。

class email.mime.text. MIMEText ( _text , _subtype = 'plain' , _charset = None , * , policy = compat32 )

模块: email.mime.text

子类化的 MIMENonMultipart MIMEText 类用于创建 MIME (多用途 Internet 邮件扩展) 对象对于主要类型 text . _text 是负载字符串。 _subtype 是次要类型且默认为 plain . _charset 是文本的字符集且作为自变量被传递给 MIMENonMultipart 构造函数;默认为 us-ascii 若字符串只包含 ascii 代码点,和 utf-8 否则。 _charset 参数接受字符串或 Charset 实例。

除非 _charset 自变量被明确设为 None ,创建的 MIMEText 对象将拥有 Content-Type 头采用 charset 参数,和 Content-Transfer-Encoding 头。这意味着后续 set_payload 调用不会产生编码负载,即使字符集被传入 set_payload 命令。可以 reset 此行为通过删除 Content-Transfer-Encoding 头,之后 set_payload 调用将自动编码新的负载 (和添加新的 Content-Transfer-Encoding 头)。

可选 policy 自变量默认为 compat32 .

3.5 版改变: _charset 还接受 Charset 实例。

3.6 版改变: 添加 policy 仅关键词参数。

上一话题

email.message.Message :表示 Email 消息使用 compat32 API

下一话题

email.header :国际化头

本页