email.mime
: 从头创建电子邮件和 MIME 对象¶
源代码: Lib/email/mime/
此模块是旧版 (Compat32
) 电子邮件 API 的组成部分。 它的功能在新版 API 中被 contentmanager
部分替代,但在某些应用中这些类仍可能有用,即使是在非旧版代码中。
通常,你是通过传递一个文件或一些文本到解析器来获得消息对象结构体的,解析器会解析文本并返回根消息对象。 不过你也可以从头开始构建一个完整的消息结构体,甚至是手动构建单独的 Message
对象。 实际上,你也可以接受一个现有的结构体并添加新的 Message
对象并移动它们。 这为切片和分割 MIME 消息提供了非常方便的接口。
你可以通过创建 Message
实例并手动添加附件和所有适当的标头来创建一个新的对象结构体。 不过对于 MIME 消息来说,email
包提供了一些便捷子类来让事情变得更容易。
这些类列示如下:
-
class
email.mime.base.
MIMEBase
(_maintype, _subtype, *, policy=compat32, **_params)¶ 模块:
email.mime.base
This is the base class for all the MIME-specific subclasses of
Message
. Ordinarily you won't create instances specifically ofMIMEBase
, although you could.MIMEBase
is provided primarily as a convenient base class for more specific MIME-aware subclasses._maintype is the Content-Type major type (e.g. text or image), and _subtype is the Content-Type minor type (e.g. plain or gif). _params is a parameter key/value dictionary and is passed directly to
Message.add_header
.If policy is specified, (defaults to the
compat32
policy) it will be passed toMessage
.The
MIMEBase
class always adds a Content-Type header (based on _maintype, _subtype, and _params), and a MIME-Version header (always set to1.0
).在 3.6 版更改: Added policy keyword-only parameter.
-
class
email.mime.nonmultipart.
MIMENonMultipart
¶ Module:
email.mime.nonmultipart
A subclass of
MIMEBase
, this is an intermediate base class for MIME messages that are not multipart. The primary purpose of this class is to prevent the use of theattach()
method, which only makes sense for multipart messages. Ifattach()
is called, aMultipartConversionError
exception is raised.
-
class
email.mime.multipart.
MIMEMultipart
(_subtype='mixed', boundary=None, _subparts=None, *, policy=compat32, **_params)¶ Module:
email.mime.multipart
A subclass of
MIMEBase
, this is an intermediate base class for MIME messages that are multipart. Optional _subtype defaults to 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.Optional 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
method.Optional policy argument defaults to
compat32
.Additional parameters for the Content-Type header are taken from the keyword arguments, or passed into the _params argument, which is a keyword dictionary.
在 3.6 版更改: Added policy keyword-only parameter.
-
class
email.mime.application.
MIMEApplication
(_data, _subtype='octet-stream', _encoder=email.encoders.encode_base64, *, policy=compat32, **_params)¶ Module:
email.mime.application
A subclass of
MIMENonMultipart
, theMIMEApplication
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.Optional _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 useget_payload()
andset_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 theemail.encoders
module for a list of the built-in encoders.Optional policy argument defaults to
compat32
._params are passed straight through to the base class constructor.
在 3.6 版更改: Added policy keyword-only parameter.
-
class
email.mime.audio.
MIMEAudio
(_audiodata, _subtype=None, _encoder=email.encoders.encode_base64, *, policy=compat32, **_params)¶ Module:
email.mime.audio
A subclass of
MIMENonMultipart
, theMIMEAudio
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 modulesndhdr
, 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, thenTypeError
is raised.Optional _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 useget_payload()
andset_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 theemail.encoders
module for a list of the built-in encoders.Optional policy argument defaults to
compat32
._params are passed straight through to the base class constructor.
在 3.6 版更改: Added policy keyword-only parameter.
-
class
email.mime.image.
MIMEImage
(_imagedata, _subtype=None, _encoder=email.encoders.encode_base64, *, policy=compat32, **_params)¶ Module:
email.mime.image
A subclass of
MIMENonMultipart
, theMIMEImage
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 moduleimghdr
, 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, thenTypeError
is raised.Optional _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 useget_payload()
andset_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 theemail.encoders
module for a list of the built-in encoders.Optional policy argument defaults to
compat32
._params are passed straight through to the
MIMEBase
constructor.在 3.6 版更改: Added policy keyword-only parameter.
-
class
email.mime.message.
MIMEMessage
(_msg, _subtype='rfc822', *, policy=compat32)¶ Module:
email.mime.message
A subclass of
MIMENonMultipart
, theMIMEMessage
class is used to create MIME objects of main type message. _msg is used as the payload, and must be an instance of classMessage
(or a subclass thereof), otherwise aTypeError
is raised.Optional _subtype sets the subtype of the message; it defaults to rfc822.
Optional policy argument defaults to
compat32
.在 3.6 版更改: Added policy keyword-only parameter.
-
class
email.mime.text.
MIMEText
(_text, _subtype='plain', _charset=None, *, policy=compat32)¶ Module:
email.mime.text
A subclass of
MIMENonMultipart
, theMIMEText
class is used to create MIME objects of major type text. _text is the string for the payload. _subtype is the minor type and defaults to plain. _charset is the character set of the text and is passed as an argument to theMIMENonMultipart
constructor; it defaults tous-ascii
if the string contains onlyascii
code points, andutf-8
otherwise. The _charset parameter accepts either a string or aCharset
instance.Unless the _charset argument is explicitly set to
None
, the MIMEText object created will have both a Content-Type header with acharset
parameter, and a Content-Transfer-Encoding header. This means that a subsequentset_payload
call will not result in an encoded payload, even if a charset is passed in theset_payload
command. You can "reset" this behavior by deleting theContent-Transfer-Encoding
header, after which aset_payload
call will automatically encode the new payload (and add a new Content-Transfer-Encoding header).Optional policy argument defaults to
compat32
.在 3.5 版更改: _charset also accepts
Charset
instances.在 3.6 版更改: Added policy keyword-only parameter.