email.mime
:從頭開始建立電子郵件和 MIME 物件¶
原始碼:Lib/email/mime/
This module is part of the legacy (Compat32
) email API. Its functionalityis partially replaced by thecontentmanager
in the new API, butin certain applications these classes may still be useful, even in non-legacycode.
Ordinarily, you get a message object structure by passing a file or some text toa parser, which parses the text and returns the root message object. Howeveryou can also build a complete message structure from scratch, or even individualMessage
objects by hand. In fact, you can also take anexisting structure and add newMessage
objects, move themaround, etc. This makes a very convenient interface for slicing-and-dicing MIMEmessages.
You can create a new object structure by creatingMessage
instances, adding attachments and all the appropriate headers manually. For MIMEmessages though, theemail
package provides some convenient subclasses tomake things easier.
Here are the classes:
- classemail.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 instancesspecifically ofMIMEBase
, although you could.MIMEBase
is provided primarily as a convenient base class for more specificMIME-aware subclasses._maintype is theContent-Type major type (e.g.textorimage), and_subtype is theContent-Type minortype (e.g.plain orgif)._params is a parameterkey/value dictionary and is passed directly to
Message.add_header
.Ifpolicy is specified, (defaults to the
compat32
policy) it will be passed toMessage
.The
MIMEBase
class always adds aContent-Type header(based on_maintype,_subtype, and_params), and aMIME-Version header (always set to1.0
).在 3.6 版的變更:新增僅限關鍵字參數policy。
- classemail.mime.nonmultipart.MIMENonMultipart¶
A subclass of
MIMEBase
, this is an intermediate baseclass for MIME messages that are notmultipart. The primarypurpose of this class is to prevent the use of theattach()
method, which only makes sense formultipart messages. Ifattach()
is called, aMultipartConversionError
exception is raised.
- classemail.mime.multipart.MIMEMultipart(_subtype='mixed',boundary=None,_subparts=None,*,policy=compat32,**_params)¶
A subclass of
MIMEBase
, this is an intermediate baseclass for MIME messages that aremultipart. Optional_subtypedefaults tomixed, but can be used to specify the subtype of themessage. AContent-Type header ofmultipart/_subtypewill be added to the message object. AMIME-Version header willalso be added.Optionalboundary is the multipart boundary string. When
None
(thedefault), the boundary is calculated when needed (for example, when themessage is serialized)._subparts is a sequence of initial subparts for the payload. It must bepossible to convert this sequence to a list. You can always attach new subpartsto the message by using the
Message.attach
method.Optionalpolicy argument defaults to
compat32
.Additional parameters for theContent-Type header are taken fromthe keyword arguments, or passed into the_params argument, which is a keyworddictionary.
在 3.6 版的變更:新增僅限關鍵字參數policy。
- classemail.mime.application.MIMEApplication(_data,_subtype='octet-stream',_encoder=email.encoders.encode_base64,*,policy=compat32,**_params)¶
A subclass of
MIMENonMultipart
, theMIMEApplication
class is used to represent MIME message objects ofmajor typeapplication._data contains the bytes for the rawapplication data. Optional_subtype specifies the MIME subtype and defaultstooctet-stream.Optional_encoder is a callable (i.e. function) which will perform the actualencoding of the data for transport. This callable takes one argument, which isthe
MIMEApplication
instance. It should useget_payload()
andset_payload()
to change the payload to encodedform. It should also addanyContent-Transfer-Encoding or other headers to the messageobject as necessary. The default encoding is base64. See theemail.encoders
module for a list of the built-in encoders.Optionalpolicy argument defaults to
compat32
._params are passed straight through to the base class constructor.
在 3.6 版的變更:新增僅限關鍵字參數policy。
- classemail.mime.audio.MIMEAudio(_audiodata,_subtype=None,_encoder=email.encoders.encode_base64,*,policy=compat32,**_params)¶
A subclass of
MIMENonMultipart
, theMIMEAudio
class is used to create MIME message objects of major typeaudio._audiodata contains the bytes for the raw audio data. Ifthis data can be decoded as au, wav, aiff, or aifc, then thesubtype will be automatically included in theContent-Type header.Otherwise you can explicitly specify the audio subtype via the_subtypeargument. 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 actualencoding 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 encodedform. It should also addanyContent-Transfer-Encoding or other headers to the messageobject as necessary. The default encoding is base64. See theemail.encoders
module for a list of the built-in encoders.Optionalpolicy argument defaults to
compat32
._params are passed straight through to the base class constructor.
在 3.6 版的變更:新增僅限關鍵字參數policy。
- classemail.mime.image.MIMEImage(_imagedata,_subtype=None,_encoder=email.encoders.encode_base64,*,policy=compat32,**_params)¶
A subclass of
MIMENonMultipart
, theMIMEImage
class is used to create MIME message objects of major typeimage._imagedata contains the bytes for the raw image data. Ifthis data type can be detected (jpeg, png, gif, tiff, rgb, pbm, pgm, ppm,rast, xbm, bmp, webp, and exr attempted), then the subtype will beautomatically included in theContent-Type header. Otherwiseyou 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 actualencoding 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 encodedform. It should also addanyContent-Transfer-Encoding or other headers to the messageobject as necessary. The default encoding is base64. See theemail.encoders
module for a list of the built-in encoders.Optionalpolicy argument defaults to
compat32
._params are passed straight through to the
MIMEBase
constructor.在 3.6 版的變更:新增僅限關鍵字參數policy。
- classemail.mime.message.MIMEMessage(_msg,_subtype='rfc822',*,policy=compat32)¶
A subclass of
MIMENonMultipart
, theMIMEMessage
class is used to create MIME objects of main typemessage._msg is used as the payload, and must be an instanceof classMessage
(or a subclass thereof), otherwiseaTypeError
is raised.Optional_subtype sets the subtype of the message; it defaults torfc822.
Optionalpolicy argument defaults to
compat32
.在 3.6 版的變更:新增僅限關鍵字參數policy。
- classemail.mime.text.MIMEText(_text,_subtype='plain',_charset=None,*,policy=compat32)¶
A subclass of
MIMENonMultipart
, theMIMEText
class is used to create MIME objects of major typetext._text is the string for the payload._subtype is theminor type and defaults toplain._charset is the characterset of the text and is passed as an argument to theMIMENonMultipart
constructor; it defaultstous-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
, theMIMEText object created will have both aContent-Type headerwith acharset
parameter, and aContent-Transfer-Encodingheader. This means that a subsequentset_payload
call will not resultin 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
callwill automatically encode the new payload (and add a newContent-Transfer-Encoding header).Optionalpolicy argument defaults to
compat32
.在 3.5 版的變更:_charset also accepts
Charset
instances.在 3.6 版的變更:新增僅限關鍵字參數policy。