email.mime:從頭開始建立電子郵件和 MIME 物件¶
原始碼:Lib/email/mime/
This module is part of the legacy (Compat32) email API. Its functionality
is partially replaced by the contentmanager in the new API, but
in certain applications these classes may still be useful, even in non-legacy
code.
Ordinarily, you get a message object structure by passing a file or some text to
a parser, which parses the text and returns the root message object. However
you can also build a complete message structure from scratch, or even individual
Message objects by hand. In fact, you can also take an
existing structure and add new Message objects, move them
around, etc. This makes a very convenient interface for slicing-and-dicing MIME
messages.
You can create a new object structure by creating Message
instances, adding attachments and all the appropriate headers manually. For MIME
messages though, the email package provides some convenient subclasses to
make things easier.
Here are the classes:
- class email.mime.base.MIMEBase(_maintype, _subtype, *, policy=compat32, **_params)¶
-
This is the base class for all the MIME-specific subclasses of
Message. Ordinarily you won't create instances specifically ofMIMEBase, although you could.MIMEBaseis 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
compat32policy) it will be passed toMessage.The
MIMEBaseclass always adds a Content-Type header (based on _maintype, _subtype, and _params), and a MIME-Version header (always set to1.0).在 3.6 版的變更: 新增僅限關鍵字參數 policy。
- class email.mime.nonmultipart.MIMENonMultipart¶
-
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, aMultipartConversionErrorexception is raised.
- class email.mime.multipart.MIMEMultipart(_subtype='mixed', boundary=None, _subparts=None, *, policy=compat32, **_params)¶
-
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.attachmethod.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 版的變更: 新增僅限關鍵字參數 policy。
- class email.mime.application.MIMEApplication(_data, _subtype='octet-stream', _encoder=email.encoders.encode_base64, *, policy=compat32, **_params)¶
-
A subclass of
MIMENonMultipart, theMIMEApplicationclass is used to represent MIME message objects of major type application. _data contains the bytes for the raw application 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
MIMEApplicationinstance. 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.encodersmodule 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 版的變更: 新增僅限關鍵字參數 policy。
- class email.mime.audio.MIMEAudio(_audiodata, _subtype=None, _encoder=email.encoders.encode_base64, *, policy=compat32, **_params)¶
-
A subclass of
MIMENonMultipart, theMIMEAudioclass is used to create MIME message objects of major type audio. _audiodata contains the bytes for 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, thenTypeErroris 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