email.mime: Creating email and MIME objects from scratch¶
Source code: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:
- class
email.mime.base.MIMEBase(_maintype,_subtype,*,policy=compat32,**_params)¶ Module:
email.mime.baseThis is the base class for all the MIME-specific subclasses of
Message. 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 to
Message.add_header.Ifpolicy is specified, (defaults to the
compat32policy) it will be passed toMessage.The
MIMEBaseclass always adds aContent-Type header(based on_maintype,_subtype, and_params), and aMIME-Version header (always set to1.0).Changed in version 3.6:Addedpolicy keyword-only parameter.
- class
email.mime.nonmultipart.MIMENonMultipart¶ Module:
email.mime.nonmultipartA 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, aMultipartConversionErrorexception is raised.
- class
email.mime.multipart.MIMEMultipart(_subtype='mixed',boundary=None,_subparts=None,*,policy=compat32,**_params)¶ Module:
email.mime.multipartA 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.attachmethod.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.
Changed in version 3.6:Addedpolicy keyword-only parameter.
- class
email.mime.application.MIMEApplication(_data,_subtype='octet-stream',_encoder=email.encoders.encode_base64,*,policy=compat32,**_params)¶ Module:
email.mime.applicationA subclass of
MIMENonMultipart, theMIMEApplicationclass is used to represent MIME message objects ofmajor typeapplication._data is a string containing the rawbyte data. Optional_subtype specifies the MIME subtype and defaults tooctet-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
MIMEApplicationinstance. 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.encodersmodule for a list of the built-in encoders.Optionalpolicy argument defaults to
compat32._params are passed straight through to the base class constructor.
Changed in version 3.6:Addedpolicy keyword-only parameter.
- class
email.mime.audio.MIMEAudio(_audiodata,_subtype=None,_encoder=email.encoders.encode_base64,*,policy=compat32,**_params)¶ Module:
email.mime.audioA subclass of
MIMENonMultipart, theMIMEAudioclass is used to create MIME message objects of major typeaudio._audiodata is a string containing the raw audio data. Ifthis data can be decoded by the standard Python modulesndhdr, 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,thenTypeErroris 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
MIMEAudioinstance. 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.encodersmodule for a list of the built-in encoders.Optionalpolicy argument defaults to
compat32._params are passed straight through to the base class constructor.
Changed in version 3.6:Addedpolicy keyword-only parameter.
- class
email.mime.image.MIMEImage(_imagedata,_subtype=None,_encoder=email.encoders.encode_base64,*,policy=compat32,**_params)¶ Module:
email.mime.imageA subclass of
MIMENonMultipart, theMIMEImageclass is used to create MIME message objects of major typeimage._imagedata is a string containing the raw image data. Ifthis data can be decoded by the standard Python moduleimghdr, then thesubtype will be automatically included in theContent-Type header.Otherwise you can explicitly specify the image subtype via the_subtypeargument. 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 actualencoding of the image data for transport. This callable takes one argument,which is the
MIMEImageinstance. 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.encodersmodule for a list of the built-in encoders.Optionalpolicy argument defaults to
compat32._params are passed straight through to the
MIMEBaseconstructor.Changed in version 3.6:Addedpolicy keyword-only parameter.
- class
email.mime.message.MIMEMessage(_msg,_subtype='rfc822',*,policy=compat32)¶ Module:
email.mime.messageA subclass of
MIMENonMultipart, theMIMEMessageclass 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), otherwiseaTypeErroris raised.Optional_subtype sets the subtype of the message; it defaults torfc822.
Optionalpolicy argument defaults to
compat32.Changed in version 3.6:Addedpolicy keyword-only parameter.
- class
email.mime.text.MIMEText(_text,_subtype='plain',_charset=None,*,policy=compat32)¶ Module:
email.mime.textA subclass of
MIMENonMultipart, theMIMETextclass 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 theMIMENonMultipartconstructor; it defaultstous-asciiif the string contains onlyasciicode points, andutf-8otherwise. The_charset parameter accepts either a string or aCharsetinstance.Unless the_charset argument is explicitly set to
None, theMIMEText object created will have both aContent-Type headerwith acharsetparameter, and aContent-Transfer-Encodingheader. This means that a subsequentset_payloadcall will not resultin an encoded payload, even if a charset is passed in theset_payloadcommand. You can “reset” this behavior by deleting theContent-Transfer-Encodingheader, after which aset_payloadcallwill automatically encode the new payload (and add a newContent-Transfer-Encoding header).Optionalpolicy argument defaults to
compat32.Changed in version 3.5:_charset also accepts
Charsetinstances.Changed in version 3.6:Addedpolicy keyword-only parameter.