gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

518 行
40KB

  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.contentmanager: Managing MIME Content" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/email.contentmanager.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/email/contentmanager.py Content Manager Instances: Currently the email package provides only one concrete content manager, raw_data_manager, although more may be added in the futur..." />
  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="Source code: Lib/email/contentmanager.py Content Manager Instances: Currently the email package provides only one concrete content manager, raw_data_manager, although more may be added in the futur..." />
  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.contentmanager: Managing MIME Content &#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: Examples" href="email.examples.html" />
  33. <link rel="prev" title="email.headerregistry: Custom Header Objects" href="email.headerregistry.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/email.contentmanager.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. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.contentmanager</span></code>: Managing MIME Content</a><ul>
  86. <li><a class="reference internal" href="#content-manager-instances">Content Manager Instances</a></li>
  87. </ul>
  88. </li>
  89. </ul>
  90. </div>
  91. <div>
  92. <h4>Previous topic</h4>
  93. <p class="topless"><a href="email.headerregistry.html"
  94. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.headerregistry</span></code>: Custom Header Objects</a></p>
  95. </div>
  96. <div>
  97. <h4>Next topic</h4>
  98. <p class="topless"><a href="email.examples.html"
  99. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code>: Examples</a></p>
  100. </div>
  101. <div role="note" aria-label="source link">
  102. <h3>This Page</h3>
  103. <ul class="this-page-menu">
  104. <li><a href="../bugs.html">Report a Bug</a></li>
  105. <li>
  106. <a href="https://github.com/python/cpython/blob/main/Doc/library/email.contentmanager.rst"
  107. rel="nofollow">Show Source
  108. </a>
  109. </li>
  110. </ul>
  111. </div>
  112. </nav>
  113. </div>
  114. </div>
  115. <div class="related" role="navigation" aria-label="related navigation">
  116. <h3>Navigation</h3>
  117. <ul>
  118. <li class="right" style="margin-right: 10px">
  119. <a href="../genindex.html" title="General Index"
  120. accesskey="I">index</a></li>
  121. <li class="right" >
  122. <a href="../py-modindex.html" title="Python Module Index"
  123. >modules</a> |</li>
  124. <li class="right" >
  125. <a href="email.examples.html" title="email: Examples"
  126. accesskey="N">next</a> |</li>
  127. <li class="right" >
  128. <a href="email.headerregistry.html" title="email.headerregistry: Custom Header Objects"
  129. accesskey="P">previous</a> |</li>
  130. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  131. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  132. <li class="switchers">
  133. <div class="language_switcher_placeholder"></div>
  134. <div class="version_switcher_placeholder"></div>
  135. </li>
  136. <li>
  137. </li>
  138. <li id="cpython-language-and-version">
  139. <a href="../index.html">3.12.3 Documentation</a> &#187;
  140. </li>
  141. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  142. <li class="nav-item nav-item-2"><a href="netdata.html" >Internet Data Handling</a> &#187;</li>
  143. <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>
  144. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.contentmanager</span></code>: Managing MIME Content</a></li>
  145. <li class="right">
  146. <div class="inline-search" role="search">
  147. <form class="inline-search" action="../search.html" method="get">
  148. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  149. <input type="submit" value="Go" />
  150. </form>
  151. </div>
  152. |
  153. </li>
  154. <li class="right">
  155. <label class="theme-selector-label">
  156. Theme
  157. <select class="theme-selector" oninput="activateTheme(this.value)">
  158. <option value="auto" selected>Auto</option>
  159. <option value="light">Light</option>
  160. <option value="dark">Dark</option>
  161. </select>
  162. </label> |</li>
  163. </ul>
  164. </div>
  165. <div class="document">
  166. <div class="documentwrapper">
  167. <div class="bodywrapper">
  168. <div class="body" role="main">
  169. <section id="module-email.contentmanager">
  170. <span id="email-contentmanager-managing-mime-content"></span><h1><a class="reference internal" href="#module-email.contentmanager" title="email.contentmanager: Storing and Retrieving Content from MIME Parts"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.contentmanager</span></code></a>: Managing MIME Content<a class="headerlink" href="#module-email.contentmanager" title="Link to this heading">¶</a></h1>
  171. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/email/contentmanager.py">Lib/email/contentmanager.py</a></p>
  172. <hr class="docutils" />
  173. <div class="versionadded">
  174. <p><span class="versionmodified added">New in version 3.6: </span><a class="footnote-reference brackets" href="#id2" id="id1" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a></p>
  175. </div>
  176. <dl class="py class">
  177. <dt class="sig sig-object py" id="email.contentmanager.ContentManager">
  178. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.contentmanager.</span></span><span class="sig-name descname"><span class="pre">ContentManager</span></span><a class="headerlink" href="#email.contentmanager.ContentManager" title="Link to this definition">¶</a></dt>
  179. <dd><p>Base class for content managers. Provides the standard registry mechanisms
  180. to register converters between MIME content and other representations, as
  181. well as the <code class="docutils literal notranslate"><span class="pre">get_content</span></code> and <code class="docutils literal notranslate"><span class="pre">set_content</span></code> dispatch methods.</p>
  182. <dl class="py method">
  183. <dt class="sig sig-object py" id="email.contentmanager.ContentManager.get_content">
  184. <span class="sig-name descname"><span class="pre">get_content</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kw</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.contentmanager.ContentManager.get_content" title="Link to this definition">¶</a></dt>
  185. <dd><p>Look up a handler function based on the <code class="docutils literal notranslate"><span class="pre">mimetype</span></code> of <em>msg</em> (see next
  186. paragraph), call it, passing through all arguments, and return the result
  187. of the call. The expectation is that the handler will extract the
  188. payload from <em>msg</em> and return an object that encodes information about
  189. the extracted data.</p>
  190. <p>To find the handler, look for the following keys in the registry,
  191. stopping with the first one found:</p>
  192. <ul class="simple">
  193. <li><p>the string representing the full MIME type (<code class="docutils literal notranslate"><span class="pre">maintype/subtype</span></code>)</p></li>
  194. <li><p>the string representing the <code class="docutils literal notranslate"><span class="pre">maintype</span></code></p></li>
  195. <li><p>the empty string</p></li>
  196. </ul>
  197. <p>If none of these keys produce a handler, raise 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> for the
  198. full MIME type.</p>
  199. </dd></dl>
  200. <dl class="py method">
  201. <dt class="sig sig-object py" id="email.contentmanager.ContentManager.set_content">
  202. <span class="sig-name descname"><span class="pre">set_content</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">obj</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kw</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.contentmanager.ContentManager.set_content" title="Link to this definition">¶</a></dt>
  203. <dd><p>If the <code class="docutils literal notranslate"><span class="pre">maintype</span></code> is <code class="docutils literal notranslate"><span class="pre">multipart</span></code>, raise 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>; otherwise
  204. look up a handler function based on the type of <em>obj</em> (see next
  205. paragraph), call <a class="reference internal" href="email.message.html#email.message.EmailMessage.clear_content" title="email.message.EmailMessage.clear_content"><code class="xref py py-meth docutils literal notranslate"><span class="pre">clear_content()</span></code></a> on the
  206. <em>msg</em>, and call the handler function, passing through all arguments. The
  207. expectation is that the handler will transform and store <em>obj</em> into
  208. <em>msg</em>, possibly making other changes to <em>msg</em> as well, such as adding
  209. various MIME headers to encode information needed to interpret the stored
  210. data.</p>
  211. <p>To find the handler, obtain the type of <em>obj</em> (<code class="docutils literal notranslate"><span class="pre">typ</span> <span class="pre">=</span> <span class="pre">type(obj)</span></code>), and
  212. look for the following keys in the registry, stopping with the first one
  213. found:</p>
  214. <ul class="simple">
  215. <li><p>the type itself (<code class="docutils literal notranslate"><span class="pre">typ</span></code>)</p></li>
  216. <li><p>the type’s fully qualified name (<code class="docutils literal notranslate"><span class="pre">typ.__module__</span> <span class="pre">+</span> <span class="pre">'.'</span> <span class="pre">+</span>
  217. <span class="pre">typ.__qualname__</span></code>).</p></li>
  218. <li><p>the type’s qualname (<code class="docutils literal notranslate"><span class="pre">typ.__qualname__</span></code>)</p></li>
  219. <li><p>the type’s name (<code class="docutils literal notranslate"><span class="pre">typ.__name__</span></code>).</p></li>
  220. </ul>
  221. <p>If none of the above match, repeat all of the checks above for each of
  222. the types in the <a class="reference internal" href="../glossary.html#term-MRO"><span class="xref std std-term">MRO</span></a> (<code class="docutils literal notranslate"><span class="pre">typ.__mro__</span></code>). Finally, if no other key
  223. yields a handler, check for a handler for the key <code class="docutils literal notranslate"><span class="pre">None</span></code>. If there is
  224. no handler for <code class="docutils literal notranslate"><span class="pre">None</span></code>, raise 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> for the fully
  225. qualified name of the type.</p>
  226. <p>Also add a <em class="mailheader">MIME-Version</em> header if one is not present (see
  227. also <a class="reference internal" href="email.message.html#email.message.MIMEPart" title="email.message.MIMEPart"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMEPart</span></code></a>).</p>
  228. </dd></dl>
  229. <dl class="py method">
  230. <dt class="sig sig-object py" id="email.contentmanager.ContentManager.add_get_handler">
  231. <span class="sig-name descname"><span class="pre">add_get_handler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">handler</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.contentmanager.ContentManager.add_get_handler" title="Link to this definition">¶</a></dt>
  232. <dd><p>Record the function <em>handler</em> as the handler for <em>key</em>. For the possible
  233. values of <em>key</em>, see <a class="reference internal" href="#email.contentmanager.get_content" title="email.contentmanager.get_content"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_content()</span></code></a>.</p>
  234. </dd></dl>
  235. <dl class="py method">
  236. <dt class="sig sig-object py" id="email.contentmanager.ContentManager.add_set_handler">
  237. <span class="sig-name descname"><span class="pre">add_set_handler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">typekey</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">handler</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.contentmanager.ContentManager.add_set_handler" title="Link to this definition">¶</a></dt>
  238. <dd><p>Record <em>handler</em> as the function to call when an object of a type
  239. matching <em>typekey</em> is passed to <a class="reference internal" href="#email.contentmanager.set_content" title="email.contentmanager.set_content"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_content()</span></code></a>. For the possible
  240. values of <em>typekey</em>, see <a class="reference internal" href="#email.contentmanager.set_content" title="email.contentmanager.set_content"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_content()</span></code></a>.</p>
  241. </dd></dl>
  242. </dd></dl>
  243. <section id="content-manager-instances">
  244. <h2>Content Manager Instances<a class="headerlink" href="#content-manager-instances" title="Link to this heading">¶</a></h2>
  245. <p>Currently the email package provides only one concrete content manager,
  246. <a class="reference internal" href="#email.contentmanager.raw_data_manager" title="email.contentmanager.raw_data_manager"><code class="xref py py-data docutils literal notranslate"><span class="pre">raw_data_manager</span></code></a>, although more may be added in the future.
  247. <a class="reference internal" href="#email.contentmanager.raw_data_manager" title="email.contentmanager.raw_data_manager"><code class="xref py py-data docutils literal notranslate"><span class="pre">raw_data_manager</span></code></a> is the
  248. <a class="reference internal" href="email.policy.html#email.policy.EmailPolicy.content_manager" title="email.policy.EmailPolicy.content_manager"><code class="xref py py-attr docutils literal notranslate"><span class="pre">content_manager</span></code></a> provided by
  249. <a class="reference internal" href="email.policy.html#email.policy.EmailPolicy" title="email.policy.EmailPolicy"><code class="xref py py-attr docutils literal notranslate"><span class="pre">EmailPolicy</span></code></a> and its derivatives.</p>
  250. <dl class="py data">
  251. <dt class="sig sig-object py" id="email.contentmanager.raw_data_manager">
  252. <span class="sig-prename descclassname"><span class="pre">email.contentmanager.</span></span><span class="sig-name descname"><span class="pre">raw_data_manager</span></span><a class="headerlink" href="#email.contentmanager.raw_data_manager" title="Link to this definition">¶</a></dt>
  253. <dd><p>This content manager provides only a minimum interface beyond that provided
  254. by <a class="reference internal" href="email.compat32-message.html#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> itself: it deals only with text, raw
  255. byte strings, and <a class="reference internal" href="email.compat32-message.html#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> objects. Nevertheless, it
  256. provides significant advantages compared to the base API: <code class="docutils literal notranslate"><span class="pre">get_content</span></code> on
  257. a text part will return a unicode string without the application needing to
  258. manually decode it, <code class="docutils literal notranslate"><span class="pre">set_content</span></code> provides a rich set of options for
  259. controlling the headers added to a part and controlling the content transfer
  260. encoding, and it enables the use of the various <code class="docutils literal notranslate"><span class="pre">add_</span></code> methods, thereby
  261. simplifying the creation of multipart messages.</p>
  262. <dl class="py method">
  263. <dt class="sig sig-object py" id="email.contentmanager.get_content">
  264. <span class="sig-prename descclassname"><span class="pre">email.contentmanager.</span></span><span class="sig-name descname"><span class="pre">get_content</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">errors</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'replace'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.contentmanager.get_content" title="Link to this definition">¶</a></dt>
  265. <dd><p>Return the payload of the part as either a string (for <code class="docutils literal notranslate"><span class="pre">text</span></code> parts), an
  266. <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> object (for <code class="docutils literal notranslate"><span class="pre">message/rfc822</span></code>
  267. parts), or a <code class="docutils literal notranslate"><span class="pre">bytes</span></code> object (for all other non-multipart types). Raise
  268. 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> if called on a <code class="docutils literal notranslate"><span class="pre">multipart</span></code>. If the part is a
  269. <code class="docutils literal notranslate"><span class="pre">text</span></code> part and <em>errors</em> is specified, use it as the error handler when
  270. decoding the payload to unicode. The default error handler is
  271. <code class="docutils literal notranslate"><span class="pre">replace</span></code>.</p>
  272. </dd></dl>
  273. <dl class="py method">
  274. <dt class="sig sig-object py" id="email.contentmanager.set_content">
  275. <span class="sig-prename descclassname"><span class="pre">email.contentmanager.</span></span><span class="sig-name descname"><span class="pre">set_content</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">&lt;'str'&gt;</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">subtype=&quot;plain&quot;</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">charset='utf-8'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cte=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">disposition=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filename=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cid=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">params=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">headers=None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.contentmanager.set_content" title="Link to this definition">¶</a></dt>
  276. <dt class="sig sig-object py">
  277. <span class="sig-prename descclassname"><span class="pre">email.contentmanager.</span></span><span class="sig-name descname"><span class="pre">set_content</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">&lt;'bytes'&gt;</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">maintype</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">subtype</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cte=&quot;base64&quot;</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">disposition=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filename=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cid=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">params=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">headers=None</span></span></em><span class="sig-paren">)</span></dt>
  278. <dt class="sig sig-object py">
  279. <span class="sig-prename descclassname"><span class="pre">email.contentmanager.</span></span><span class="sig-name descname"><span class="pre">set_content</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">&lt;'EmailMessage'&gt;</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cte=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">disposition=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">filename=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cid=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">params=None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">headers=None</span></span></em><span class="sig-paren">)</span></dt>
  280. <dd><p>Add headers and payload to <em>msg</em>:</p>
  281. <p>Add a <em class="mailheader">Content-Type</em> header with a <code class="docutils literal notranslate"><span class="pre">maintype/subtype</span></code>
  282. value.</p>
  283. <ul class="simple">
  284. <li><p>For <code class="docutils literal notranslate"><span class="pre">str</span></code>, set the MIME <code class="docutils literal notranslate"><span class="pre">maintype</span></code> to <code class="docutils literal notranslate"><span class="pre">text</span></code>, and set the
  285. subtype to <em>subtype</em> if it is specified, or <code class="docutils literal notranslate"><span class="pre">plain</span></code> if it is not.</p></li>
  286. <li><p>For <code class="docutils literal notranslate"><span class="pre">bytes</span></code>, use the specified <em>maintype</em> and <em>subtype</em>, or
  287. raise 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> if they are not specified.</p></li>
  288. <li><p>For <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> objects, set the maintype
  289. to <code class="docutils literal notranslate"><span class="pre">message</span></code>, and set the subtype to <em>subtype</em> if it is
  290. specified or <code class="docutils literal notranslate"><span class="pre">rfc822</span></code> if it is not. If <em>subtype</em> is
  291. <code class="docutils literal notranslate"><span class="pre">partial</span></code>, raise an error (<code class="docutils literal notranslate"><span class="pre">bytes</span></code> objects must be used to
  292. construct <code class="docutils literal notranslate"><span class="pre">message/partial</span></code> parts).</p></li>
  293. </ul>
  294. <p>If <em>charset</em> is provided (which is valid only for <code class="docutils literal notranslate"><span class="pre">str</span></code>), encode the
  295. string to bytes using the specified character set. The default is
  296. <code class="docutils literal notranslate"><span class="pre">utf-8</span></code>. If the specified <em>charset</em> is a known alias for a standard
  297. MIME charset name, use the standard charset instead.</p>
  298. <p>If <em>cte</em> is set, encode the payload using the specified content transfer
  299. encoding, and set the <em class="mailheader">Content-Transfer-Encoding</em> header to
  300. that value. Possible values for <em>cte</em> are <code class="docutils literal notranslate"><span class="pre">quoted-printable</span></code>,
  301. <code class="docutils literal notranslate"><span class="pre">base64</span></code>, <code class="docutils literal notranslate"><span class="pre">7bit</span></code>, <code class="docutils literal notranslate"><span class="pre">8bit</span></code>, and <code class="docutils literal notranslate"><span class="pre">binary</span></code>. If the input cannot be
  302. encoded in the specified encoding (for example, specifying a <em>cte</em> of
  303. <code class="docutils literal notranslate"><span class="pre">7bit</span></code> for an input that contains non-ASCII values), raise a
  304. <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>.</p>
  305. <ul class="simple">
  306. <li><p>For <code class="docutils literal notranslate"><span class="pre">str</span></code> objects, if <em>cte</em> is not set use heuristics to
  307. determine the most compact encoding.</p></li>
  308. <li><p>For <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>, per <span class="target" id="index-0"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2046.html"><strong>RFC 2046</strong></a>, raise
  309. an error if a <em>cte</em> of <code class="docutils literal notranslate"><span class="pre">quoted-printable</span></code> or <code class="docutils literal notranslate"><span class="pre">base64</span></code> is
  310. requested for <em>subtype</em> <code class="docutils literal notranslate"><span class="pre">rfc822</span></code>, and for any <em>cte</em> other than
  311. <code class="docutils literal notranslate"><span class="pre">7bit</span></code> for <em>subtype</em> <code class="docutils literal notranslate"><span class="pre">external-body</span></code>. For
  312. <code class="docutils literal notranslate"><span class="pre">message/rfc822</span></code>, use <code class="docutils literal notranslate"><span class="pre">8bit</span></code> if <em>cte</em> is not specified. For
  313. all other values of <em>subtype</em>, use <code class="docutils literal notranslate"><span class="pre">7bit</span></code>.</p></li>
  314. </ul>
  315. <div class="admonition note">
  316. <p class="admonition-title">Note</p>
  317. <p>A <em>cte</em> of <code class="docutils literal notranslate"><span class="pre">binary</span></code> does not actually work correctly yet.
  318. The <code class="docutils literal notranslate"><span class="pre">EmailMessage</span></code> object as modified by <code class="docutils literal notranslate"><span class="pre">set_content</span></code> is
  319. correct, but <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> does not
  320. serialize it correctly.</p>
  321. </div>
  322. <p>If <em>disposition</em> is set, use it as the value of the
  323. <em class="mailheader">Content-Disposition</em> header. If not specified, and
  324. <em>filename</em> is specified, add the header with the value <code class="docutils literal notranslate"><span class="pre">attachment</span></code>.
  325. If <em>disposition</em> is not specified and <em>filename</em> is also not specified,
  326. do not add the header. The only valid values for <em>disposition</em> are
  327. <code class="docutils literal notranslate"><span class="pre">attachment</span></code> and <code class="docutils literal notranslate"><span class="pre">inline</span></code>.</p>
  328. <p>If <em>filename</em> is specified, use it as the value of the <code class="docutils literal notranslate"><span class="pre">filename</span></code>
  329. parameter of the <em class="mailheader">Content-Disposition</em> header.</p>
  330. <p>If <em>cid</em> is specified, add a <em class="mailheader">Content-ID</em> header with
  331. <em>cid</em> as its value.</p>
  332. <p>If <em>params</em> is specified, iterate its <code class="docutils literal notranslate"><span class="pre">items</span></code> method and use the
  333. resulting <code class="docutils literal notranslate"><span class="pre">(key,</span> <span class="pre">value)</span></code> pairs to set additional parameters on the
  334. <em class="mailheader">Content-Type</em> header.</p>
  335. <p>If <em>headers</em> is specified and is a list of strings of the form
  336. <code class="docutils literal notranslate"><span class="pre">headername:</span> <span class="pre">headervalue</span></code> or a list of <code class="docutils literal notranslate"><span class="pre">header</span></code> objects
  337. (distinguished from strings by having a <code class="docutils literal notranslate"><span class="pre">name</span></code> attribute), add the
  338. headers to <em>msg</em>.</p>
  339. </dd></dl>
  340. </dd></dl>
  341. <p class="rubric">Footnotes</p>
  342. <aside class="footnote-list brackets">
  343. <aside class="footnote brackets" id="id2" role="doc-footnote">
  344. <span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id1">1</a><span class="fn-bracket">]</span></span>
  345. <p>Originally added in 3.4 as a <a class="reference internal" href="../glossary.html#term-provisional-package"><span class="xref std std-term">provisional module</span></a></p>
  346. </aside>
  347. </aside>
  348. </section>
  349. </section>
  350. <div class="clearer"></div>
  351. </div>
  352. </div>
  353. </div>
  354. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  355. <div class="sphinxsidebarwrapper">
  356. <div>
  357. <h3><a href="../contents.html">Table of Contents</a></h3>
  358. <ul>
  359. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.contentmanager</span></code>: Managing MIME Content</a><ul>
  360. <li><a class="reference internal" href="#content-manager-instances">Content Manager Instances</a></li>
  361. </ul>
  362. </li>
  363. </ul>
  364. </div>
  365. <div>
  366. <h4>Previous topic</h4>
  367. <p class="topless"><a href="email.headerregistry.html"
  368. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.headerregistry</span></code>: Custom Header Objects</a></p>
  369. </div>
  370. <div>
  371. <h4>Next topic</h4>
  372. <p class="topless"><a href="email.examples.html"
  373. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code>: Examples</a></p>
  374. </div>
  375. <div role="note" aria-label="source link">
  376. <h3>This Page</h3>
  377. <ul class="this-page-menu">
  378. <li><a href="../bugs.html">Report a Bug</a></li>
  379. <li>
  380. <a href="https://github.com/python/cpython/blob/main/Doc/library/email.contentmanager.rst"
  381. rel="nofollow">Show Source
  382. </a>
  383. </li>
  384. </ul>
  385. </div>
  386. </div>
  387. <div id="sidebarbutton" title="Collapse sidebar">
  388. <span>«</span>
  389. </div>
  390. </div>
  391. <div class="clearer"></div>
  392. </div>
  393. <div class="related" role="navigation" aria-label="related navigation">
  394. <h3>Navigation</h3>
  395. <ul>
  396. <li class="right" style="margin-right: 10px">
  397. <a href="../genindex.html" title="General Index"
  398. >index</a></li>
  399. <li class="right" >
  400. <a href="../py-modindex.html" title="Python Module Index"
  401. >modules</a> |</li>
  402. <li class="right" >
  403. <a href="email.examples.html" title="email: Examples"
  404. >next</a> |</li>
  405. <li class="right" >
  406. <a href="email.headerregistry.html" title="email.headerregistry: Custom Header Objects"
  407. >previous</a> |</li>
  408. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  409. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  410. <li class="switchers">
  411. <div class="language_switcher_placeholder"></div>
  412. <div class="version_switcher_placeholder"></div>
  413. </li>
  414. <li>
  415. </li>
  416. <li id="cpython-language-and-version">
  417. <a href="../index.html">3.12.3 Documentation</a> &#187;
  418. </li>
  419. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  420. <li class="nav-item nav-item-2"><a href="netdata.html" >Internet Data Handling</a> &#187;</li>
  421. <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>
  422. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.contentmanager</span></code>: Managing MIME Content</a></li>
  423. <li class="right">
  424. <div class="inline-search" role="search">
  425. <form class="inline-search" action="../search.html" method="get">
  426. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  427. <input type="submit" value="Go" />
  428. </form>
  429. </div>
  430. |
  431. </li>
  432. <li class="right">
  433. <label class="theme-selector-label">
  434. Theme
  435. <select class="theme-selector" oninput="activateTheme(this.value)">
  436. <option value="auto" selected>Auto</option>
  437. <option value="light">Light</option>
  438. <option value="dark">Dark</option>
  439. </select>
  440. </label> |</li>
  441. </ul>
  442. </div>
  443. <div class="footer">
  444. &copy;
  445. <a href="../copyright.html">
  446. Copyright
  447. </a>
  448. 2001-2024, Python Software Foundation.
  449. <br />
  450. This page is licensed under the Python Software Foundation License Version 2.
  451. <br />
  452. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  453. <br />
  454. See <a href="/license.html">History and License</a> for more information.<br />
  455. <br />
  456. The Python Software Foundation is a non-profit corporation.
  457. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  458. <br />
  459. <br />
  460. Last updated on Apr 09, 2024 (13:47 UTC).
  461. <a href="/bugs.html">Found a bug</a>?
  462. <br />
  463. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  464. </div>
  465. </body>
  466. </html>
上海开阖软件有限公司 沪ICP备12045867号-1