gooderp18绿色标准版
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1048 lines
100KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="email.message.Message: Representing an email message using the compat32 API" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/email.compat32-message.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="The Message class is very similar to the EmailMessage class, without the methods added by that class, and with the default behavior of certain other methods being slightly different. We also docume..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="The Message class is very similar to the EmailMessage class, without the methods added by that class, and with the default behavior of certain other methods being slightly different. We also docume..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>email.message.Message: Representing an email message using the compat32 API &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="email.mime: Creating email and MIME objects from scratch" href="email.mime.html" />
  33. <link rel="prev" title="email: Examples" href="email.examples.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/email.compat32-message.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h4>Previous topic</h4>
  84. <p class="topless"><a href="email.examples.html"
  85. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code>: Examples</a></p>
  86. </div>
  87. <div>
  88. <h4>Next topic</h4>
  89. <p class="topless"><a href="email.mime.html"
  90. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.mime</span></code>: Creating email and MIME objects from scratch</a></p>
  91. </div>
  92. <div role="note" aria-label="source link">
  93. <h3>This Page</h3>
  94. <ul class="this-page-menu">
  95. <li><a href="../bugs.html">Report a Bug</a></li>
  96. <li>
  97. <a href="https://github.com/python/cpython/blob/main/Doc/library/email.compat32-message.rst"
  98. rel="nofollow">Show Source
  99. </a>
  100. </li>
  101. </ul>
  102. </div>
  103. </nav>
  104. </div>
  105. </div>
  106. <div class="related" role="navigation" aria-label="related navigation">
  107. <h3>Navigation</h3>
  108. <ul>
  109. <li class="right" style="margin-right: 10px">
  110. <a href="../genindex.html" title="General Index"
  111. accesskey="I">index</a></li>
  112. <li class="right" >
  113. <a href="../py-modindex.html" title="Python Module Index"
  114. >modules</a> |</li>
  115. <li class="right" >
  116. <a href="email.mime.html" title="email.mime: Creating email and MIME objects from scratch"
  117. accesskey="N">next</a> |</li>
  118. <li class="right" >
  119. <a href="email.examples.html" title="email: Examples"
  120. accesskey="P">previous</a> |</li>
  121. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  122. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  123. <li class="switchers">
  124. <div class="language_switcher_placeholder"></div>
  125. <div class="version_switcher_placeholder"></div>
  126. </li>
  127. <li>
  128. </li>
  129. <li id="cpython-language-and-version">
  130. <a href="../index.html">3.12.3 Documentation</a> &#187;
  131. </li>
  132. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  133. <li class="nav-item nav-item-2"><a href="netdata.html" >Internet Data Handling</a> &#187;</li>
  134. <li class="nav-item nav-item-3"><a href="email.html" accesskey="U"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code> — An email and MIME handling package</a> &#187;</li>
  135. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.message.Message</span></code>: Representing an email message using the <code class="xref py py-data docutils literal notranslate"><span class="pre">compat32</span></code> API</a></li>
  136. <li class="right">
  137. <div class="inline-search" role="search">
  138. <form class="inline-search" action="../search.html" method="get">
  139. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  140. <input type="submit" value="Go" />
  141. </form>
  142. </div>
  143. |
  144. </li>
  145. <li class="right">
  146. <label class="theme-selector-label">
  147. Theme
  148. <select class="theme-selector" oninput="activateTheme(this.value)">
  149. <option value="auto" selected>Auto</option>
  150. <option value="light">Light</option>
  151. <option value="dark">Dark</option>
  152. </select>
  153. </label> |</li>
  154. </ul>
  155. </div>
  156. <div class="document">
  157. <div class="documentwrapper">
  158. <div class="bodywrapper">
  159. <div class="body" role="main">
  160. <section id="email-message-message-representing-an-email-message-using-the-compat32-api">
  161. <span id="compat32-message"></span><h1><a class="reference internal" href="#email.message.Message" title="email.message.Message"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.message.Message</span></code></a>: Representing an email message using the <a class="reference internal" href="email.policy.html#email.policy.compat32" title="email.policy.compat32"><code class="xref py py-data docutils literal notranslate"><span class="pre">compat32</span></code></a> API<a class="headerlink" href="#email-message-message-representing-an-email-message-using-the-compat32-api" title="Link to this heading">¶</a></h1>
  162. <p>The <a class="reference internal" href="#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> class is very similar to the
  163. <a class="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">EmailMessage</span></code></a> class, without the methods added by that
  164. class, and with the default behavior of certain other methods being slightly
  165. different. We also document here some methods that, while supported by the
  166. <a class="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">EmailMessage</span></code></a> class, are not recommended unless you are
  167. dealing with legacy code.</p>
  168. <p>The philosophy and structure of the two classes is otherwise the same.</p>
  169. <p>This document describes the behavior under the default (for <a class="reference internal" href="#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a>)
  170. policy <a class="reference internal" href="email.policy.html#email.policy.Compat32" title="email.policy.Compat32"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Compat32</span></code></a>. If you are going to use another policy,
  171. you should be using the <a class="reference internal" href="email.message.html#email.message.EmailMessage" title="email.message.EmailMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">EmailMessage</span></code></a> class instead.</p>
  172. <p>An email message consists of <em>headers</em> and a <em>payload</em>. Headers must be
  173. <span class="target" id="index-0"></span><a class="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
  174. separated by a colon. The colon is not part of either the field name or the
  175. field value. The payload may be a simple text message, or a binary object, or
  176. a structured sequence of sub-messages each with their own set of headers and
  177. their own payload. The latter type of payload is indicated by the message
  178. having a MIME type such as <em class="mimetype">multipart/*</em> or
  179. <em class="mimetype">message/rfc822</em>.</p>
  180. <p>The conceptual model provided by a <a class="reference internal" href="#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> object is that of an
  181. ordered dictionary of headers with additional methods for accessing both
  182. specialized information from the headers, for accessing the payload, for
  183. generating a serialized version of the message, and for recursively walking
  184. over the object tree. Note that duplicate headers are supported but special
  185. methods must be used to access them.</p>
  186. <p>The <a class="reference internal" href="#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> pseudo-dictionary is indexed by the header names, which
  187. must be ASCII values. The values of the dictionary are strings that are
  188. supposed to contain only ASCII characters; there is some special handling for
  189. non-ASCII input, but it doesn’t always produce the correct results. Headers
  190. are stored and returned in case-preserving form, but field names are matched
  191. case-insensitively. There may also be a single envelope header, also known as
  192. the <em>Unix-From</em> header or the <code class="docutils literal notranslate"><span class="pre">From_</span></code> header. The <em>payload</em> is either a
  193. string or bytes, in the case of simple message objects, or a list of
  194. <a class="reference internal" href="#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> objects, for MIME container documents (e.g.
  195. <em class="mimetype">multipart/*</em> and <em class="mimetype">message/rfc822</em>).</p>
  196. <p>Here are the methods of the <a class="reference internal" href="#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> class:</p>
  197. <dl class="py class">
  198. <dt class="sig sig-object py" id="email.message.Message">
  199. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.message.</span></span><span class="sig-name descname"><span class="pre">Message</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">policy</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">compat32</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message" title="Link to this definition">¶</a></dt>
  200. <dd><p>If <em>policy</em> is specified (it must be an instance of a <a class="reference internal" href="email.policy.html#module-email.policy" title="email.policy: Controlling the parsing and generating of messages"><code class="xref py py-mod docutils literal notranslate"><span class="pre">policy</span></code></a>
  201. class) use the rules it specifies to update and serialize the representation
  202. of the message. If <em>policy</em> is not set, use the <a class="reference internal" href="email.policy.html#email.policy.Compat32" title="email.policy.Compat32"><code class="xref py py-class docutils literal notranslate"><span class="pre">compat32</span></code></a> policy, which maintains backward compatibility with
  203. the Python 3.2 version of the email package. For more information see the
  204. <a class="reference internal" href="email.policy.html#module-email.policy" title="email.policy: Controlling the parsing and generating of messages"><code class="xref py py-mod docutils literal notranslate"><span class="pre">policy</span></code></a> documentation.</p>
  205. <div class="versionchanged">
  206. <p><span class="versionmodified changed">Changed in version 3.3: </span>The <em>policy</em> keyword argument was added.</p>
  207. </div>
  208. <dl class="py method">
  209. <dt class="sig sig-object py" id="email.message.Message.as_string">
  210. <span class="sig-name descname"><span class="pre">as_string</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">unixfrom</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">maxheaderlen</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">policy</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.as_string" title="Link to this definition">¶</a></dt>
  211. <dd><p>Return the entire message flattened as a string. When optional <em>unixfrom</em>
  212. is true, the envelope header is included in the returned string.
  213. <em>unixfrom</em> defaults to <code class="docutils literal notranslate"><span class="pre">False</span></code>. For backward compatibility reasons,
  214. <em>maxheaderlen</em> defaults to <code class="docutils literal notranslate"><span class="pre">0</span></code>, so if you want a different value you
  215. must override it explicitly (the value specified for <em>max_line_length</em> in
  216. the policy will be ignored by this method). The <em>policy</em> argument may be
  217. used to override the default policy obtained from the message instance.
  218. This can be used to control some of the formatting produced by the
  219. method, since the specified <em>policy</em> will be passed to the <code class="docutils literal notranslate"><span class="pre">Generator</span></code>.</p>
  220. <p>Flattening the message may trigger changes to the <a class="reference internal" href="#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> if
  221. defaults need to be filled in to complete the transformation to a string
  222. (for example, MIME boundaries may be generated or modified).</p>
  223. <p>Note that this method is provided as a convenience and may not always
  224. format the message the way you want. For example, by default it does
  225. not do the mangling of lines that begin with <code class="docutils literal notranslate"><span class="pre">From</span></code> that is
  226. required by the Unix mbox format. For more flexibility, instantiate a
  227. <a class="reference internal" href="email.generator.html#email.generator.Generator" title="email.generator.Generator"><code class="xref py py-class docutils literal notranslate"><span class="pre">Generator</span></code></a> instance and use its
  228. <a class="reference internal" href="email.generator.html#email.generator.Generator.flatten" title="email.generator.Generator.flatten"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flatten()</span></code></a> method directly. For example:</p>
  229. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">io</span> <span class="kn">import</span> <span class="n">StringIO</span>
  230. <span class="kn">from</span> <span class="nn">email.generator</span> <span class="kn">import</span> <span class="n">Generator</span>
  231. <span class="n">fp</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">()</span>
  232. <span class="n">g</span> <span class="o">=</span> <span class="n">Generator</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="n">mangle_from_</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">maxheaderlen</span><span class="o">=</span><span class="mi">60</span><span class="p">)</span>
  233. <span class="n">g</span><span class="o">.</span><span class="n">flatten</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
  234. <span class="n">text</span> <span class="o">=</span> <span class="n">fp</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>
  235. </pre></div>
  236. </div>
  237. <p>If the message object contains binary data that is not encoded according
  238. to RFC standards, the non-compliant data will be replaced by unicode
  239. “unknown character” code points. (See also <a class="reference internal" href="#email.message.Message.as_bytes" title="email.message.Message.as_bytes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">as_bytes()</span></code></a> and
  240. <a class="reference internal" href="email.generator.html#email.generator.BytesGenerator" title="email.generator.BytesGenerator"><code class="xref py py-class docutils literal notranslate"><span class="pre">BytesGenerator</span></code></a>.)</p>
  241. <div class="versionchanged">
  242. <p><span class="versionmodified changed">Changed in version 3.4: </span>the <em>policy</em> keyword argument was added.</p>
  243. </div>
  244. </dd></dl>
  245. <dl class="py method">
  246. <dt class="sig sig-object py" id="email.message.Message.__str__">
  247. <span class="sig-name descname"><span class="pre">__str__</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.__str__" title="Link to this definition">¶</a></dt>
  248. <dd><p>Equivalent to <a class="reference internal" href="#email.message.Message.as_string" title="email.message.Message.as_string"><code class="xref py py-meth docutils literal notranslate"><span class="pre">as_string()</span></code></a>. Allows <code class="docutils literal notranslate"><span class="pre">str(msg)</span></code> to produce a
  249. string containing the formatted message.</p>
  250. </dd></dl>
  251. <dl class="py method">
  252. <dt class="sig sig-object py" id="email.message.Message.as_bytes">
  253. <span class="sig-name descname"><span class="pre">as_bytes</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">unixfrom</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">policy</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.as_bytes" title="Link to this definition">¶</a></dt>
  254. <dd><p>Return the entire message flattened as a bytes object. When optional
  255. <em>unixfrom</em> is true, the envelope header is included in the returned
  256. string. <em>unixfrom</em> defaults to <code class="docutils literal notranslate"><span class="pre">False</span></code>. The <em>policy</em> argument may be
  257. used to override the default policy obtained from the message instance.
  258. This can be used to control some of the formatting produced by the
  259. method, since the specified <em>policy</em> will be passed to the
  260. <code class="docutils literal notranslate"><span class="pre">BytesGenerator</span></code>.</p>
  261. <p>Flattening the message may trigger changes to the <a class="reference internal" href="#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> if
  262. defaults need to be filled in to complete the transformation to a string
  263. (for example, MIME boundaries may be generated or modified).</p>
  264. <p>Note that this method is provided as a convenience and may not always
  265. format the message the way you want. For example, by default it does
  266. not do the mangling of lines that begin with <code class="docutils literal notranslate"><span class="pre">From</span></code> that is
  267. required by the Unix mbox format. For more flexibility, instantiate a
  268. <a class="reference internal" href="email.generator.html#email.generator.BytesGenerator" title="email.generator.BytesGenerator"><code class="xref py py-class docutils literal notranslate"><span class="pre">BytesGenerator</span></code></a> instance and use its
  269. <a class="reference internal" href="email.generator.html#email.generator.BytesGenerator.flatten" title="email.generator.BytesGenerator.flatten"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flatten()</span></code></a> method directly.
  270. For example:</p>
  271. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">io</span> <span class="kn">import</span> <span class="n">BytesIO</span>
  272. <span class="kn">from</span> <span class="nn">email.generator</span> <span class="kn">import</span> <span class="n">BytesGenerator</span>
  273. <span class="n">fp</span> <span class="o">=</span> <span class="n">BytesIO</span><span class="p">()</span>
  274. <span class="n">g</span> <span class="o">=</span> <span class="n">BytesGenerator</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="n">mangle_from_</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">maxheaderlen</span><span class="o">=</span><span class="mi">60</span><span class="p">)</span>
  275. <span class="n">g</span><span class="o">.</span><span class="n">flatten</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
  276. <span class="n">text</span> <span class="o">=</span> <span class="n">fp</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>
  277. </pre></div>
  278. </div>
  279. <div class="versionadded">
  280. <p><span class="versionmodified added">New in version 3.4.</span></p>
  281. </div>
  282. </dd></dl>
  283. <dl class="py method">
  284. <dt class="sig sig-object py" id="email.message.Message.__bytes__">
  285. <span class="sig-name descname"><span class="pre">__bytes__</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.__bytes__" title="Link to this definition">¶</a></dt>
  286. <dd><p>Equivalent to <a class="reference internal" href="#email.message.Message.as_bytes" title="email.message.Message.as_bytes"><code class="xref py py-meth docutils literal notranslate"><span class="pre">as_bytes()</span></code></a>. Allows <code class="docutils literal notranslate"><span class="pre">bytes(msg)</span></code> to produce a
  287. bytes object containing the formatted message.</p>
  288. <div class="versionadded">
  289. <p><span class="versionmodified added">New in version 3.4.</span></p>
  290. </div>
  291. </dd></dl>
  292. <dl class="py method">
  293. <dt class="sig sig-object py" id="email.message.Message.is_multipart">
  294. <span class="sig-name descname"><span class="pre">is_multipart</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.is_multipart" title="Link to this definition">¶</a></dt>
  295. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the message’s payload is a list of
  296. sub-<a class="reference internal" href="#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> objects, otherwise return <code class="docutils literal notranslate"><span class="pre">False</span></code>. When
  297. <a class="reference internal" href="#email.message.Message.is_multipart" title="email.message.Message.is_multipart"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_multipart()</span></code></a> returns <code class="docutils literal notranslate"><span class="pre">False</span></code>, the payload should be a string
  298. object (which might be a CTE encoded binary payload). (Note that
  299. <a class="reference internal" href="#email.message.Message.is_multipart" title="email.message.Message.is_multipart"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_multipart()</span></code></a> returning <code class="docutils literal notranslate"><span class="pre">True</span></code> does not necessarily mean that
  300. “msg.get_content_maintype() == ‘multipart’” will return the <code class="docutils literal notranslate"><span class="pre">True</span></code>.
  301. For example, <code class="docutils literal notranslate"><span class="pre">is_multipart</span></code> will return <code class="docutils literal notranslate"><span class="pre">True</span></code> when the
  302. <a class="reference internal" href="#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> is of type <code class="docutils literal notranslate"><span class="pre">message/rfc822</span></code>.)</p>
  303. </dd></dl>
  304. <dl class="py method">
  305. <dt class="sig sig-object py" id="email.message.Message.set_unixfrom">
  306. <span class="sig-name descname"><span class="pre">set_unixfrom</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">unixfrom</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.set_unixfrom" title="Link to this definition">¶</a></dt>
  307. <dd><p>Set the message’s envelope header to <em>unixfrom</em>, which should be a string.</p>
  308. </dd></dl>
  309. <dl class="py method">
  310. <dt class="sig sig-object py" id="email.message.Message.get_unixfrom">
  311. <span class="sig-name descname"><span class="pre">get_unixfrom</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.get_unixfrom" title="Link to this definition">¶</a></dt>
  312. <dd><p>Return the message’s envelope header. Defaults to <code class="docutils literal notranslate"><span class="pre">None</span></code> if the
  313. envelope header was never set.</p>
  314. </dd></dl>
  315. <dl class="py method">
  316. <dt class="sig sig-object py" id="email.message.Message.attach">
  317. <span class="sig-name descname"><span class="pre">attach</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">payload</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.attach" title="Link to this definition">¶</a></dt>
  318. <dd><p>Add the given <em>payload</em> to the current payload, which must be <code class="docutils literal notranslate"><span class="pre">None</span></code> or
  319. a list of <a class="reference internal" href="#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> objects before the call. After the call, the
  320. payload will always be a list of <a class="reference internal" href="#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> objects. If you want to
  321. set the payload to a scalar object (e.g. a string), use
  322. <a class="reference internal" href="#email.message.Message.set_payload" title="email.message.Message.set_payload"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_payload()</span></code></a> instead.</p>
  323. <p>This is a legacy method. On the
  324. <code class="xref py py-class docutils literal notranslate"><span class="pre">EmailMessage</span></code> class its functionality is
  325. replaced by <a class="reference internal" href="email.message.html#email.message.EmailMessage.set_content" title="email.message.EmailMessage.set_content"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_content()</span></code></a> and the
  326. related <code class="docutils literal notranslate"><span class="pre">make</span></code> and <code class="docutils literal notranslate"><span class="pre">add</span></code> methods.</p>
  327. </dd></dl>
  328. <dl class="py method">
  329. <dt class="sig sig-object py" id="email.message.Message.get_payload">
  330. <span class="sig-name descname"><span class="pre">get_payload</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">i</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">decode</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.get_payload" title="Link to this definition">¶</a></dt>
  331. <dd><p>Return the current payload, which will be a list of
  332. <a class="reference internal" href="#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> objects when <a class="reference internal" href="#email.message.Message.is_multipart" title="email.message.Message.is_multipart"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_multipart()</span></code></a> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, or a
  333. string when <a class="reference internal" href="#email.message.Message.is_multipart" title="email.message.Message.is_multipart"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_multipart()</span></code></a> is <code class="docutils literal notranslate"><span class="pre">False</span></code>. If the payload is a list
  334. and you mutate the list object, you modify the message’s payload in place.</p>
  335. <p>With optional argument <em>i</em>, <a class="reference internal" href="#email.message.Message.get_payload" title="email.message.Message.get_payload"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_payload()</span></code></a> will return the <em>i</em>-th
  336. element of the payload, counting from zero, if <a class="reference internal" href="#email.message.Message.is_multipart" title="email.message.Message.is_multipart"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_multipart()</span></code></a> is
  337. <code class="docutils literal notranslate"><span class="pre">True</span></code>. An <a class="reference internal" href="exceptions.html#IndexError" title="IndexError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IndexError</span></code></a> will be raised if <em>i</em> is less than 0 or
  338. greater than or equal to the number of items in the payload. If the
  339. payload is a string (i.e. <a class="reference internal" href="#email.message.Message.is_multipart" title="email.message.Message.is_multipart"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_multipart()</span></code></a> is <code class="docutils literal notranslate"><span class="pre">False</span></code>) and <em>i</em> is
  340. given, a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised.</p>
  341. <p>Optional <em>decode</em> is a flag indicating whether the payload should be
  342. decoded or not, according to the <em class="mailheader">Content-Transfer-Encoding</em>
  343. header. When <code class="docutils literal notranslate"><span class="pre">True</span></code> and the message is not a multipart, the payload will
  344. be decoded if this header’s value is <code class="docutils literal notranslate"><span class="pre">quoted-printable</span></code> or <code class="docutils literal notranslate"><span class="pre">base64</span></code>.
  345. If some other encoding is used, or <em class="mailheader">Content-Transfer-Encoding</em>
  346. header is missing, the payload is
  347. returned as-is (undecoded). In all cases the returned value is binary
  348. data. If the message is a multipart and the <em>decode</em> flag is <code class="docutils literal notranslate"><span class="pre">True</span></code>,
  349. then <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned. If the payload is base64 and it was not
  350. perfectly formed (missing padding, characters outside the base64
  351. alphabet), then an appropriate defect will be added to the message’s
  352. defect property (<code class="xref py py-class docutils literal notranslate"><span class="pre">InvalidBase64PaddingDefect</span></code> or
  353. <code class="xref py py-class docutils literal notranslate"><span class="pre">InvalidBase64CharactersDefect</span></code>, respectively).</p>
  354. <p>When <em>decode</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code> (the default) the body is returned as a string
  355. without decoding the <em class="mailheader">Content-Transfer-Encoding</em>. However,
  356. for a <em class="mailheader">Content-Transfer-Encoding</em> of 8bit, an attempt is made
  357. to decode the original bytes using the <code class="docutils literal notranslate"><span class="pre">charset</span></code> specified by the
  358. <em class="mailheader">Content-Type</em> header, using the <code class="docutils literal notranslate"><span class="pre">replace</span></code> error handler.
  359. If no <code class="docutils literal notranslate"><span class="pre">charset</span></code> is specified, or if the <code class="docutils literal notranslate"><span class="pre">charset</span></code> given is not
  360. recognized by the email package, the body is decoded using the default
  361. ASCII charset.</p>
  362. <p>This is a legacy method. On the
  363. <code class="xref py py-class docutils literal notranslate"><span class="pre">EmailMessage</span></code> class its functionality is
  364. replaced by <a class="reference internal" href="email.message.html#email.message.EmailMessage.get_content" title="email.message.EmailMessage.get_content"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_content()</span></code></a> and
  365. <a class="reference internal" href="email.message.html#email.message.EmailMessage.iter_parts" title="email.message.EmailMessage.iter_parts"><code class="xref py py-meth docutils literal notranslate"><span class="pre">iter_parts()</span></code></a>.</p>
  366. </dd></dl>
  367. <dl class="py method">
  368. <dt class="sig sig-object py" id="email.message.Message.set_payload">
  369. <span class="sig-name descname"><span class="pre">set_payload</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">payload</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">charset</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.set_payload" title="Link to this definition">¶</a></dt>
  370. <dd><p>Set the entire message object’s payload to <em>payload</em>. It is the client’s
  371. responsibility to ensure the payload invariants. Optional <em>charset</em> sets
  372. the message’s default character set; see <a class="reference internal" href="#email.message.Message.set_charset" title="email.message.Message.set_charset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_charset()</span></code></a> for details.</p>
  373. <p>This is a legacy method. On the
  374. <code class="xref py py-class docutils literal notranslate"><span class="pre">EmailMessage</span></code> class its functionality is
  375. replaced by <a class="reference internal" href="email.message.html#email.message.EmailMessage.set_content" title="email.message.EmailMessage.set_content"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_content()</span></code></a>.</p>
  376. </dd></dl>
  377. <dl class="py method">
  378. <dt class="sig sig-object py" id="email.message.Message.set_charset">
  379. <span class="sig-name descname"><span class="pre">set_charset</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">charset</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.set_charset" title="Link to this definition">¶</a></dt>
  380. <dd><p>Set the character set of the payload to <em>charset</em>, which can either be a
  381. <a class="reference internal" href="email.charset.html#email.charset.Charset" title="email.charset.Charset"><code class="xref py py-class docutils literal notranslate"><span class="pre">Charset</span></code></a> instance (see <a class="reference internal" href="email.charset.html#module-email.charset" title="email.charset: Character Sets"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.charset</span></code></a>), a
  382. string naming a character set, or <code class="docutils literal notranslate"><span class="pre">None</span></code>. If it is a string, it will
  383. be converted to a <a class="reference internal" href="email.charset.html#email.charset.Charset" title="email.charset.Charset"><code class="xref py py-class docutils literal notranslate"><span class="pre">Charset</span></code></a> instance. If <em>charset</em>
  384. is <code class="docutils literal notranslate"><span class="pre">None</span></code>, the <code class="docutils literal notranslate"><span class="pre">charset</span></code> parameter will be removed from the
  385. <em class="mailheader">Content-Type</em> header (the message will not be otherwise
  386. modified). Anything else will generate a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>.</p>
  387. <p>If there is no existing <em class="mailheader">MIME-Version</em> header one will be
  388. added. If there is no existing <em class="mailheader">Content-Type</em> header, one
  389. will be added with a value of <em class="mimetype">text/plain</em>. Whether the
  390. <em class="mailheader">Content-Type</em> header already exists or not, its <code class="docutils literal notranslate"><span class="pre">charset</span></code>
  391. parameter will be set to <em>charset.output_charset</em>. If
  392. <em>charset.input_charset</em> and <em>charset.output_charset</em> differ, the payload
  393. will be re-encoded to the <em>output_charset</em>. If there is no existing
  394. <em class="mailheader">Content-Transfer-Encoding</em> header, then the payload will be
  395. transfer-encoded, if needed, using the specified
  396. <a class="reference internal" href="email.charset.html#email.charset.Charset" title="email.charset.Charset"><code class="xref py py-class docutils literal notranslate"><span class="pre">Charset</span></code></a>, and a header with the appropriate value
  397. will be added. If a <em class="mailheader">Content-Transfer-Encoding</em> header
  398. already exists, the payload is assumed to already be correctly encoded
  399. using that <em class="mailheader">Content-Transfer-Encoding</em> and is not modified.</p>
  400. <p>This is a legacy method. On the
  401. <code class="xref py py-class docutils literal notranslate"><span class="pre">EmailMessage</span></code> class its functionality is
  402. replaced by the <em>charset</em> parameter of the
  403. <code class="xref py py-meth docutils literal notranslate"><span class="pre">email.emailmessage.EmailMessage.set_content()</span></code> method.</p>
  404. </dd></dl>
  405. <dl class="py method">
  406. <dt class="sig sig-object py" id="email.message.Message.get_charset">
  407. <span class="sig-name descname"><span class="pre">get_charset</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.get_charset" title="Link to this definition">¶</a></dt>
  408. <dd><p>Return the <a class="reference internal" href="email.charset.html#email.charset.Charset" title="email.charset.Charset"><code class="xref py py-class docutils literal notranslate"><span class="pre">Charset</span></code></a> instance associated with the
  409. message’s payload.</p>
  410. <p>This is a legacy method. On the
  411. <code class="xref py py-class docutils literal notranslate"><span class="pre">EmailMessage</span></code> class it always returns
  412. <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  413. </dd></dl>
  414. <p>The following methods implement a mapping-like interface for accessing the
  415. message’s <span class="target" id="index-1"></span><a class="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
  416. between these methods and a normal mapping (i.e. dictionary) interface. For
  417. example, in a dictionary there are no duplicate keys, but here there may be
  418. duplicate message headers. Also, in dictionaries there is no guaranteed
  419. order to the keys returned by <a class="reference internal" href="#email.message.Message.keys" title="email.message.Message.keys"><code class="xref py py-meth docutils literal notranslate"><span class="pre">keys()</span></code></a>, but in a <a class="reference internal" href="#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> object,
  420. headers are always returned in the order they appeared in the original
  421. message, or were added to the message later. Any header deleted and then
  422. re-added are always appended to the end of the header list.</p>
  423. <p>These semantic differences are intentional and are biased toward maximal
  424. convenience.</p>
  425. <p>Note that in all cases, any envelope header present in the message is not
  426. included in the mapping interface.</p>
  427. <p>In a model generated from bytes, any header values that (in contravention of
  428. the RFCs) contain non-ASCII bytes will, when retrieved through this
  429. interface, be represented as <a class="reference internal" href="email.header.html#email.header.Header" title="email.header.Header"><code class="xref py py-class docutils literal notranslate"><span class="pre">Header</span></code></a> objects with
  430. a charset of <code class="docutils literal notranslate"><span class="pre">unknown-8bit</span></code>.</p>
  431. <dl class="py method">
  432. <dt class="sig sig-object py" id="email.message.Message.__len__">
  433. <span class="sig-name descname"><span class="pre">__len__</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.__len__" title="Link to this definition">¶</a></dt>
  434. <dd><p>Return the total number of headers, including duplicates.</p>
  435. </dd></dl>
  436. <dl class="py method">
  437. <dt class="sig sig-object py" id="email.message.Message.__contains__">
  438. <span class="sig-name descname"><span class="pre">__contains__</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.__contains__" title="Link to this definition">¶</a></dt>
  439. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the message object has a field named <em>name</em>. Matching is
  440. done case-insensitively and <em>name</em> should not include the trailing colon.
  441. Used for the <code class="docutils literal notranslate"><span class="pre">in</span></code> operator, e.g.:</p>
  442. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="s1">&#39;message-id&#39;</span> <span class="ow">in</span> <span class="n">myMessage</span><span class="p">:</span>
  443. <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;Message-ID:&#39;</span><span class="p">,</span> <span class="n">myMessage</span><span class="p">[</span><span class="s1">&#39;message-id&#39;</span><span class="p">])</span>
  444. </pre></div>
  445. </div>
  446. </dd></dl>
  447. <dl class="py method">
  448. <dt class="sig sig-object py" id="email.message.Message.__getitem__">
  449. <span class="sig-name descname"><span class="pre">__getitem__</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.__getitem__" title="Link to this definition">¶</a></dt>
  450. <dd><p>Return the value of the named header field. <em>name</em> should not include the
  451. colon field separator. If the header is missing, <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned; a
  452. <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> is never raised.</p>
  453. <p>Note that if the named field appears more than once in the message’s
  454. headers, exactly which of those field values will be returned is
  455. undefined. Use the <a class="reference internal" href="#email.message.Message.get_all" title="email.message.Message.get_all"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_all()</span></code></a> method to get the values of all the
  456. extant named headers.</p>
  457. </dd></dl>
  458. <dl class="py method">
  459. <dt class="sig sig-object py" id="email.message.Message.__setitem__">
  460. <span class="sig-name descname"><span class="pre">__setitem__</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">val</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.__setitem__" title="Link to this definition">¶</a></dt>
  461. <dd><p>Add a header to the message with field name <em>name</em> and value <em>val</em>. The
  462. field is appended to the end of the message’s existing fields.</p>
  463. <p>Note that this does <em>not</em> overwrite or delete any existing header with the same
  464. name. If you want to ensure that the new header is the only one present in the
  465. message with field name <em>name</em>, delete the field first, e.g.:</p>
  466. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">del</span> <span class="n">msg</span><span class="p">[</span><span class="s1">&#39;subject&#39;</span><span class="p">]</span>
  467. <span class="n">msg</span><span class="p">[</span><span class="s1">&#39;subject&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;Python roolz!&#39;</span>
  468. </pre></div>
  469. </div>
  470. </dd></dl>
  471. <dl class="py method">
  472. <dt class="sig sig-object py" id="email.message.Message.__delitem__">
  473. <span class="sig-name descname"><span class="pre">__delitem__</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.__delitem__" title="Link to this definition">¶</a></dt>
  474. <dd><p>Delete all occurrences of the field with name <em>name</em> from the message’s
  475. headers. No exception is raised if the named field isn’t present in the
  476. headers.</p>
  477. </dd></dl>
  478. <dl class="py method">
  479. <dt class="sig sig-object py" id="email.message.Message.keys">
  480. <span class="sig-name descname"><span class="pre">keys</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.keys" title="Link to this definition">¶</a></dt>
  481. <dd><p>Return a list of all the message’s header field names.</p>
  482. </dd></dl>
  483. <dl class="py method">
  484. <dt class="sig sig-object py" id="email.message.Message.values">
  485. <span class="sig-name descname"><span class="pre">values</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.values" title="Link to this definition">¶</a></dt>
  486. <dd><p>Return a list of all the message’s field values.</p>
  487. </dd></dl>
  488. <dl class="py method">
  489. <dt class="sig sig-object py" id="email.message.Message.items">
  490. <span class="sig-name descname"><span class="pre">items</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.items" title="Link to this definition">¶</a></dt>
  491. <dd><p>Return a list of 2-tuples containing all the message’s field headers and
  492. values.</p>
  493. </dd></dl>
  494. <dl class="py method">
  495. <dt class="sig sig-object py" id="email.message.Message.get">
  496. <span class="sig-name descname"><span class="pre">get</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">failobj</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.get" title="Link to this definition">¶</a></dt>
  497. <dd><p>Return the value of the named header field. This is identical to
  498. <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getitem__()</span></code></a> except that optional <em>failobj</em> is returned if the
  499. named header is missing (defaults to <code class="docutils literal notranslate"><span class="pre">None</span></code>).</p>
  500. </dd></dl>
  501. <p>Here are some additional useful methods:</p>
  502. <dl class="py method">
  503. <dt class="sig sig-object py" id="email.message.Message.get_all">
  504. <span class="sig-name descname"><span class="pre">get_all</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">failobj</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.get_all" title="Link to this definition">¶</a></dt>
  505. <dd><p>Return a list of all the values for the field named <em>name</em>. If there are
  506. no such named headers in the message, <em>failobj</em> is returned (defaults to
  507. <code class="docutils literal notranslate"><span class="pre">None</span></code>).</p>
  508. </dd></dl>
  509. <dl class="py method">
  510. <dt class="sig sig-object py" id="email.message.Message.add_header">
  511. <span class="sig-name descname"><span class="pre">add_header</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">_name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">_value</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">_params</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.add_header" title="Link to this definition">¶</a></dt>
  512. <dd><p>Extended header setting. This method is similar to <a class="reference internal" href="#email.message.Message.__setitem__" title="email.message.Message.__setitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__setitem__()</span></code></a>
  513. except that additional header parameters can be provided as keyword
  514. arguments. <em>_name</em> is the header field to add and <em>_value</em> is the
  515. <em>primary</em> value for the header.</p>
  516. <p>For each item in the keyword argument dictionary <em>_params</em>, the key is
  517. taken as the parameter name, with underscores converted to dashes (since
  518. dashes are illegal in Python identifiers). Normally, the parameter will
  519. be added as <code class="docutils literal notranslate"><span class="pre">key=&quot;value&quot;</span></code> unless the value is <code class="docutils literal notranslate"><span class="pre">None</span></code>, in which case
  520. only the key will be added. If the value contains non-ASCII characters,
  521. it can be specified as a three tuple in the format
  522. <code class="docutils literal notranslate"><span class="pre">(CHARSET,</span> <span class="pre">LANGUAGE,</span> <span class="pre">VALUE)</span></code>, where <code class="docutils literal notranslate"><span class="pre">CHARSET</span></code> is a string naming the
  523. charset to be used to encode the value, <code class="docutils literal notranslate"><span class="pre">LANGUAGE</span></code> can usually be set
  524. to <code class="docutils literal notranslate"><span class="pre">None</span></code> or the empty string (see <span class="target" id="index-2"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2231.html"><strong>RFC 2231</strong></a> for other possibilities),
  525. and <code class="docutils literal notranslate"><span class="pre">VALUE</span></code> is the string value containing non-ASCII code points. If
  526. a three tuple is not passed and the value contains non-ASCII characters,
  527. it is automatically encoded in <span class="target" id="index-3"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2231.html"><strong>RFC 2231</strong></a> format using a <code class="docutils literal notranslate"><span class="pre">CHARSET</span></code>
  528. of <code class="docutils literal notranslate"><span class="pre">utf-8</span></code> and a <code class="docutils literal notranslate"><span class="pre">LANGUAGE</span></code> of <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  529. <p>Here’s an example:</p>
  530. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">msg</span><span class="o">.</span><span class="n">add_header</span><span class="p">(</span><span class="s1">&#39;Content-Disposition&#39;</span><span class="p">,</span> <span class="s1">&#39;attachment&#39;</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="s1">&#39;bud.gif&#39;</span><span class="p">)</span>
  531. </pre></div>
  532. </div>
  533. <p>This will add a header that looks like</p>
  534. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">Content</span><span class="o">-</span><span class="n">Disposition</span><span class="p">:</span> <span class="n">attachment</span><span class="p">;</span> <span class="n">filename</span><span class="o">=</span><span class="s2">&quot;bud.gif&quot;</span>
  535. </pre></div>
  536. </div>
  537. <p>An example with non-ASCII characters:</p>
  538. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">msg</span><span class="o">.</span><span class="n">add_header</span><span class="p">(</span><span class="s1">&#39;Content-Disposition&#39;</span><span class="p">,</span> <span class="s1">&#39;attachment&#39;</span><span class="p">,</span>
  539. <span class="n">filename</span><span class="o">=</span><span class="p">(</span><span class="s1">&#39;iso-8859-1&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="s1">&#39;Fußballer.ppt&#39;</span><span class="p">))</span>
  540. </pre></div>
  541. </div>
  542. <p>Which produces</p>
  543. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">Content</span><span class="o">-</span><span class="n">Disposition</span><span class="p">:</span> <span class="n">attachment</span><span class="p">;</span> <span class="n">filename</span><span class="o">*=</span><span class="s2">&quot;iso-8859-1&#39;&#39;Fu%DFballer.ppt&quot;</span>
  544. </pre></div>
  545. </div>
  546. </dd></dl>
  547. <dl class="py method">
  548. <dt class="sig sig-object py" id="email.message.Message.replace_header">
  549. <span class="sig-name descname"><span class="pre">replace_header</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">_name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">_value</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.replace_header" title="Link to this definition">¶</a></dt>
  550. <dd><p>Replace a header. Replace the first header found in the message that
  551. matches <em>_name</em>, retaining header order and field name case. If no
  552. matching header was found, a <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> is raised.</p>
  553. </dd></dl>
  554. <dl class="py method">
  555. <dt class="sig sig-object py" id="email.message.Message.get_content_type">
  556. <span class="sig-name descname"><span class="pre">get_content_type</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.get_content_type" title="Link to this definition">¶</a></dt>
  557. <dd><p>Return the message’s content type. The returned string is coerced to
  558. lower case of the form <em class="mimetype">maintype/subtype</em>. If there was no
  559. <em class="mailheader">Content-Type</em> header in the message the default type as given
  560. by <a class="reference internal" href="#email.message.Message.get_default_type" title="email.message.Message.get_default_type"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_default_type()</span></code></a> will be returned. Since according to
  561. <span class="target" id="index-4"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2045.html"><strong>RFC 2045</strong></a>, messages always have a default type, <a class="reference internal" href="#email.message.Message.get_content_type" title="email.message.Message.get_content_type"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_content_type()</span></code></a>
  562. will always return a value.</p>
  563. <p><span class="target" id="index-5"></span><a class="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 <em class="mimetype">text/plain</em>
  564. unless it appears inside a <em class="mimetype">multipart/digest</em> container, in
  565. which case it would be <em class="mimetype">message/rfc822</em>. If the
  566. <em class="mailheader">Content-Type</em> header has an invalid type specification,
  567. <span class="target" id="index-6"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2045.html"><strong>RFC 2045</strong></a> mandates that the default type be <em class="mimetype">text/plain</em>.</p>
  568. </dd></dl>
  569. <dl class="py method">
  570. <dt class="sig sig-object py" id="email.message.Message.get_content_maintype">
  571. <span class="sig-name descname"><span class="pre">get_content_maintype</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.get_content_maintype" title="Link to this definition">¶</a></dt>
  572. <dd><p>Return the message’s main content type. This is the <em class="mimetype">maintype</em>
  573. part of the string returned by <a class="reference internal" href="#email.message.Message.get_content_type" title="email.message.Message.get_content_type"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_content_type()</span></code></a>.</p>
  574. </dd></dl>
  575. <dl class="py method">
  576. <dt class="sig sig-object py" id="email.message.Message.get_content_subtype">
  577. <span class="sig-name descname"><span class="pre">get_content_subtype</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.get_content_subtype" title="Link to this definition">¶</a></dt>
  578. <dd><p>Return the message’s sub-content type. This is the <em class="mimetype">subtype</em>
  579. part of the string returned by <a class="reference internal" href="#email.message.Message.get_content_type" title="email.message.Message.get_content_type"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_content_type()</span></code></a>.</p>
  580. </dd></dl>
  581. <dl class="py method">
  582. <dt class="sig sig-object py" id="email.message.Message.get_default_type">
  583. <span class="sig-name descname"><span class="pre">get_default_type</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.get_default_type" title="Link to this definition">¶</a></dt>
  584. <dd><p>Return the default content type. Most messages have a default content
  585. type of <em class="mimetype">text/plain</em>, except for messages that are subparts of
  586. <em class="mimetype">multipart/digest</em> containers. Such subparts have a default
  587. content type of <em class="mimetype">message/rfc822</em>.</p>
  588. </dd></dl>
  589. <dl class="py method">
  590. <dt class="sig sig-object py" id="email.message.Message.set_default_type">
  591. <span class="sig-name descname"><span class="pre">set_default_type</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">ctype</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.set_default_type" title="Link to this definition">¶</a></dt>
  592. <dd><p>Set the default content type. <em>ctype</em> should either be
  593. <em class="mimetype">text/plain</em> or <em class="mimetype">message/rfc822</em>, although this is not
  594. enforced. The default content type is not stored in the
  595. <em class="mailheader">Content-Type</em> header.</p>
  596. </dd></dl>
  597. <dl class="py method">
  598. <dt class="sig sig-object py" id="email.message.Message.get_params">
  599. <span class="sig-name descname"><span class="pre">get_params</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">failobj</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">header</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'content-type'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">unquote</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.get_params" title="Link to this definition">¶</a></dt>
  600. <dd><p>Return the message’s <em class="mailheader">Content-Type</em> parameters, as a list.
  601. The elements of the returned list are 2-tuples of key/value pairs, as
  602. split on the <code class="docutils literal notranslate"><span class="pre">'='</span></code> sign. The left hand side of the <code class="docutils literal notranslate"><span class="pre">'='</span></code> is the key,
  603. while the right hand side is the value. If there is no <code class="docutils literal notranslate"><span class="pre">'='</span></code> sign in
  604. the parameter the value is the empty string, otherwise the value is as
  605. described in <a class="reference internal" href="#email.message.Message.get_param" title="email.message.Message.get_param"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_param()</span></code></a> and is unquoted if optional <em>unquote</em> is
  606. <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default).</p>
  607. <p>Optional <em>failobj</em> is the object to return if there is no
  608. <em class="mailheader">Content-Type</em> header. Optional <em>header</em> is the header to
  609. search instead of <em class="mailheader">Content-Type</em>.</p>
  610. <p>This is a legacy method. On the
  611. <code class="xref py py-class docutils literal notranslate"><span class="pre">EmailMessage</span></code> class its functionality is
  612. replaced by the <em>params</em> property of the individual header objects
  613. returned by the header access methods.</p>
  614. </dd></dl>
  615. <dl class="py method">
  616. <dt class="sig sig-object py" id="email.message.Message.get_param">
  617. <span class="sig-name descname"><span class="pre">get_param</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">param</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">failobj</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">header</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'content-type'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">unquote</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.get_param" title="Link to this definition">¶</a></dt>
  618. <dd><p>Return the value of the <em class="mailheader">Content-Type</em> header’s parameter
  619. <em>param</em> as a string. If the message has no <em class="mailheader">Content-Type</em>
  620. header or if there is no such parameter, then <em>failobj</em> is returned
  621. (defaults to <code class="docutils literal notranslate"><span class="pre">None</span></code>).</p>
  622. <p>Optional <em>header</em> if given, specifies the message header to use instead of
  623. <em class="mailheader">Content-Type</em>.</p>
  624. <p>Parameter keys are always compared case insensitively. The return value
  625. can either be a string, or a 3-tuple if the parameter was <span class="target" id="index-7"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2231.html"><strong>RFC 2231</strong></a>
  626. encoded. When it’s a 3-tuple, the elements of the value are of the form
  627. <code class="docutils literal notranslate"><span class="pre">(CHARSET,</span> <span class="pre">LANGUAGE,</span> <span class="pre">VALUE)</span></code>. Note that both <code class="docutils literal notranslate"><span class="pre">CHARSET</span></code> and
  628. <code class="docutils literal notranslate"><span class="pre">LANGUAGE</span></code> can be <code class="docutils literal notranslate"><span class="pre">None</span></code>, in which case you should consider <code class="docutils literal notranslate"><span class="pre">VALUE</span></code>
  629. to be encoded in the <code class="docutils literal notranslate"><span class="pre">us-ascii</span></code> charset. You can usually ignore
  630. <code class="docutils literal notranslate"><span class="pre">LANGUAGE</span></code>.</p>
  631. <p>If your application doesn’t care whether the parameter was encoded as in
  632. <span class="target" id="index-8"></span><a class="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
  633. <a class="reference internal" href="email.utils.html#email.utils.collapse_rfc2231_value" title="email.utils.collapse_rfc2231_value"><code class="xref py py-func docutils literal notranslate"><span class="pre">email.utils.collapse_rfc2231_value()</span></code></a>, passing in the return value
  634. from <a class="reference internal" href="#email.message.Message.get_param" title="email.message.Message.get_param"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_param()</span></code></a>. This will return a suitably decoded Unicode
  635. string when the value is a tuple, or the original string unquoted if it
  636. isn’t. For example:</p>
  637. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">rawparam</span> <span class="o">=</span> <span class="n">msg</span><span class="o">.</span><span class="n">get_param</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">)</span>
  638. <span class="n">param</span> <span class="o">=</span> <span class="n">email</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">collapse_rfc2231_value</span><span class="p">(</span><span class="n">rawparam</span><span class="p">)</span>
  639. </pre></div>
  640. </div>
  641. <p>In any case, the parameter value (either the returned string, or the
  642. <code class="docutils literal notranslate"><span class="pre">VALUE</span></code> item in the 3-tuple) is always unquoted, unless <em>unquote</em> is set
  643. to <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  644. <p>This is a legacy method. On the
  645. <code class="xref py py-class docutils literal notranslate"><span class="pre">EmailMessage</span></code> class its functionality is
  646. replaced by the <em>params</em> property of the individual header objects
  647. returned by the header access methods.</p>
  648. </dd></dl>
  649. <dl class="py method">
  650. <dt class="sig sig-object py" id="email.message.Message.set_param">
  651. <span class="sig-name descname"><span class="pre">set_param</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">param</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">header</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'Content-Type'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">requote</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">charset</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">language</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">replace</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.set_param" title="Link to this definition">¶</a></dt>
  652. <dd><p>Set a parameter in the <em class="mailheader">Content-Type</em> header. If the
  653. parameter already exists in the header, its value will be replaced with
  654. <em>value</em>. If the <em class="mailheader">Content-Type</em> header as not yet been defined
  655. for this message, it will be set to <em class="mimetype">text/plain</em> and the new
  656. parameter value will be appended as per <span class="target" id="index-9"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2045.html"><strong>RFC 2045</strong></a>.</p>
  657. <p>Optional <em>header</em> specifies an alternative header to
  658. <em class="mailheader">Content-Type</em>, and all parameters will be quoted as necessary
  659. unless optional <em>requote</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code> (the default is <code class="docutils literal notranslate"><span class="pre">True</span></code>).</p>
  660. <p>If optional <em>charset</em> is specified, the parameter will be encoded
  661. according to <span class="target" id="index-10"></span><a class="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
  662. language, defaulting to the empty string. Both <em>charset</em> and <em>language</em>
  663. should be strings.</p>
  664. <p>If <em>replace</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code> (the default) the header is moved to the
  665. end of the list of headers. If <em>replace</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, the header
  666. will be updated in place.</p>
  667. <div class="versionchanged">
  668. <p><span class="versionmodified changed">Changed in version 3.4: </span><code class="docutils literal notranslate"><span class="pre">replace</span></code> keyword was added.</p>
  669. </div>
  670. </dd></dl>
  671. <dl class="py method">
  672. <dt class="sig sig-object py" id="email.message.Message.del_param">
  673. <span class="sig-name descname"><span class="pre">del_param</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">param</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">header</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'content-type'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">requote</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.del_param" title="Link to this definition">¶</a></dt>
  674. <dd><p>Remove the given parameter completely from the <em class="mailheader">Content-Type</em>
  675. header. The header will be re-written in place without the parameter or
  676. its value. All values will be quoted as necessary unless <em>requote</em> is
  677. <code class="docutils literal notranslate"><span class="pre">False</span></code> (the default is <code class="docutils literal notranslate"><span class="pre">True</span></code>). Optional <em>header</em> specifies an
  678. alternative to <em class="mailheader">Content-Type</em>.</p>
  679. </dd></dl>
  680. <dl class="py method">
  681. <dt class="sig sig-object py" id="email.message.Message.set_type">
  682. <span class="sig-name descname"><span class="pre">set_type</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">type</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">header</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'Content-Type'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">requote</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.set_type" title="Link to this definition">¶</a></dt>
  683. <dd><p>Set the main type and subtype for the <em class="mailheader">Content-Type</em>
  684. header. <em>type</em> must be a string in the form <em class="mimetype">maintype/subtype</em>,
  685. otherwise a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised.</p>
  686. <p>This method replaces the <em class="mailheader">Content-Type</em> header, keeping all
  687. the parameters in place. If <em>requote</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, this leaves the
  688. existing header’s quoting as is, otherwise the parameters will be quoted
  689. (the default).</p>
  690. <p>An alternative header can be specified in the <em>header</em> argument. When the
  691. <em class="mailheader">Content-Type</em> header is set a <em class="mailheader">MIME-Version</em>
  692. header is also added.</p>
  693. <p>This is a legacy method. On the
  694. <code class="xref py py-class docutils literal notranslate"><span class="pre">EmailMessage</span></code> class its functionality is
  695. replaced by the <code class="docutils literal notranslate"><span class="pre">make_</span></code> and <code class="docutils literal notranslate"><span class="pre">add_</span></code> methods.</p>
  696. </dd></dl>
  697. <dl class="py method">
  698. <dt class="sig sig-object py" id="email.message.Message.get_filename">
  699. <span class="sig-name descname"><span class="pre">get_filename</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">failobj</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.get_filename" title="Link to this definition">¶</a></dt>
  700. <dd><p>Return the value of the <code class="docutils literal notranslate"><span class="pre">filename</span></code> parameter of the
  701. <em class="mailheader">Content-Disposition</em> header of the message. If the header
  702. does not have a <code class="docutils literal notranslate"><span class="pre">filename</span></code> parameter, this method falls back to looking
  703. for the <code class="docutils literal notranslate"><span class="pre">name</span></code> parameter on the <em class="mailheader">Content-Type</em> header. If
  704. neither is found, or the header is missing, then <em>failobj</em> is returned.
  705. The returned string will always be unquoted as per
  706. <a class="reference internal" href="email.utils.html#email.utils.unquote" title="email.utils.unquote"><code class="xref py py-func docutils literal notranslate"><span class="pre">email.utils.unquote()</span></code></a>.</p>
  707. </dd></dl>
  708. <dl class="py method">
  709. <dt class="sig sig-object py" id="email.message.Message.get_boundary">
  710. <span class="sig-name descname"><span class="pre">get_boundary</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">failobj</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.get_boundary" title="Link to this definition">¶</a></dt>
  711. <dd><p>Return the value of the <code class="docutils literal notranslate"><span class="pre">boundary</span></code> parameter of the
  712. <em class="mailheader">Content-Type</em> header of the message, or <em>failobj</em> if either
  713. the header is missing, or has no <code class="docutils literal notranslate"><span class="pre">boundary</span></code> parameter. The returned
  714. string will always be unquoted as per <a class="reference internal" href="email.utils.html#email.utils.unquote" title="email.utils.unquote"><code class="xref py py-func docutils literal notranslate"><span class="pre">email.utils.unquote()</span></code></a>.</p>
  715. </dd></dl>
  716. <dl class="py method">
  717. <dt class="sig sig-object py" id="email.message.Message.set_boundary">
  718. <span class="sig-name descname"><span class="pre">set_boundary</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">boundary</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.set_boundary" title="Link to this definition">¶</a></dt>
  719. <dd><p>Set the <code class="docutils literal notranslate"><span class="pre">boundary</span></code> parameter of the <em class="mailheader">Content-Type</em> header to
  720. <em>boundary</em>. <a class="reference internal" href="#email.message.Message.set_boundary" title="email.message.Message.set_boundary"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_boundary()</span></code></a> will always quote <em>boundary</em> if
  721. necessary. A <a class="reference internal" href="email.errors.html#email.errors.HeaderParseError" title="email.errors.HeaderParseError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HeaderParseError</span></code></a> is raised if the
  722. message object has no <em class="mailheader">Content-Type</em> header.</p>
  723. <p>Note that using this method is subtly different than deleting the old
  724. <em class="mailheader">Content-Type</em> header and adding a new one with the new
  725. boundary via <a class="reference internal" href="#email.message.Message.add_header" title="email.message.Message.add_header"><code class="xref py py-meth docutils literal notranslate"><span class="pre">add_header()</span></code></a>, because <a class="reference internal" href="#email.message.Message.set_boundary" title="email.message.Message.set_boundary"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_boundary()</span></code></a> preserves
  726. the order of the <em class="mailheader">Content-Type</em> header in the list of
  727. headers. However, it does <em>not</em> preserve any continuation lines which may
  728. have been present in the original <em class="mailheader">Content-Type</em> header.</p>
  729. </dd></dl>
  730. <dl class="py method">
  731. <dt class="sig sig-object py" id="email.message.Message.get_content_charset">
  732. <span class="sig-name descname"><span class="pre">get_content_charset</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">failobj</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.get_content_charset" title="Link to this definition">¶</a></dt>
  733. <dd><p>Return the <code class="docutils literal notranslate"><span class="pre">charset</span></code> parameter of the <em class="mailheader">Content-Type</em> header,
  734. coerced to lower case. If there is no <em class="mailheader">Content-Type</em> header, or if
  735. that header has no <code class="docutils literal notranslate"><span class="pre">charset</span></code> parameter, <em>failobj</em> is returned.</p>
  736. <p>Note that this method differs from <a class="reference internal" href="#email.message.Message.get_charset" title="email.message.Message.get_charset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_charset()</span></code></a> which returns the
  737. <a class="reference internal" href="email.charset.html#email.charset.Charset" title="email.charset.Charset"><code class="xref py py-class docutils literal notranslate"><span class="pre">Charset</span></code></a> instance for the default encoding of the message body.</p>
  738. </dd></dl>
  739. <dl class="py method">
  740. <dt class="sig sig-object py" id="email.message.Message.get_charsets">
  741. <span class="sig-name descname"><span class="pre">get_charsets</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">failobj</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.get_charsets" title="Link to this definition">¶</a></dt>
  742. <dd><p>Return a list containing the character set names in the message. If the
  743. message is a <em class="mimetype">multipart</em>, then the list will contain one element
  744. for each subpart in the payload, otherwise, it will be a list of length 1.</p>
  745. <p>Each item in the list will be a string which is the value of the
  746. <code class="docutils literal notranslate"><span class="pre">charset</span></code> parameter in the <em class="mailheader">Content-Type</em> header for the
  747. represented subpart. However, if the subpart has no
  748. <em class="mailheader">Content-Type</em> header, no <code class="docutils literal notranslate"><span class="pre">charset</span></code> parameter, or is not of
  749. the <em class="mimetype">text</em> main MIME type, then that item in the returned list
  750. will be <em>failobj</em>.</p>
  751. </dd></dl>
  752. <dl class="py method">
  753. <dt class="sig sig-object py" id="email.message.Message.get_content_disposition">
  754. <span class="sig-name descname"><span class="pre">get_content_disposition</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.get_content_disposition" title="Link to this definition">¶</a></dt>
  755. <dd><p>Return the lowercased value (without parameters) of the message’s
  756. <em class="mailheader">Content-Disposition</em> header if it has one, or <code class="docutils literal notranslate"><span class="pre">None</span></code>. The
  757. possible values for this method are <em>inline</em>, <em>attachment</em> or <code class="docutils literal notranslate"><span class="pre">None</span></code>
  758. if the message follows <span class="target" id="index-11"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2183.html"><strong>RFC 2183</strong></a>.</p>
  759. <div class="versionadded">
  760. <p><span class="versionmodified added">New in version 3.5.</span></p>
  761. </div>
  762. </dd></dl>
  763. <dl class="py method">
  764. <dt class="sig sig-object py" id="email.message.Message.walk">
  765. <span class="sig-name descname"><span class="pre">walk</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#email.message.Message.walk" title="Link to this definition">¶</a></dt>
  766. <dd><p>The <a class="reference internal" href="#email.message.Message.walk" title="email.message.Message.walk"><code class="xref py py-meth docutils literal notranslate"><span class="pre">walk()</span></code></a> method is an all-purpose generator which can be used to
  767. iterate over all the parts and subparts of a message object tree, in
  768. depth-first traversal order. You will typically use <a class="reference internal" href="#email.message.Message.walk" title="email.message.Message.walk"><code class="xref py py-meth docutils literal notranslate"><span class="pre">walk()</span></code></a> as the
  769. iterator in a <code class="docutils literal notranslate"><span class="pre">for</span></code> loop; each iteration returns the next subpart.</p>
  770. <p>Here’s an example that prints the MIME type of every part of a multipart
  771. message structure:</p>
  772. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">part</span> <span class="ow">in</span> <span class="n">msg</span><span class="o">.</span><span class="n">walk</span><span class="p">():</span>
  773. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">part</span><span class="o">.</span><span class="n">get_content_type</span><span class="p">())</span>
  774. <span class="go">multipart/report</span>
  775. <span class="go">text/plain</span>
  776. <span class="go">message/delivery-status</span>
  777. <span class="go">text/plain</span>
  778. <span class="go">text/plain</span>
  779. <span class="go">message/rfc822</span>
  780. <span class="go">text/plain</span>
  781. </pre></div>
  782. </div>
  783. <p><code class="docutils literal notranslate"><span class="pre">walk</span></code> iterates over the subparts of any part where
  784. <a class="reference internal" href="#email.message.Message.is_multipart" title="email.message.Message.is_multipart"><code class="xref py py-meth docutils literal notranslate"><span class="pre">is_multipart()</span></code></a> returns <code class="docutils literal notranslate"><span class="pre">True</span></code>, even though
  785. <code class="docutils literal notranslate"><span class="pre">msg.get_content_maintype()</span> <span class="pre">==</span> <span class="pre">'multipart'</span></code> may return <code class="docutils literal notranslate"><span class="pre">False</span></code>. We
  786. can see this in our example by making use of the <code class="docutils literal notranslate"><span class="pre">_structure</span></code> debug
  787. helper function:</p>
  788. <div class="highlight-pycon notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">part</span> <span class="ow">in</span> <span class="n">msg</span><span class="o">.</span><span class="n">walk</span><span class="p">():</span>
  789. <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">part</span><span class="o">.</span><span class="n">get_content_maintype</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;multipart&#39;</span><span class="p">,</span>
  790. <span class="gp">... </span> <span class="n">part</span><span class="o">.</span><span class="n">is_multipart</span><span class="p">())</span>
  791. <span class="go">True True</span>
  792. <span class="go">False False</span>
  793. <span class="go">False True</span>
  794. <span class="go">False False</span>
  795. <span class="go">False False</span>
  796. <span class="go">False True</span>
  797. <span class="go">False False</span>
  798. <span class="gp">&gt;&gt;&gt; </span><span class="n">_structure</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
  799. <span class="go">multipart/report</span>
  800. <span class="go"> text/plain</span>
  801. <span class="go"> message/delivery-status</span>
  802. <span class="go"> text/plain</span>
  803. <span class="go"> text/plain</span>
  804. <span class="go"> message/rfc822</span>
  805. <span class="go"> text/plain</span>
  806. </pre></div>
  807. </div>
  808. <p>Here the <code class="docutils literal notranslate"><span class="pre">message</span></code> parts are not <code class="docutils literal notranslate"><span class="pre">multiparts</span></code>, but they do contain
  809. subparts. <code class="docutils literal notranslate"><span class="pre">is_multipart()</span></code> returns <code class="docutils literal notranslate"><span class="pre">True</span></code> and <code class="docutils literal notranslate"><span class="pre">walk</span></code> descends
  810. into the subparts.</p>
  811. </dd></dl>
  812. <p><a class="reference internal" href="#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> objects can also optionally contain two instance attributes,
  813. which can be used when generating the plain text of a MIME message.</p>
  814. <dl class="py attribute">
  815. <dt class="sig sig-object py" id="email.message.Message.preamble">
  816. <span class="sig-name descname"><span class="pre">preamble</span></span><a class="headerlink" href="#email.message.Message.preamble" title="Link to this definition">¶</a></dt>
  817. <dd><p>The format of a MIME document allows for some text between the blank line
  818. following the headers, and the first multipart boundary string. Normally,
  819. this text is never visible in a MIME-aware mail reader because it falls
  820. outside the standard MIME armor. However, when viewing the raw text of
  821. the message, or when viewing the message in a non-MIME aware reader, this
  822. text can become visible.</p>
  823. <p>The <em>preamble</em> attribute contains this leading extra-armor text for MIME
  824. documents. When the <a class="reference internal" href="email.parser.html#email.parser.Parser" title="email.parser.Parser"><code class="xref py py-class docutils literal notranslate"><span class="pre">Parser</span></code></a> discovers some text
  825. after the headers but before the first boundary string, it assigns this
  826. text to the message’s <em>preamble</em> attribute. When the
  827. <a class="reference internal" href="email.generator.html#email.generator.Generator" title="email.generator.Generator"><code class="xref py py-class docutils literal notranslate"><span class="pre">Generator</span></code></a> is writing out the plain text
  828. representation of a MIME message, and it finds the
  829. message has a <em>preamble</em> attribute, it will write this text in the area
  830. between the headers and the first boundary. See <a class="reference internal" href="email.parser.html#module-email.parser" title="email.parser: Parse flat text email messages to produce a message object structure."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.parser</span></code></a> and
  831. <a class="reference internal" href="email.generator.html#module-email.generator" title="email.generator: Generate flat text email messages from a message structure."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.generator</span></code></a> for details.</p>
  832. <p>Note that if the message object has no preamble, the <em>preamble</em> attribute
  833. will be <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  834. </dd></dl>
  835. <dl class="py attribute">
  836. <dt class="sig sig-object py" id="email.message.Message.epilogue">
  837. <span class="sig-name descname"><span class="pre">epilogue</span></span><a class="headerlink" href="#email.message.Message.epilogue" title="Link to this definition">¶</a></dt>
  838. <dd><p>The <em>epilogue</em> attribute acts the same way as the <em>preamble</em> attribute,
  839. except that it contains text that appears between the last boundary and
  840. the end of the message.</p>
  841. <p>You do not need to set the epilogue to the empty string in order for the
  842. <a class="reference internal" href="email.generator.html#email.generator.Generator" title="email.generator.Generator"><code class="xref py py-class docutils literal notranslate"><span class="pre">Generator</span></code></a> to print a newline at the end of the
  843. file.</p>
  844. </dd></dl>
  845. <dl class="py attribute">
  846. <dt class="sig sig-object py" id="email.message.Message.defects">
  847. <span class="sig-name descname"><span class="pre">defects</span></span><a class="headerlink" href="#email.message.Message.defects" title="Link to this definition">¶</a></dt>
  848. <dd><p>The <em>defects</em> attribute contains a list of all the problems found when
  849. parsing this message. See <a class="reference internal" href="email.errors.html#module-email.errors" title="email.errors: The exception classes used by the email package."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.errors</span></code></a> for a detailed description
  850. of the possible parsing defects.</p>
  851. </dd></dl>
  852. </dd></dl>
  853. </section>
  854. <div class="clearer"></div>
  855. </div>
  856. </div>
  857. </div>
  858. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  859. <div class="sphinxsidebarwrapper">
  860. <div>
  861. <h4>Previous topic</h4>
  862. <p class="topless"><a href="email.examples.html"
  863. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code>: Examples</a></p>
  864. </div>
  865. <div>
  866. <h4>Next topic</h4>
  867. <p class="topless"><a href="email.mime.html"
  868. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.mime</span></code>: Creating email and MIME objects from scratch</a></p>
  869. </div>
  870. <div role="note" aria-label="source link">
  871. <h3>This Page</h3>
  872. <ul class="this-page-menu">
  873. <li><a href="../bugs.html">Report a Bug</a></li>
  874. <li>
  875. <a href="https://github.com/python/cpython/blob/main/Doc/library/email.compat32-message.rst"
  876. rel="nofollow">Show Source
  877. </a>
  878. </li>
  879. </ul>
  880. </div>
  881. </div>
  882. <div id="sidebarbutton" title="Collapse sidebar">
  883. <span>«</span>
  884. </div>
  885. </div>
  886. <div class="clearer"></div>
  887. </div>
  888. <div class="related" role="navigation" aria-label="related navigation">
  889. <h3>Navigation</h3>
  890. <ul>
  891. <li class="right" style="margin-right: 10px">
  892. <a href="../genindex.html" title="General Index"
  893. >index</a></li>
  894. <li class="right" >
  895. <a href="../py-modindex.html" title="Python Module Index"
  896. >modules</a> |</li>
  897. <li class="right" >
  898. <a href="email.mime.html" title="email.mime: Creating email and MIME objects from scratch"
  899. >next</a> |</li>
  900. <li class="right" >
  901. <a href="email.examples.html" title="email: Examples"
  902. >previous</a> |</li>
  903. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  904. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  905. <li class="switchers">
  906. <div class="language_switcher_placeholder"></div>
  907. <div class="version_switcher_placeholder"></div>
  908. </li>
  909. <li>
  910. </li>
  911. <li id="cpython-language-and-version">
  912. <a href="../index.html">3.12.3 Documentation</a> &#187;
  913. </li>
  914. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  915. <li class="nav-item nav-item-2"><a href="netdata.html" >Internet Data Handling</a> &#187;</li>
  916. <li class="nav-item nav-item-3"><a href="email.html" ><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code> — An email and MIME handling package</a> &#187;</li>
  917. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.message.Message</span></code>: Representing an email message using the <code class="xref py py-data docutils literal notranslate"><span class="pre">compat32</span></code> API</a></li>
  918. <li class="right">
  919. <div class="inline-search" role="search">
  920. <form class="inline-search" action="../search.html" method="get">
  921. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  922. <input type="submit" value="Go" />
  923. </form>
  924. </div>
  925. |
  926. </li>
  927. <li class="right">
  928. <label class="theme-selector-label">
  929. Theme
  930. <select class="theme-selector" oninput="activateTheme(this.value)">
  931. <option value="auto" selected>Auto</option>
  932. <option value="light">Light</option>
  933. <option value="dark">Dark</option>
  934. </select>
  935. </label> |</li>
  936. </ul>
  937. </div>
  938. <div class="footer">
  939. &copy;
  940. <a href="../copyright.html">
  941. Copyright
  942. </a>
  943. 2001-2024, Python Software Foundation.
  944. <br />
  945. This page is licensed under the Python Software Foundation License Version 2.
  946. <br />
  947. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  948. <br />
  949. See <a href="/license.html">History and License</a> for more information.<br />
  950. <br />
  951. The Python Software Foundation is a non-profit corporation.
  952. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  953. <br />
  954. <br />
  955. Last updated on Apr 09, 2024 (13:47 UTC).
  956. <a href="/bugs.html">Found a bug</a>?
  957. <br />
  958. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  959. </div>
  960. </body>
  961. </html>
上海开阖软件有限公司 沪ICP备12045867号-1