19.1.6.email.contentmanager: Managing MIME Content¶
New in version 3.4:as aprovisional module.
Source code:Lib/email/contentmanager.py
Note
The contentmanager module has been included in the standard library on aprovisional basis. Backwards incompatiblechanges (up to and including removal of the module) may occur if deemednecessary by the core developers.
Themessage module provides a class that can represent anarbitrary email message. That basic message model has a useful and flexibleAPI, but it provides only a lower-level API for interacting with the genericparts of a message (the headers, generic header parameters, and the payload,which may be a list of sub-parts). This module provides classes and toolsthat provide an enhanced and extensible API for dealing with various specifictypes of content, including the ability to retrieve the content of the messageas a specialized object type rather than as a simple bytes object. The moduleautomatically takes care of the RFC-specified MIME details (required headersand parameters, etc.) for the certain common content types content properties,and support for additional types can be added by an application using theextension mechanisms.
This module defines the eponymous “Content Manager” classes. The baseContentManager class defines an API for registering contentmanagement functions which extract data fromMessage objects or insert dataand headers intoMessage objects, thus providing a way of convertingbetweenMessage objects containing data and other representations of thatdata (Python data types, specialized Python objects, external files, etc). Themodule also defines one concrete content manager:raw_data_managerconverts between MIME content types andstr orbytes data. It alsoprovides a convenient API for managing the MIME parameters when insertingcontent intoMessages. It also handles inserting and extractingMessage objects when dealing with themessage/rfc822 content type.
Another part of the enhanced interface is subclasses ofMessage that provide new convenience API functions,including convenience methods for calling the Content Managers derived fromthis module.
Note
AlthoughEmailMessage andMIMEPart are currentlydocumented in this module because of the provisional nature of the code, theimplementation lives in theemail.message module.
- class
email.message.EmailMessage(policy=default)¶ Ifpolicy is specified (it must be an instance of a
policyclass) use the rules it specifies to update and serialize the representationof the message. Ifpolicy is not set, use thedefaultpolicy, which follows the rules of the emailRFCs except for line endings (instead of the RFC mandated\r\n, it usesthe Python standard\nline endings). For more information see thepolicydocumentation.This class is a subclass of
Message. It addsthe following methods:is_attachment()¶Return
Trueif there is aContent-Disposition headerand its (case insensitive) value isattachment,Falseotherwise.Changed in version 3.4.2:is_attachment is now a method instead of a property, for consistencywith
is_multipart().
get_body(preferencelist=('related','html','plain'))¶Return the MIME part that is the best candidate to be the “body” of themessage.
preferencelist must be a sequence of strings from the set
related,html, andplain, and indicates the order of preference for thecontent type of the part returned.Start looking for candidate matches with the object on which the
get_bodymethod is called.If
relatedis not included inpreferencelist, consider the rootpart (or subpart of the root part) of any related encountered as acandidate if the (sub-)part matches a preference.When encountering a
multipart/related, check thestartparameterand if a part with a matchingContent-ID is found, consideronly it when looking for candidate matches. Otherwise consider only thefirst (default root) part of themultipart/related.If a part has aContent-Disposition header, only considerthe part a candidate match if the value of the header is
inline.If none of the candidates matches any of the preferences inpreferencelist, return
None.Notes: (1) For most applications the onlypreferencelist combinationsthat really make sense are
('plain',),('html','plain'), and thedefault,('related','html','plain'). (2) Because matching startswith the object on whichget_bodyis called, callingget_bodyonamultipart/relatedwill return the object itself unlesspreferencelist has a non-default value. (3) Messages (or message parts)that do not specify aContent-Type or whoseContent-Type header is invalid will be treated as if theyare of typetext/plain, which may occasionally causeget_bodytoreturn unexpected results.
iter_attachments()¶Return an iterator over all of the parts of the message that are notcandidate “body” parts. That is, skip the first occurrence of each of
text/plain,text/html,multipart/related, ormultipart/alternative(unless they are explicitly marked asattachments viaContent-Disposition: attachment), andreturn all remaining parts. When applied directly to amultipart/related, return an iterator over the all the related partsexcept the root part (ie: the part pointed to by thestartparameter,or the first part if there is nostartparameter or thestartparameter doesn’t match theContent-ID of any of theparts). When applied directly to amultipart/alternativeor anon-multipart, return an empty iterator.
iter_parts()¶Return an iterator over all of the immediate sub-parts of the message,which will be empty for a non-
multipart. (See alsowalk().)
get_content(*args,content_manager=None,**kw)¶Call the
get_contentmethod of thecontent_manager, passing selfas the message object, and passing along any other arguments or keywordsas additional arguments. Ifcontent_manager is not specified, usethecontent_managerspecified by the currentpolicy.
set_content(*args,content_manager=None,**kw)¶Call the
set_contentmethod of thecontent_manager, passing selfas the message object, and passing along any other arguments or keywordsas additional arguments. Ifcontent_manager is not specified, usethecontent_managerspecified by the currentpolicy.
make_related(boundary=None)¶Convert a non-
multipartmessage into amultipart/relatedmessage,moving any existingContent- headers and payload into a(new) first part of themultipart. Ifboundary is specified, useit as the boundary string in the multipart, otherwise leave the boundaryto be automatically created when it is needed (for example, when themessage is serialized).
make_alternative(boundary=None)¶Convert a non-
multipartor amultipart/relatedinto amultipart/alternative, moving any existingContent-headers and payload into a (new) first part of themultipart. Ifboundary is specified, use it as the boundary string in the multipart,otherwise leave the boundary to be automatically created when it isneeded (for example, when the message is serialized).
make_mixed(boundary=None)¶Convert a non-
multipart, amultipart/related, or amultipart-alternativeinto amultipart/mixed, moving any existingContent- headers and payload into a (new) first part of themultipart. Ifboundary is specified, use it as the boundary stringin the multipart, otherwise leave the boundary to be automaticallycreated when it is needed (for example, when the message is serialized).
add_related(*args,content_manager=None,**kw)¶If the message is a
multipart/related, create a new messageobject, pass all of the arguments to itsset_content()method,andattach()it to themultipart. Ifthe message is a non-multipart, callmake_related()and thenproceed as above. If the message is any other type ofmultipart,raise aTypeError. Ifcontent_manager is not specified, usethecontent_managerspecified by the currentpolicy.If the added part has noContent-Disposition header,add one with the valueinline.
add_alternative(*args,content_manager=None,**kw)¶If the message is a
multipart/alternative, create a new messageobject, pass all of the arguments to itsset_content()method, andattach()it to themultipart. If themessage is a non-multipartormultipart/related, callmake_alternative()and then proceed as above. If the message isany other type ofmultipart, raise aTypeError. Ifcontent_manager is not specified, use thecontent_managerspecifiedby the currentpolicy.
add_attachment(*args,content_manager=None,**kw)¶If the message is a
multipart/mixed, create a new message object,pass all of the arguments to itsset_content()method, andattach()it to themultipart. If themessage is a non-multipart,multipart/related, ormultipart/alternative, callmake_mixed()and then proceed asabove. Ifcontent_manager is not specified, use thecontent_managerspecified by the currentpolicy. If the added parthas noContent-Disposition header, add one with the valueattachment. This method can be used both for explicit attachments(Content-Disposition: attachment andinlineattachments(Content-Disposition: inline), by passing appropriateoptions to thecontent_manager.
clear()¶Remove the payload and all of the headers.
clear_content()¶Remove the payload and all of the
Content-headers, leavingall other headers intact and in their original order.
- class
email.message.MIMEPart(policy=default)¶ This class represents a subpart of a MIME message. It is identical to
EmailMessage, except that noMIME-Version headers areadded whenset_content()is called, since sub-parts donot need their ownMIME-Version headers.
- class
email.contentmanager.ContentManager¶ Base class for content managers. Provides the standard registry mechanismsto register converters between MIME content and other representations, aswell as the
get_contentandset_contentdispatch methods.get_content(msg,*args,**kw)¶Look up a handler function based on the
mimetypeofmsg (see nextparagraph), call it, passing through all arguments, and return the resultof the call. The expectation is that the handler will extract thepayload frommsg and return an object that encodes information aboutthe extracted data.To find the handler, look for the following keys in the registry,stopping with the first one found:
- the string representing the full MIME type (
maintype/subtype) - the string representing the
maintype - the empty string
If none of these keys produce a handler, raise a
KeyErrorfor thefull MIME type.- the string representing the full MIME type (
set_content(msg,obj,*args,**kw)¶If the
maintypeismultipart, raise aTypeError; otherwiselook up a handler function based on the type ofobj (see nextparagraph), callclear_content()on themsg, and call the handler function, passing through all arguments. Theexpectation is that the handler will transform and storeobj intomsg, possibly making other changes tomsg as well, such as addingvarious MIME headers to encode information needed to interpret the storeddata.To find the handler, obtain the type ofobj (
typ=type(obj)), andlook for the following keys in the registry, stopping with the first onefound:- the type itself (
typ) - the type’s fully qualified name (
typ.__module__+'.'+typ.__qualname__). - the type’s qualname (
typ.__qualname__) - the type’s name (
typ.__name__).
If none of the above match, repeat all of the checks above for each ofthe types in theMRO (
typ.__mro__). Finally, if no other keyyields a handler, check for a handler for the keyNone. If there isno handler forNone, raise aKeyErrorfor the fullyqualified name of the type.Also add aMIME-Version header if one is not present (seealso
MIMEPart).- the type itself (
add_get_handler(key,handler)¶Record the functionhandler as the handler forkey. For the possiblevalues ofkey, see
get_content().
add_set_handler(typekey,handler)¶Recordhandler as the function to call when an object of a typematchingtypekey is passed to
set_content(). For the possiblevalues oftypekey, seeset_content().
19.1.6.1. Content Manager Instances¶
Currently the email package provides only one concrete content manager,raw_data_manager, although more may be added in the future.raw_data_manager is thecontent_manager provided byEmailPolicy and its derivatives.
email.contentmanager.raw_data_manager¶This content manager provides only a minimum interface beyond that providedby
Messageitself: it deals only with text, rawbyte strings, andMessageobjects. Nevertheless, itprovides significant advantages compared to the base API:get_contentona text part will return a unicode string without the application needing tomanually decode it,set_contentprovides a rich set of options forcontrolling the headers added to a part and controlling the content transferencoding, and it enables the use of the variousadd_methods, therebysimplifying the creation of multipart messages.email.contentmanager.get_content(msg,errors='replace')¶Return the payload of the part as either a string (for
textparts), anEmailMessageobject (formessage/rfc822parts), or abytesobject (for all other non-multipart types). RaiseaKeyErrorif called on amultipart. If the part is atextpart anderrors is specified, use it as the error handler whendecoding the payload to unicode. The default error handler isreplace.
email.contentmanager.set_content(msg,<'str'>,subtype="plain",charset='utf-8' cte=None,disposition=None,filename=None,cid=None,params=None,headers=None)¶email.contentmanager.set_content(msg,<'bytes'>,maintype,subtype,cte="base64",disposition=None,filename=None,cid=None,params=None,headers=None)email.contentmanager.set_content(msg,<'Message'>,cte=None,disposition=None,filename=None,cid=None,params=None,headers=None)email.contentmanager.set_content(msg,<'list'>,subtype='mixed',disposition=None,filename=None,cid=None,params=None,headers=None)Add headers and payload tomsg:
Add aContent-Type header with a
maintype/subtypevalue.- For
str, set the MIMEmaintypetotext, and set thesubtype tosubtype if it is specified, orplainif it is not. - For
bytes, use the specifiedmaintype andsubtype, orraise aTypeErrorif they are not specified. - For
Messageobjects, set the maintype tomessage, and set the subtype tosubtype if it is specifiedorrfc822if it is not. Ifsubtype ispartial, raise anerror (bytesobjects must be used to constructmessage/partialparts). - For<’list’>, which should be a list of
Messageobjects, set themaintypetomultipart, and thesubtypetosubtype if it isspecified, andmixedif it is not. If the message parts inthe<’list’> haveMIME-Version headers, removethem.
Ifcharset is provided (which is valid only for
str), encode thestring to bytes using the specified character set. The default isutf-8. If the specifiedcharset is a known alias for a standardMIME charset name, use the standard charset instead.Ifcte is set, encode the payload using the specified content transferencoding, and set theContent-Transfer-Encoding header tothat value. For
strobjects, if it is not set use heuristics todetermine the most compact encoding. Possible values forcte arequoted-printable,base64,7bit,8bit, andbinary.If the input cannot be encoded in the specified encoding (eg:7bit),raise aValueError. ForMessage, perRFC 2046, raise an error if acte ofquoted-printableorbase64is requested forsubtyperfc822, and for anycteother than7bitforsubtypeexternal-body. Formessage/rfc822, use8bitifcte is not specified. For allother values ofsubtype, use7bit.Note
Acte of
binarydoes not actually work correctly yet.TheMessageobject as modified byset_contentis correct, butBytesGeneratordoes not serialize itcorrectly.Ifdisposition is set, use it as the value of theContent-Disposition header. If not specified, andfilename is specified, add the header with the value
attachment.If it is not specified andfilename is also not specified, do not addthe header. The only valid values fordisposition areattachmentandinline.Iffilename is specified, use it as the value of the
filenameparameter of theContent-Disposition header. There is nodefault.Ifcid is specified, add aContent-ID header withcid as its value.
Ifparams is specified, iterate its
itemsmethod and use theresulting(key,value)pairs to set additional parameters on theContent-Type header.Ifheaders is specified and is a list of strings of the form
headername:headervalueor a list ofheaderobjects(distinguished from strings by having anameattribute), add theheaders tomsg.- For
