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 creatingMessageinstances, 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)

模組:email.mime.base

This is the base class for all the MIME-specific subclasses ofMessage. Ordinarily you won't create instancesspecifically ofMIMEBase, although you could.MIMEBaseis 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 toMessage.add_header.

Ifpolicy is specified, (defaults to thecompat32 policy) it will be passed toMessage.

TheMIMEBase 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

模組:email.mime.nonmultipart

A subclass ofMIMEBase, 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)

模組:email.mime.multipart

A subclass ofMIMEBase, 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. WhenNone (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 theMessage.attach method.

Optionalpolicy argument defaults tocompat32.

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)

模組:email.mime.application

A subclass ofMIMENonMultipart, 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 istheMIMEApplication 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 tocompat32.

_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)

模組:email.mime.audio

A subclass ofMIMENonMultipart, 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 theMIMEAudio 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 tocompat32.

_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)

模組:email.mime.image

A subclass ofMIMENonMultipart, 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 theMIMEImage 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 tocompat32.

_params are passed straight through to theMIMEBaseconstructor.

在 3.6 版的變更:新增僅限關鍵字參數policy

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

模組:email.mime.message

A subclass ofMIMENonMultipart, 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 tocompat32.

在 3.6 版的變更:新增僅限關鍵字參數policy

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

模組:email.mime.text

A subclass ofMIMENonMultipart, 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 toNone, 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_payloadcommand. 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 tocompat32.

在 3.5 版的變更:_charset also acceptsCharset instances.

在 3.6 版的變更:新增僅限關鍵字參數policy