You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.
Dismiss alert
<title>email.message.Message: Representing an email message using the compat32 API — Python 3.15.2 說明文件</title><metaname="viewport" content="width=device-width, initial-scale=1.0">
<spanid="compat32-message"></span><h1><aclass="reference internal" href="#email.message.Message" title="email.message.Message"><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">email.message.Message</span></code></a>: Representing an email message using the <aclass="reference internal" href="email.policy.html#email.policy.compat32" title="email.policy.compat32"><codeclass="xref py py-data docutils literal notranslate"><spanclass="pre">compat32</span></code></a> API<aclass="headerlink" href="#email-message-message-representing-an-email-message-using-the-compat32-api" title="連結到這個標頭">¶</a></h1>
<p>The <aclass="reference internal" href="#email.message.Message" title="email.message.Message"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Message</span></code></a> class is very similar to the
<aclass="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">EmailMessage</span></code></a> class, without the methods added by that
class, and with the default behavior of certain other methods being slightly
different. We also document here some methods that, while supported by the
<aclass="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">EmailMessage</span></code></a> class, are not recommended unless you are
dealing with legacy code.</p>
<p>The philosophy and structure of the two classes is otherwise the same.</p>
<p>This document describes the behavior under the default (for <aclass="reference internal" href="#email.message.Message" title="email.message.Message"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Message</span></code></a>)
policy <aclass="reference internal" href="email.policy.html#email.policy.Compat32" title="email.policy.Compat32"><codeclass="xref py py-attr docutils literal notranslate"><spanclass="pre">Compat32</span></code></a>. If you are going to use another policy,
you should be using the <aclass="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">EmailMessage</span></code></a> class instead.</p>
<p>An email message consists of <em>headers</em> and a <em>payload</em>. Headers must be
<spanclass="target" id="index-0"></span><aclass="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5322.html"><strong>RFC 5322</strong></a> style names and values, where the field name and value are
separated by a colon. The colon is not part of either the field name or the
field value. The payload may be a simple text message, or a binary object, or
a structured sequence of sub-messages each with their own set of headers and
their own payload. The latter type of payload is indicated by the message
having a MIME type such as <emclass="mimetype">multipart/*</em> or
<emclass="mimetype">message/rfc822</em>.</p>
<p>The conceptual model provided by a <aclass="reference internal" href="#email.message.Message" title="email.message.Message"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Message</span></code></a> object is that of an
ordered dictionary of headers with additional methods for accessing both
specialized information from the headers, for accessing the payload, for
generating a serialized version of the message, and for recursively walking
over the object tree. Note that duplicate headers are supported but special
methods must be used to access them.</p>
<p>The <aclass="reference internal" href="#email.message.Message" title="email.message.Message"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Message</span></code></a> pseudo-dictionary is indexed by the header names, which
must be ASCII values. The values of the dictionary are strings that are
supposed to contain only ASCII characters; there is some special handling for
non-ASCII input, but it doesn't always produce the correct results. Headers
are stored and returned in case-preserving form, but field names are matched
case-insensitively. There may also be a single envelope header, also known as
the <em>Unix-From</em> header or the <codeclass="docutils literal notranslate"><spanclass="pre">From_</span></code> header. The <em>payload</em> is either a
string or bytes, in the case of simple message objects, or a list of
<emclass="mimetype">multipart/*</em> and <emclass="mimetype">message/rfc822</em>).</p>
<p>Here are the methods of the <aclass="reference internal" href="#email.message.Message" title="email.message.Message"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Message</span></code></a> class:</p>
<dd><p>If <em>policy</em> is specified (it must be an instance of a <aclass="reference internal" href="email.policy.html#module-email.policy" title="email.policy: Controlling the parsing and generating of messages"><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">policy</span></code></a>
class) use the rules it specifies to update and serialize the representation
of the message. If <em>policy</em> is not set, use the <aclass="reference internal" href="email.policy.html#email.policy.Compat32" title="email.policy.Compat32"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">compat32</span></code></a> policy, which maintains backward compatibility with
the Python 3.2 version of the email package. For more information see the
<aclass="reference internal" href="email.policy.html#module-email.policy" title="email.policy: Controlling the parsing and generating of messages"><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">policy</span></code></a> documentation.</p>
<dd><p>Return the entire message flattened as a string. When optional <em>unixfrom</em>
is true, the envelope header is included in the returned string.
<em>unixfrom</em> defaults to <codeclass="docutils literal notranslate"><spanclass="pre">False</span></code>. For backward compatibility reasons,
<em>maxheaderlen</em> defaults to <codeclass="docutils literal notranslate"><spanclass="pre">0</span></code>, so if you want a different value you
must override it explicitly (the value specified for <em>max_line_length</em> in
the policy will be ignored by this method). The <em>policy</em> argument may be
used to override the default policy obtained from the message instance.
This can be used to control some of the formatting produced by the
method, since the specified <em>policy</em> will be passed to the <codeclass="docutils literal notranslate"><spanclass="pre">Generator</span></code>.</p>
<p>Flattening the message may trigger changes to the <aclass="reference internal" href="#email.message.Message" title="email.message.Message"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Message</span></code></a> if
defaults need to be filled in to complete the transformation to a string
(for example, MIME boundaries may be generated or modified).</p>
<p>Note that this method is provided as a convenience and may not always
format the message the way you want. For example, by default it does
not do the mangling of lines that begin with <codeclass="docutils literal notranslate"><spanclass="pre">From</span></code> that is
required by the Unix mbox format. For more flexibility, instantiate a
<aclass="reference internal" href="email.generator.html#email.generator.Generator" title="email.generator.Generator"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Generator</span></code></a> instance and use its
<dd><p>Equivalent to <aclass="reference internal" href="#email.message.Message.as_string" title="email.message.Message.as_string"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">as_string()</span></code></a>. Allows <codeclass="docutils literal notranslate"><spanclass="pre">str(msg)</span></code> to produce a
<dd><p>Return the entire message flattened as a bytes object. When optional
<em>unixfrom</em> is true, the envelope header is included in the returned
string. <em>unixfrom</em> defaults to <codeclass="docutils literal notranslate"><spanclass="pre">False</span></code>. The <em>policy</em> argument may be
used to override the default policy obtained from the message instance.
This can be used to control some of the formatting produced by the
method, since the specified <em>policy</em> will be passed to the
<p>Flattening the message may trigger changes to the <aclass="reference internal" href="#email.message.Message" title="email.message.Message"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Message</span></code></a> if
defaults need to be filled in to complete the transformation to a string
(for example, MIME boundaries may be generated or modified).</p>
<p>Note that this method is provided as a convenience and may not always
format the message the way you want. For example, by default it does
not do the mangling of lines that begin with <codeclass="docutils literal notranslate"><spanclass="pre">From</span></code> that is
required by the Unix mbox format. For more flexibility, instantiate a
<aclass="reference internal" href="email.generator.html#email.generator.BytesGenerator" title="email.generator.BytesGenerator"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">BytesGenerator</span></code></a> instance and use its
<dd><p>Equivalent to <aclass="reference internal" href="#email.message.Message.as_bytes" title="email.message.Message.as_bytes"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">as_bytes()</span></code></a>. Allows <codeclass="docutils literal notranslate"><spanclass="pre">bytes(msg)</span></code> to produce a
bytes object containing the formatted message.</p>
<aclass="reference internal" href="#email.message.Message.is_multipart" title="email.message.Message.is_multipart"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">is_multipart()</span></code></a> returns <codeclass="docutils literal notranslate"><spanclass="pre">False</span></code>, the payload should be a string
object (which might be a CTE encoded binary payload). (Note that
<aclass="reference internal" href="#email.message.Message.is_multipart" title="email.message.Message.is_multipart"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">is_multipart()</span></code></a> returning <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code> does not necessarily mean that
"msg.get_content_maintype() == 'multipart'" will return the <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code>.
For example, <codeclass="docutils literal notranslate"><spanclass="pre">is_multipart</span></code> will return <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code> when the
<aclass="reference internal" href="#email.message.Message" title="email.message.Message"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Message</span></code></a> is of type <codeclass="docutils literal notranslate"><spanclass="pre">message/rfc822</span></code>.)</p>
<dd><p>Add the given <em>payload</em> to the current payload, which must be <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code> or
a list of <aclass="reference internal" href="#email.message.Message" title="email.message.Message"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Message</span></code></a> objects before the call. After the call, the
payload will always be a list of <aclass="reference internal" href="#email.message.Message" title="email.message.Message"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Message</span></code></a> objects. If you want to
set the payload to a scalar object (e.g. a string), use
<aclass="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">EmailMessage</span></code></a> class its functionality is
replaced by <aclass="reference internal" href="email.message.html#email.message.EmailMessage.set_content" title="email.message.EmailMessage.set_content"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">set_content()</span></code></a> and the
related <codeclass="docutils literal notranslate"><spanclass="pre">make</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">add</span></code> methods.</p>
<dd><p>Return the current payload, which will be a list of
<aclass="reference internal" href="#email.message.Message" title="email.message.Message"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Message</span></code></a> objects when <aclass="reference internal" href="#email.message.Message.is_multipart" title="email.message.Message.is_multipart"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">is_multipart()</span></code></a> is <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code>, or a
string when <aclass="reference internal" href="#email.message.Message.is_multipart" title="email.message.Message.is_multipart"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">is_multipart()</span></code></a> is <codeclass="docutils literal notranslate"><spanclass="pre">False</span></code>. If the payload is a list
and you mutate the list object, you modify the message's payload in place.</p>
<p>With optional argument <em>i</em>, <aclass="reference internal" href="#email.message.Message.get_payload" title="email.message.Message.get_payload"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">get_payload()</span></code></a> will return the <em>i</em>-th
element of the payload, counting from zero, if <aclass="reference internal" href="#email.message.Message.is_multipart" title="email.message.Message.is_multipart"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">is_multipart()</span></code></a> is
<codeclass="docutils literal notranslate"><spanclass="pre">True</span></code>. An <aclass="reference internal" href="../builtins/exceptions.html#IndexError" title="IndexError"><codeclass="xref py py-exc docutils literal notranslate"><spanclass="pre">IndexError</span></code></a> will be raised if <em>i</em> is less than 0 or
greater than or equal to the number of items in the payload. If the
payload is a string (i.e. <aclass="reference internal" href="#email.message.Message.is_multipart" title="email.message.Message.is_multipart"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">is_multipart()</span></code></a> is <codeclass="docutils literal notranslate"><spanclass="pre">False</span></code>) and <em>i</em> is
given, a <aclass="reference internal" href="../builtins/exceptions.html#TypeError" title="TypeError"><codeclass="xref py py-exc docutils literal notranslate"><spanclass="pre">TypeError</span></code></a> is raised.</p>
<p>Optional <em>decode</em> is a flag indicating whether the payload should be
decoded or not, according to the <emclass="mailheader">Content-Transfer-Encoding</em>
header. When <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code> and the message is not a multipart, the payload will
be decoded if this header's value is <codeclass="docutils literal notranslate"><spanclass="pre">quoted-printable</span></code> or <codeclass="docutils literal notranslate"><spanclass="pre">base64</span></code>.
If some other encoding is used, or <emclass="mailheader">Content-Transfer-Encoding</em>
header is missing, the payload is
returned as-is (undecoded). In all cases the returned value is binary
data. If the message is a multipart and the <em>decode</em> flag is <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code>,
then <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code> is returned. If the payload is base64 and it was not
perfectly formed (missing padding, characters outside the base64
alphabet), then an appropriate defect will be added to the message's
<p>When <em>decode</em> is <codeclass="docutils literal notranslate"><spanclass="pre">False</span></code> (the default) the body is returned as a string
without decoding the <emclass="mailheader">Content-Transfer-Encoding</em>. However,
for a <emclass="mailheader">Content-Transfer-Encoding</em> of 8bit, an attempt is made
to decode the original bytes using the <codeclass="docutils literal notranslate"><spanclass="pre">charset</span></code> specified by the
<emclass="mailheader">Content-Type</em> header, using the <codeclass="docutils literal notranslate"><spanclass="pre">replace</span></code> error handler.
If no <codeclass="docutils literal notranslate"><spanclass="pre">charset</span></code> is specified, or if the <codeclass="docutils literal notranslate"><spanclass="pre">charset</span></code> given is not
recognized by the email package, the body is decoded using the default
ASCII charset.</p>
<p>This is a legacy method. On the
<aclass="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">EmailMessage</span></code></a> class its functionality is
replaced by <aclass="reference internal" href="email.message.html#email.message.EmailMessage.get_content" title="email.message.EmailMessage.get_content"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">get_content()</span></code></a> and
<dd><p>Set the entire message object's payload to <em>payload</em>. It is the client's
responsibility to ensure the payload invariants. Optional <em>charset</em> sets
the message's default character set; see <aclass="reference internal" href="#email.message.Message.set_charset" title="email.message.Message.set_charset"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">set_charset()</span></code></a> for details.</p>
<p>This is a legacy method. On the
<aclass="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">EmailMessage</span></code></a> class its functionality is
<dd><p>Set the character set of the payload to <em>charset</em>, which can either be a
<aclass="reference internal" href="email.charset.html#email.charset.Charset" title="email.charset.Charset"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Charset</span></code></a> instance (see <aclass="reference internal" href="email.charset.html#module-email.charset" title="email.charset: Character Sets"><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">email.charset</span></code></a>), a
string naming a character set, or <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code>. If it is a string, it will
be converted to a <aclass="reference internal" href="email.charset.html#email.charset.Charset" title="email.charset.Charset"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Charset</span></code></a> instance. If <em>charset</em>
is <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code>, the <codeclass="docutils literal notranslate"><spanclass="pre">charset</span></code> parameter will be removed from the
<emclass="mailheader">Content-Type</em> header (the message will not be otherwise
modified). Anything else will generate a <aclass="reference internal" href="../builtins/exceptions.html#TypeError" title="TypeError"><codeclass="xref py py-exc docutils literal notranslate"><spanclass="pre">TypeError</span></code></a>.</p>
<p>If there is no existing <emclass="mailheader">MIME-Version</em> header one will be
added. If there is no existing <emclass="mailheader">Content-Type</em> header, one
will be added with a value of <emclass="mimetype">text/plain</em>. Whether the
<emclass="mailheader">Content-Type</em> header already exists or not, its <codeclass="docutils literal notranslate"><spanclass="pre">charset</span></code>
parameter will be set to <em>charset.output_charset</em>. If
<em>charset.input_charset</em> and <em>charset.output_charset</em> differ, the payload
will be re-encoded to the <em>output_charset</em>. If there is no existing
<emclass="mailheader">Content-Transfer-Encoding</em> header, then the payload will be
transfer-encoded, if needed, using the specified
<aclass="reference internal" href="email.charset.html#email.charset.Charset" title="email.charset.Charset"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Charset</span></code></a>, and a header with the appropriate value
will be added. If a <emclass="mailheader">Content-Transfer-Encoding</em> header
already exists, the payload is assumed to already be correctly encoded
using that <emclass="mailheader">Content-Transfer-Encoding</em> and is not modified.</p>
<p>This is a legacy method. On the
<aclass="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">EmailMessage</span></code></a> class its functionality is
<dd><p>Return the <aclass="reference internal" href="email.charset.html#email.charset.Charset" title="email.charset.Charset"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Charset</span></code></a> instance associated with the
message's payload.</p>
<p>This is a legacy method. On the
<aclass="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">EmailMessage</span></code></a> class it always returns
<p>The following methods implement a mapping-like interface for accessing the
message's <spanclass="target" id="index-1"></span><aclass="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2822.html"><strong>RFC 2822</strong></a> headers. Note that there are some semantic differences
between these methods and a normal mapping (i.e. dictionary) interface. For
example, in a dictionary there are no duplicate keys, but here there may be
duplicate message headers. Also, in dictionaries there is no guaranteed
order to the keys returned by <aclass="reference internal" href="#email.message.Message.keys" title="email.message.Message.keys"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">keys()</span></code></a>, but in a <aclass="reference internal" href="#email.message.Message" title="email.message.Message"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Message</span></code></a> object,
headers are always returned in the order they appeared in the original
message, or were added to the message later. Any header deleted and then
re-added are always appended to the end of the header list.</p>
<p>These semantic differences are intentional and are biased toward maximal
convenience.</p>
<p>Note that in all cases, any envelope header present in the message is not
included in the mapping interface.</p>
<p>In a model generated from bytes, any header values that (in contravention of
the RFCs) contain non-ASCII bytes will, when retrieved through this
interface, be represented as <aclass="reference internal" href="email.header.html#email.header.Header" title="email.header.Header"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Header</span></code></a> objects with
a charset of <codeclass="docutils literal notranslate"><spanclass="pre">unknown-8bit</span></code>.</p>
<dd><p>Return <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code> if the message object has a field named <em>name</em>. Matching is
done case-insensitively and <em>name</em> should not include the trailing colon.
Used for the <codeclass="docutils literal notranslate"><spanclass="pre">in</span></code> operator, e.g.:</p>
<dd><p>Return the value of the named header field. <em>name</em> should not include the
colon field separator. If the header is missing, <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code> is returned; a
<aclass="reference internal" href="../builtins/exceptions.html#KeyError" title="KeyError"><codeclass="xref py py-exc docutils literal notranslate"><spanclass="pre">KeyError</span></code></a> is never raised.</p>
<p>Note that if the named field appears more than once in the message's
headers, exactly which of those field values will be returned is
undefined. Use the <aclass="reference internal" href="#email.message.Message.get_all" title="email.message.Message.get_all"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">get_all()</span></code></a> method to get the values of all the
<dd><p>Return the value of the named header field. This is identical to
<aclass="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">__getitem__()</span></code></a> except that optional <em>failobj</em> is returned if the
named header is missing (defaults to <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code>).</p>
<dd><p>Extended header setting. This method is similar to <aclass="reference internal" href="#email.message.Message.__setitem__" title="email.message.Message.__setitem__"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">__setitem__()</span></code></a>
except that additional header parameters can be provided as keyword
arguments. <em>_name</em> is the header field to add and <em>_value</em> is the
<em>primary</em> value for the header.</p>
<p>For each item in the keyword argument dictionary <em>_params</em>, the key is
taken as the parameter name, with underscores converted to dashes (since
dashes are illegal in Python identifiers). Normally, the parameter will
be added as <codeclass="docutils literal notranslate"><spanclass="pre">key="value"</span></code> unless the value is <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code>, in which case
only the key will be added. If the value contains non-ASCII characters,
it can be specified as a three tuple in the format
<codeclass="docutils literal notranslate"><spanclass="pre">(CHARSET,</span><spanclass="pre">LANGUAGE,</span><spanclass="pre">VALUE)</span></code>, where <codeclass="docutils literal notranslate"><spanclass="pre">CHARSET</span></code> is a string naming the
charset to be used to encode the value, <codeclass="docutils literal notranslate"><spanclass="pre">LANGUAGE</span></code> can usually be set
to <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code> or the empty string (see <spanclass="target" id="index-2"></span><aclass="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2231.html"><strong>RFC 2231</strong></a> for other possibilities),
and <codeclass="docutils literal notranslate"><spanclass="pre">VALUE</span></code> is the string value containing non-ASCII code points. If
a three tuple is not passed and the value contains non-ASCII characters,
it is automatically encoded in <spanclass="target" id="index-3"></span><aclass="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2231.html"><strong>RFC 2231</strong></a> format using a <codeclass="docutils literal notranslate"><spanclass="pre">CHARSET</span></code>
of <codeclass="docutils literal notranslate"><spanclass="pre">utf-8</span></code> and a <codeclass="docutils literal notranslate"><spanclass="pre">LANGUAGE</span></code> of <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code>.</p>
<dd><p>Return the message's content type. The returned string is coerced to
lower case of the form <emclass="mimetype">maintype/subtype</em>. If there was no
<emclass="mailheader">Content-Type</em> header in the message the default type as given
by <aclass="reference internal" href="#email.message.Message.get_default_type" title="email.message.Message.get_default_type"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">get_default_type()</span></code></a> will be returned. Since according to
<p><spanclass="target" id="index-5"></span><aclass="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2045.html"><strong>RFC 2045</strong></a> defines a message's default type to be <emclass="mimetype">text/plain</em>
unless it appears inside a <emclass="mimetype">multipart/digest</em> container, in
which case it would be <emclass="mimetype">message/rfc822</em>. If the
<emclass="mailheader">Content-Type</em> header has an invalid type specification,
<spanclass="target" id="index-6"></span><aclass="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2045.html"><strong>RFC 2045</strong></a> mandates that the default type be <emclass="mimetype">text/plain</em>.</p>
<dd><p>Return the message's main content type. This is the <emclass="mimetype">maintype</em>
part of the string returned by <aclass="reference internal" href="#email.message.Message.get_content_type" title="email.message.Message.get_content_type"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">get_content_type()</span></code></a>.</p>
<dd><p>Return the message's sub-content type. This is the <emclass="mimetype">subtype</em>
part of the string returned by <aclass="reference internal" href="#email.message.Message.get_content_type" title="email.message.Message.get_content_type"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">get_content_type()</span></code></a>.</p>
<dd><p>Return the message's <emclass="mailheader">Content-Type</em> parameters, as a list.
The elements of the returned list are 2-tuples of key/value pairs, as
split on the <codeclass="docutils literal notranslate"><spanclass="pre">'='</span></code> sign. The left hand side of the <codeclass="docutils literal notranslate"><spanclass="pre">'='</span></code> is the key,
while the right hand side is the value. If there is no <codeclass="docutils literal notranslate"><spanclass="pre">'='</span></code> sign in
the parameter the value is the empty string, otherwise the value is as
described in <aclass="reference internal" href="#email.message.Message.get_param" title="email.message.Message.get_param"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">get_param()</span></code></a> and is unquoted if optional <em>unquote</em> is
<codeclass="docutils literal notranslate"><spanclass="pre">True</span></code> (the default).</p>
<p>Optional <em>failobj</em> is the object to return if there is no
<emclass="mailheader">Content-Type</em> header. Optional <em>header</em> is the header to
search instead of <emclass="mailheader">Content-Type</em>.</p>
<p>This is a legacy method. On the
<aclass="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">EmailMessage</span></code></a> class its functionality is
replaced by the <em>params</em> property of the individual header objects
<dd><p>Return the value of the <emclass="mailheader">Content-Type</em> header's parameter
<em>param</em> as a string. If the message has no <emclass="mailheader">Content-Type</em>
header or if there is no such parameter, then <em>failobj</em> is returned
(defaults to <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code>).</p>
<p>Optional <em>header</em> if given, specifies the message header to use instead of
<emclass="mailheader">Content-Type</em>.</p>
<p>Parameter keys are always compared case insensitively. The return value
can either be a string, or a 3-tuple if the parameter was <spanclass="target" id="index-7"></span><aclass="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2231.html"><strong>RFC 2231</strong></a>
encoded. When it's a 3-tuple, the elements of the value are of the form
<codeclass="docutils literal notranslate"><spanclass="pre">(CHARSET,</span><spanclass="pre">LANGUAGE,</span><spanclass="pre">VALUE)</span></code>. Note that both <codeclass="docutils literal notranslate"><spanclass="pre">CHARSET</span></code> and
<codeclass="docutils literal notranslate"><spanclass="pre">LANGUAGE</span></code> can be <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code>, in which case you should consider <codeclass="docutils literal notranslate"><spanclass="pre">VALUE</span></code>
to be encoded in the <codeclass="docutils literal notranslate"><spanclass="pre">us-ascii</span></code> charset. You can usually ignore
<p>If your application doesn't care whether the parameter was encoded as in
<spanclass="target" id="index-8"></span><aclass="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2231.html"><strong>RFC 2231</strong></a>, you can collapse the parameter value by calling
<aclass="reference internal" href="email.utils.html#email.utils.collapse_rfc2231_value" title="email.utils.collapse_rfc2231_value"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">email.utils.collapse_rfc2231_value()</span></code></a>, passing in the return value
from <aclass="reference internal" href="#email.message.Message.get_param" title="email.message.Message.get_param"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">get_param()</span></code></a>. This will return a suitably decoded Unicode
string when the value is a tuple, or the original string unquoted if it
<p>In any case, the parameter value (either the returned string, or the
<codeclass="docutils literal notranslate"><spanclass="pre">VALUE</span></code> item in the 3-tuple) is always unquoted, unless <em>unquote</em> is set
to <codeclass="docutils literal notranslate"><spanclass="pre">False</span></code>.</p>
<p>This is a legacy method. On the
<aclass="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">EmailMessage</span></code></a> class its functionality is
replaced by the <em>params</em> property of the individual header objects
<dd><p>Set a parameter in the <emclass="mailheader">Content-Type</em> header. If the
parameter already exists in the header, its value will be replaced with
<em>value</em>. If the <emclass="mailheader">Content-Type</em> header as not yet been defined
for this message, it will be set to <emclass="mimetype">text/plain</em> and the new
parameter value will be appended as per <spanclass="target" id="index-9"></span><aclass="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2045.html"><strong>RFC 2045</strong></a>.</p>
<p>Optional <em>header</em> specifies an alternative header to
<emclass="mailheader">Content-Type</em>, and all parameters will be quoted as necessary
unless optional <em>requote</em> is <codeclass="docutils literal notranslate"><spanclass="pre">False</span></code> (the default is <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code>).</p>
<p>If optional <em>charset</em> is specified, the parameter will be encoded
according to <spanclass="target" id="index-10"></span><aclass="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2231.html"><strong>RFC 2231</strong></a>. Optional <em>language</em> specifies the RFC 2231
language, defaulting to the empty string. Both <em>charset</em> and <em>language</em>
should be strings.</p>
<p>If <em>replace</em> is <codeclass="docutils literal notranslate"><spanclass="pre">False</span></code> (the default) the header is moved to the
end of the list of headers. If <em>replace</em> is <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code>, the header
<dd><p>Remove the given parameter completely from the <emclass="mailheader">Content-Type</em>
header. The header will be re-written in place without the parameter or
its value. All values will be quoted as necessary unless <em>requote</em> is
<codeclass="docutils literal notranslate"><spanclass="pre">False</span></code> (the default is <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code>). Optional <em>header</em> specifies an
alternative to <emclass="mailheader">Content-Type</em>.</p>
<dd><p>Set the main type and subtype for the <emclass="mailheader">Content-Type</em>
header. <em>type</em> must be a string in the form <emclass="mimetype">maintype/subtype</em>,
otherwise a <aclass="reference internal" href="../builtins/exceptions.html#ValueError" title="ValueError"><codeclass="xref py py-exc docutils literal notranslate"><spanclass="pre">ValueError</span></code></a> is raised.</p>
<p>This method replaces the <emclass="mailheader">Content-Type</em> header, keeping all
the parameters in place. If <em>requote</em> is <codeclass="docutils literal notranslate"><spanclass="pre">False</span></code>, this leaves the
existing header's quoting as is, otherwise the parameters will be quoted
(the default).</p>
<p>An alternative header can be specified in the <em>header</em> argument. When the
<emclass="mailheader">Content-Type</em> header is set a <emclass="mailheader">MIME-Version</em>
header is also added.</p>
<p>This is a legacy method. On the
<aclass="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">EmailMessage</span></code></a> class its functionality is
replaced by the <codeclass="docutils literal notranslate"><spanclass="pre">make_</span></code> and <codeclass="docutils literal notranslate"><spanclass="pre">add_</span></code> methods.</p>
<dd><p>Return the value of the <codeclass="docutils literal notranslate"><spanclass="pre">filename</span></code> parameter of the
<emclass="mailheader">Content-Disposition</em> header of the message. If the header
does not have a <codeclass="docutils literal notranslate"><spanclass="pre">filename</span></code> parameter, this method falls back to looking
for the <codeclass="docutils literal notranslate"><spanclass="pre">name</span></code> parameter on the <emclass="mailheader">Content-Type</em> header. If
neither is found, or the header is missing, then <em>failobj</em> is returned.
The returned string will always be unquoted as per
<dd><p>Return the value of the <codeclass="docutils literal notranslate"><spanclass="pre">boundary</span></code> parameter of the
<emclass="mailheader">Content-Type</em> header of the message, or <em>failobj</em> if either
the header is missing, or has no <codeclass="docutils literal notranslate"><spanclass="pre">boundary</span></code> parameter. The returned
string will always be unquoted as per <aclass="reference internal" href="email.utils.html#email.utils.unquote" title="email.utils.unquote"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">email.utils.unquote()</span></code></a>.</p>
<dd><p>Set the <codeclass="docutils literal notranslate"><spanclass="pre">boundary</span></code> parameter of the <emclass="mailheader">Content-Type</em> header to
<em>boundary</em>. <aclass="reference internal" href="#email.message.Message.set_boundary" title="email.message.Message.set_boundary"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">set_boundary()</span></code></a> will always quote <em>boundary</em> if
necessary. A <aclass="reference internal" href="email.errors.html#email.errors.HeaderParseError" title="email.errors.HeaderParseError"><codeclass="xref py py-exc docutils literal notranslate"><spanclass="pre">HeaderParseError</span></code></a> is raised if the
message object has no <emclass="mailheader">Content-Type</em> header.</p>
<p>Note that using this method is subtly different than deleting the old
<emclass="mailheader">Content-Type</em> header and adding a new one with the new
<dd><p>Return the <codeclass="docutils literal notranslate"><spanclass="pre">charset</span></code> parameter of the <emclass="mailheader">Content-Type</em> header,
coerced to lower case. If there is no <emclass="mailheader">Content-Type</em> header, or if
that header has no <codeclass="docutils literal notranslate"><spanclass="pre">charset</span></code> parameter, <em>failobj</em> is returned.</p>
<p>Note that this method differs from <aclass="reference internal" href="#email.message.Message.get_charset" title="email.message.Message.get_charset"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">get_charset()</span></code></a> which returns the
<aclass="reference internal" href="email.charset.html#email.charset.Charset" title="email.charset.Charset"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Charset</span></code></a> instance for the default encoding of the message body.</p>
<dd><p>Return a list containing the character set names in the message. If the
message is a <emclass="mimetype">multipart</em>, then the list will contain one element
for each subpart in the payload, otherwise, it will be a list of length 1.</p>
<p>Each item in the list will be a string which is the value of the
<codeclass="docutils literal notranslate"><spanclass="pre">charset</span></code> parameter in the <emclass="mailheader">Content-Type</em> header for the
represented subpart. However, if the subpart has no
<emclass="mailheader">Content-Type</em> header, no <codeclass="docutils literal notranslate"><spanclass="pre">charset</span></code> parameter, or is not of
the <emclass="mimetype">text</em> main MIME type, then that item in the returned list
<dd><p>Return the lowercased value (without parameters) of the message's
<emclass="mailheader">Content-Disposition</em> header if it has one, or <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code>. The
possible values for this method are <em>inline</em>, <em>attachment</em> or <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code>
if the message follows <spanclass="target" id="index-11"></span><aclass="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2183.html"><strong>RFC 2183</strong></a>.</p>
<dd><p>The <aclass="reference internal" href="#email.message.Message.walk" title="email.message.Message.walk"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">walk()</span></code></a> method is an all-purpose generator which can be used to
iterate over all the parts and subparts of a message object tree, in
depth-first traversal order. You will typically use <aclass="reference internal" href="#email.message.Message.walk" title="email.message.Message.walk"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">walk()</span></code></a> as the
iterator in a <codeclass="docutils literal notranslate"><spanclass="pre">for</span></code> loop; each iteration returns the next subpart.</p>
<p>Here's an example that prints the MIME type of every part of a multipart
<p><codeclass="docutils literal notranslate"><spanclass="pre">walk</span></code> iterates over the subparts of any part where
<aclass="reference internal" href="#email.message.Message.is_multipart" title="email.message.Message.is_multipart"><codeclass="xref py py-meth docutils literal notranslate"><spanclass="pre">is_multipart()</span></code></a> returns <codeclass="docutils literal notranslate"><spanclass="pre">True</span></code>, even though
<codeclass="docutils literal notranslate"><spanclass="pre">msg.get_content_maintype()</span><spanclass="pre">==</span><spanclass="pre">'multipart'</span></code> may return <codeclass="docutils literal notranslate"><spanclass="pre">False</span></code>. We
can see this in our example by making use of the <codeclass="docutils literal notranslate"><spanclass="pre">_structure</span></code> debug
<p>Here the <codeclass="docutils literal notranslate"><spanclass="pre">message</span></code> parts are not <codeclass="docutils literal notranslate"><spanclass="pre">multiparts</span></code>, but they do contain
<dd><p>The format of a MIME document allows for some text between the blank line
following the headers, and the first multipart boundary string. Normally,
this text is never visible in a MIME-aware mail reader because it falls
outside the standard MIME armor. However, when viewing the raw text of
the message, or when viewing the message in a non-MIME aware reader, this
text can become visible.</p>
<p>The <em>preamble</em> attribute contains this leading extra-armor text for MIME
documents. When the <aclass="reference internal" href="email.parser.html#email.parser.Parser" title="email.parser.Parser"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Parser</span></code></a> discovers some text
after the headers but before the first boundary string, it assigns this
text to the message's <em>preamble</em> attribute. When the
<aclass="reference internal" href="email.generator.html#email.generator.Generator" title="email.generator.Generator"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Generator</span></code></a> is writing out the plain text
representation of a MIME message, and it finds the
message has a <em>preamble</em> attribute, it will write this text in the area
between the headers and the first boundary. See <aclass="reference internal" href="email.parser.html#module-email.parser" title="email.parser: Parse flat text email messages to produce a message object structure."><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">email.parser</span></code></a> and
<aclass="reference internal" href="email.generator.html#module-email.generator" title="email.generator: Generate flat text email messages from a message structure."><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">email.generator</span></code></a> for details.</p>
<p>Note that if the message object has no preamble, the <em>preamble</em> attribute
will be <codeclass="docutils literal notranslate"><spanclass="pre">None</span></code>.</p>
<dd><p>The <em>epilogue</em> attribute acts the same way as the <em>preamble</em> attribute,
except that it contains text that appears between the last boundary and
the end of the message.</p>
<p>You do not need to set the epilogue to the empty string in order for the
<aclass="reference internal" href="email.generator.html#email.generator.Generator" title="email.generator.Generator"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Generator</span></code></a> to print a newline at the end of the
<dd><p>The <em>defects</em> attribute contains a list of all the problems found when
parsing this message. See <aclass="reference internal" href="email.errors.html#module-email.errors" title="email.errors: The exception classes used by the email package."><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">email.errors</span></code></a> for a detailed description