class email.message.EmailMessage(policy=default)
If policy is specified (it must be an instance of a policy class) use the rules it specifies to udpate and serialize the representation of the message. If policy is not set, use the default policy, which follows the rules of the email RFCs except for line endings (instead of the RFC mandated \r\n, it uses the Python standard \n line endings). For more information see the policy documentation.
This class is a subclass of Message. It adds the following methods:
-
is_attachment() -
Return
Trueif there is a Content-Disposition header and its (case insensitive) value isattachment,Falseotherwise.Changed in version 3.4.2: is_attachment is now a method instead of a property, for consistency with
is_multipart().
-
get_body(preferencelist=('related', 'html', 'plain')) -
Return the MIME part that is the best candidate to be the “body” of the message.
preferencelist must be a sequence of strings from the set
related,html, andplain, and indicates the order of preference for the content type of the part returned.Start looking for candidate matches with the object on which the
get_bodymethod is called.If
relatedis not included in preferencelist, consider the root part (or subpart of the root part) of any related encountered as a candidate if the (sub-)part matches a preference.When encountering a
multipart/related, check thestartparameter and if a part with a matching Content-ID is found, consider only it when looking for candidate matches. Otherwise consider only the first (default root) part of themultipart/related.If a part has a Content-Disposition header, only consider the part a candidate match if the value of the header is
inline.If none of the candidates matches any of the preferences in preferneclist, return
None.Notes: (1) For most applications the only preferencelist combinations that really make sense are
('plain',),('html', 'plain'), and the default,('related', 'html', 'plain'). (2) Because matching starts with the object on whichget_bodyis called, callingget_bodyon amultipart/relatedwill return the object itself unless preferencelist has a non-default value. (3) Messages (or message parts) that do not specify a Content-Type or whose Content-Type header is invalid will be treated as if they are of typetext/plain, which may occasionally causeget_bodyto return unexpected results.
-
iter_attachments() -
Return an iterator over all of the parts of the message that are not candidate “body” parts. That is, skip the first occurrence of each of
text/plain,text/html,multipart/related, ormultipart/alternative(unless they are explicitly marked as attachments via Content-Disposition: attachment), and return all remaining parts. When applied directly to amultipart/related, return an iterator over the all the related parts except the root part (ie: the part pointed to by thestartparameter, or the first part if there is nostartparameter or thestartparameter doesn’t match the Content-ID of any of the parts). When applied directly to amultipart/alternativeor a non-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 the content_manager, passing self as the message object, and passing along any other arguments or keywords as additional arguments. If content_manager is not specified, use thecontent_managerspecified by the currentpolicy.
-
set_content(*args, content_manager=None, **kw) -
Call the
set_contentmethod of the content_manager, passing self as the message object, and passing along any other arguments or keywords as additional arguments. If content_manager is not specified, use thecontent_managerspecified by the currentpolicy.
-
make_related(boundary=None) -
Convert a non-
multipartmessage into amultipart/relatedmessage, moving any existing Content- headers and payload into a (new) first part of themultipart. If boundary is specified, use it as the boundary string in the multipart, otherwise leave the boundary to be automatically created when it is needed (for example, when the message is serialized).
-
make_alternative(boundary=None) -
Convert a non-
multipartor amultipart/relatedinto amultipart/alternative, moving any existing Content- headers and payload into a (new) first part of themultipart. If boundary is specified, use it as the boundary string in the multipart, otherwise leave the boundary to be automatically created when it is needed (for example, when the message is serialized).
-
make_mixed(boundary=None) -
Convert a non-
multipart, amultipart/related, or amultipart-alternativeinto amultipart/mixed, moving any existing Content- headers and payload into a (new) first part of themultipart. If boundary is specified, use it as the boundary string in the multipart, otherwise leave the boundary to be automatically created 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 message object, pass all of the arguments to itsset_content()method, andattach()it to themultipart. If the message is a non-multipart, callmake_related()and then proceed as above. If the message is any other type ofmultipart, raise aTypeError. If content_manager is not specified, use thecontent_managerspecified by the currentpolicy. If the added part has no Content-Disposition header, add one with the valueinline.
-
add_alternative(*args, content_manager=None, **kw) -
If the message is a
multipart/alternative, create a new message object, pass all of the arguments to itsset_content()method, andattach()it to themultipart. If the message is a non-multipartormultipart/related, callmake_alternative()and then proceed as above. If the message is any other type ofmultipart, raise aTypeError. If content_manager is not specified, use thecontent_managerspecified by 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 the message is a non-multipart,multipart/related, ormultipart/alternative, callmake_mixed()and then proceed as above. If content_manager is not specified, use thecontent_managerspecified by the currentpolicy. If the added part has no Content-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 appropriate options to thecontent_manager.
-
clear() -
Remove the payload and all of the headers.
-
clear_content() -
Remove the payload and all of the
Content-headers, leaving all other headers intact and in their original order.
Please login to continue.