gooderp18绿色标准版
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

522 lines
48KB

  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.mime: Creating email and MIME objects from scratch" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/email.mime.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/email/mime/ This module is part of the legacy ( Compat32) email API. Its functionality is partially replaced by the contentmanager in the new API, but in certain applications these..." />
  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/mime/ This module is part of the legacy ( Compat32) email API. Its functionality is partially replaced by the contentmanager in the new API, but in certain applications these..." />
  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.mime: Creating email and MIME objects from scratch &#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.header: Internationalized headers" href="email.header.html" />
  33. <link rel="prev" title="email.message.Message: Representing an email message using the compat32 API" href="email.compat32-message.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/email.mime.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.compat32-message.html"
  85. title="previous chapter"><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></p>
  86. </div>
  87. <div>
  88. <h4>Next topic</h4>
  89. <p class="topless"><a href="email.header.html"
  90. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.header</span></code>: Internationalized headers</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.mime.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.header.html" title="email.header: Internationalized headers"
  117. accesskey="N">next</a> |</li>
  118. <li class="right" >
  119. <a href="email.compat32-message.html" title="email.message.Message: Representing an email message using the compat32 API"
  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.mime</span></code>: Creating email and MIME objects from scratch</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="module-email.mime">
  161. <span id="email-mime-creating-email-and-mime-objects-from-scratch"></span><h1><a class="reference internal" href="#module-email.mime" title="email.mime: Build MIME messages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.mime</span></code></a>: Creating email and MIME objects from scratch<a class="headerlink" href="#module-email.mime" title="Link to this heading">¶</a></h1>
  162. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/email/mime/">Lib/email/mime/</a></p>
  163. <hr class="docutils" />
  164. <p>This module is part of the legacy (<code class="docutils literal notranslate"><span class="pre">Compat32</span></code>) email API. Its functionality
  165. is partially replaced by the <a class="reference internal" href="email.contentmanager.html#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">contentmanager</span></code></a> in the new API, but
  166. in certain applications these classes may still be useful, even in non-legacy
  167. code.</p>
  168. <p>Ordinarily, you get a message object structure by passing a file or some text to
  169. a parser, which parses the text and returns the root message object. However
  170. you can also build a complete message structure from scratch, or even individual
  171. <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 by hand. In fact, you can also take an
  172. existing structure and add new <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, move them
  173. around, etc. This makes a very convenient interface for slicing-and-dicing MIME
  174. messages.</p>
  175. <p>You can create a new object structure by creating <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>
  176. instances, adding attachments and all the appropriate headers manually. For MIME
  177. messages though, the <a class="reference internal" href="email.html#module-email" title="email: Package supporting the parsing, manipulating, and generating email messages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code></a> package provides some convenient subclasses to
  178. make things easier.</p>
  179. <p>Here are the classes:</p>
  180. <dl class="py class" id="module-email.mime.base">
  181. <dt class="sig sig-object py" id="email.mime.base.MIMEBase">
  182. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.mime.base.</span></span><span class="sig-name descname"><span class="pre">MIMEBase</span></span><span class="sig-paren">(</span><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="o"><span class="pre">*</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">compat32</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.mime.base.MIMEBase" title="Link to this definition">¶</a></dt>
  183. <dd><p>Module: <a class="reference internal" href="#module-email.mime.base" title="email.mime.base"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.mime.base</span></code></a></p>
  184. <p>This is the base class for all the MIME-specific subclasses of
  185. <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>. Ordinarily you won’t create instances
  186. specifically of <a class="reference internal" href="#email.mime.base.MIMEBase" title="email.mime.base.MIMEBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMEBase</span></code></a>, although you could. <a class="reference internal" href="#email.mime.base.MIMEBase" title="email.mime.base.MIMEBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMEBase</span></code></a>
  187. is provided primarily as a convenient base class for more specific
  188. MIME-aware subclasses.</p>
  189. <p><em>_maintype</em> is the <em class="mailheader">Content-Type</em> major type (e.g. <em class="mimetype">text</em>
  190. or <em class="mimetype">image</em>), and <em>_subtype</em> is the <em class="mailheader">Content-Type</em> minor
  191. type (e.g. <em class="mimetype">plain</em> or <em class="mimetype">gif</em>). <em>_params</em> is a parameter
  192. key/value dictionary and is passed directly to <a class="reference internal" href="email.compat32-message.html#email.message.Message.add_header" title="email.message.Message.add_header"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Message.add_header</span></code></a>.</p>
  193. <p>If <em>policy</em> is specified, (defaults to the
  194. <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) it will be passed to
  195. <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>.</p>
  196. <p>The <a class="reference internal" href="#email.mime.base.MIMEBase" title="email.mime.base.MIMEBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMEBase</span></code></a> class always adds a <em class="mailheader">Content-Type</em> header
  197. (based on <em>_maintype</em>, <em>_subtype</em>, and <em>_params</em>), and a
  198. <em class="mailheader">MIME-Version</em> header (always set to <code class="docutils literal notranslate"><span class="pre">1.0</span></code>).</p>
  199. <div class="versionchanged">
  200. <p><span class="versionmodified changed">Changed in version 3.6: </span>Added <em>policy</em> keyword-only parameter.</p>
  201. </div>
  202. </dd></dl>
  203. <dl class="py class" id="module-email.mime.nonmultipart">
  204. <dt class="sig sig-object py" id="email.mime.nonmultipart.MIMENonMultipart">
  205. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.mime.nonmultipart.</span></span><span class="sig-name descname"><span class="pre">MIMENonMultipart</span></span><a class="headerlink" href="#email.mime.nonmultipart.MIMENonMultipart" title="Link to this definition">¶</a></dt>
  206. <dd><p>Module: <a class="reference internal" href="#module-email.mime.nonmultipart" title="email.mime.nonmultipart"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.mime.nonmultipart</span></code></a></p>
  207. <p>A subclass of <a class="reference internal" href="#email.mime.base.MIMEBase" title="email.mime.base.MIMEBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMEBase</span></code></a>, this is an intermediate base
  208. class for MIME messages that are not <em class="mimetype">multipart</em>. The primary
  209. purpose of this class is to prevent the use of the
  210. <a class="reference internal" href="email.compat32-message.html#email.message.Message.attach" title="email.message.Message.attach"><code class="xref py py-meth docutils literal notranslate"><span class="pre">attach()</span></code></a> method, which only makes sense for
  211. <em class="mimetype">multipart</em> messages. If <a class="reference internal" href="email.compat32-message.html#email.message.Message.attach" title="email.message.Message.attach"><code class="xref py py-meth docutils literal notranslate"><span class="pre">attach()</span></code></a>
  212. is called, a <a class="reference internal" href="email.errors.html#email.errors.MultipartConversionError" title="email.errors.MultipartConversionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">MultipartConversionError</span></code></a> exception is raised.</p>
  213. </dd></dl>
  214. <dl class="py class" id="module-email.mime.multipart">
  215. <dt class="sig sig-object py" id="email.mime.multipart.MIMEMultipart">
  216. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.mime.multipart.</span></span><span class="sig-name descname"><span class="pre">MIMEMultipart</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">_subtype</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'mixed'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">boundary</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">_subparts</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="o"><span class="pre">*</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">compat32</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.mime.multipart.MIMEMultipart" title="Link to this definition">¶</a></dt>
  217. <dd><p>Module: <a class="reference internal" href="#module-email.mime.multipart" title="email.mime.multipart"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.mime.multipart</span></code></a></p>
  218. <p>A subclass of <a class="reference internal" href="#email.mime.base.MIMEBase" title="email.mime.base.MIMEBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMEBase</span></code></a>, this is an intermediate base
  219. class for MIME messages that are <em class="mimetype">multipart</em>. Optional <em>_subtype</em>
  220. defaults to <em class="mimetype">mixed</em>, but can be used to specify the subtype of the
  221. message. A <em class="mailheader">Content-Type</em> header of <em class="mimetype">multipart/_subtype</em>
  222. will be added to the message object. A <em class="mailheader">MIME-Version</em> header will
  223. also be added.</p>
  224. <p>Optional <em>boundary</em> is the multipart boundary string. When <code class="docutils literal notranslate"><span class="pre">None</span></code> (the
  225. default), the boundary is calculated when needed (for example, when the
  226. message is serialized).</p>
  227. <p><em>_subparts</em> is a sequence of initial subparts for the payload. It must be
  228. possible to convert this sequence to a list. You can always attach new subparts
  229. to the message by using the <a class="reference internal" href="email.compat32-message.html#email.message.Message.attach" title="email.message.Message.attach"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Message.attach</span></code></a> method.</p>
  230. <p>Optional <em>policy</em> argument defaults to <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>.</p>
  231. <p>Additional parameters for the <em class="mailheader">Content-Type</em> header are taken from
  232. the keyword arguments, or passed into the <em>_params</em> argument, which is a keyword
  233. dictionary.</p>
  234. <div class="versionchanged">
  235. <p><span class="versionmodified changed">Changed in version 3.6: </span>Added <em>policy</em> keyword-only parameter.</p>
  236. </div>
  237. </dd></dl>
  238. <dl class="py class" id="module-email.mime.application">
  239. <dt class="sig sig-object py" id="email.mime.application.MIMEApplication">
  240. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.mime.application.</span></span><span class="sig-name descname"><span class="pre">MIMEApplication</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">_data</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">_subtype</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'octet-stream'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">_encoder</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">email.encoders.encode_base64</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</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">compat32</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.mime.application.MIMEApplication" title="Link to this definition">¶</a></dt>
  241. <dd><p>Module: <a class="reference internal" href="#module-email.mime.application" title="email.mime.application"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.mime.application</span></code></a></p>
  242. <p>A subclass of <a class="reference internal" href="#email.mime.nonmultipart.MIMENonMultipart" title="email.mime.nonmultipart.MIMENonMultipart"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMENonMultipart</span></code></a>, the
  243. <a class="reference internal" href="#email.mime.application.MIMEApplication" title="email.mime.application.MIMEApplication"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMEApplication</span></code></a> class is used to represent MIME message objects of
  244. major type <em class="mimetype">application</em>. <em>_data</em> contains the bytes for the raw
  245. application data. Optional <em>_subtype</em> specifies the MIME subtype and defaults
  246. to <em class="mimetype">octet-stream</em>.</p>
  247. <p>Optional <em>_encoder</em> is a callable (i.e. function) which will perform the actual
  248. encoding of the data for transport. This callable takes one argument, which is
  249. the <a class="reference internal" href="#email.mime.application.MIMEApplication" title="email.mime.application.MIMEApplication"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMEApplication</span></code></a> instance. It should use
  250. <a class="reference internal" href="email.compat32-message.html#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> and
  251. <a class="reference internal" href="email.compat32-message.html#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> to change the payload to encoded
  252. form. It should also add
  253. any <em class="mailheader">Content-Transfer-Encoding</em> or other headers to the message
  254. object as necessary. The default encoding is base64. See the
  255. <a class="reference internal" href="email.encoders.html#module-email.encoders" title="email.encoders: Encoders for email message payloads."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.encoders</span></code></a> module for a list of the built-in encoders.</p>
  256. <p>Optional <em>policy</em> argument defaults to <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>.</p>
  257. <p><em>_params</em> are passed straight through to the base class constructor.</p>
  258. <div class="versionchanged">
  259. <p><span class="versionmodified changed">Changed in version 3.6: </span>Added <em>policy</em> keyword-only parameter.</p>
  260. </div>
  261. </dd></dl>
  262. <dl class="py class" id="module-email.mime.audio">
  263. <dt class="sig sig-object py" id="email.mime.audio.MIMEAudio">
  264. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.mime.audio.</span></span><span class="sig-name descname"><span class="pre">MIMEAudio</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">_audiodata</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">_subtype</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">_encoder</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">email.encoders.encode_base64</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</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">compat32</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.mime.audio.MIMEAudio" title="Link to this definition">¶</a></dt>
  265. <dd><p>Module: <a class="reference internal" href="#module-email.mime.audio" title="email.mime.audio"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.mime.audio</span></code></a></p>
  266. <p>A subclass of <a class="reference internal" href="#email.mime.nonmultipart.MIMENonMultipart" title="email.mime.nonmultipart.MIMENonMultipart"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMENonMultipart</span></code></a>, the
  267. <a class="reference internal" href="#email.mime.audio.MIMEAudio" title="email.mime.audio.MIMEAudio"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMEAudio</span></code></a> class is used to create MIME message objects of major type
  268. <em class="mimetype">audio</em>. <em>_audiodata</em> contains the bytes for the raw audio data. If
  269. this data can be decoded as au, wav, aiff, or aifc, then the
  270. subtype will be automatically included in the <em class="mailheader">Content-Type</em> header.
  271. Otherwise you can explicitly specify the audio subtype via the <em>_subtype</em>
  272. argument. If the minor type could not be guessed and <em>_subtype</em> was not given,
  273. then <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>
  274. <p>Optional <em>_encoder</em> is a callable (i.e. function) which will perform the actual
  275. encoding of the audio data for transport. This callable takes one argument,
  276. which is the <a class="reference internal" href="#email.mime.audio.MIMEAudio" title="email.mime.audio.MIMEAudio"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMEAudio</span></code></a> instance. It should use
  277. <a class="reference internal" href="email.compat32-message.html#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> and
  278. <a class="reference internal" href="email.compat32-message.html#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> to change the payload to encoded
  279. form. It should also add
  280. any <em class="mailheader">Content-Transfer-Encoding</em> or other headers to the message
  281. object as necessary. The default encoding is base64. See the
  282. <a class="reference internal" href="email.encoders.html#module-email.encoders" title="email.encoders: Encoders for email message payloads."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.encoders</span></code></a> module for a list of the built-in encoders.</p>
  283. <p>Optional <em>policy</em> argument defaults to <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>.</p>
  284. <p><em>_params</em> are passed straight through to the base class constructor.</p>
  285. <div class="versionchanged">
  286. <p><span class="versionmodified changed">Changed in version 3.6: </span>Added <em>policy</em> keyword-only parameter.</p>
  287. </div>
  288. </dd></dl>
  289. <dl class="py class" id="module-email.mime.image">
  290. <dt class="sig sig-object py" id="email.mime.image.MIMEImage">
  291. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.mime.image.</span></span><span class="sig-name descname"><span class="pre">MIMEImage</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">_imagedata</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">_subtype</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">_encoder</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">email.encoders.encode_base64</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</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">compat32</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.mime.image.MIMEImage" title="Link to this definition">¶</a></dt>
  292. <dd><p>Module: <a class="reference internal" href="#module-email.mime.image" title="email.mime.image"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.mime.image</span></code></a></p>
  293. <p>A subclass of <a class="reference internal" href="#email.mime.nonmultipart.MIMENonMultipart" title="email.mime.nonmultipart.MIMENonMultipart"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMENonMultipart</span></code></a>, the
  294. <a class="reference internal" href="#email.mime.image.MIMEImage" title="email.mime.image.MIMEImage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMEImage</span></code></a> class is used to create MIME message objects of major type
  295. <em class="mimetype">image</em>. <em>_imagedata</em> contains the bytes for the raw image data. If
  296. this data type can be detected (jpeg, png, gif, tiff, rgb, pbm, pgm, ppm,
  297. rast, xbm, bmp, webp, and exr attempted), then the subtype will be
  298. automatically included in the <em class="mailheader">Content-Type</em> header. Otherwise
  299. you can explicitly specify the image subtype via the <em>_subtype</em> argument.
  300. If the minor type could not be guessed and <em>_subtype</em> was not given, then
  301. <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>
  302. <p>Optional <em>_encoder</em> is a callable (i.e. function) which will perform the actual
  303. encoding of the image data for transport. This callable takes one argument,
  304. which is the <a class="reference internal" href="#email.mime.image.MIMEImage" title="email.mime.image.MIMEImage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMEImage</span></code></a> instance. It should use
  305. <a class="reference internal" href="email.compat32-message.html#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> and
  306. <a class="reference internal" href="email.compat32-message.html#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> to change the payload to encoded
  307. form. It should also add
  308. any <em class="mailheader">Content-Transfer-Encoding</em> or other headers to the message
  309. object as necessary. The default encoding is base64. See the
  310. <a class="reference internal" href="email.encoders.html#module-email.encoders" title="email.encoders: Encoders for email message payloads."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.encoders</span></code></a> module for a list of the built-in encoders.</p>
  311. <p>Optional <em>policy</em> argument defaults to <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>.</p>
  312. <p><em>_params</em> are passed straight through to the <a class="reference internal" href="#email.mime.base.MIMEBase" title="email.mime.base.MIMEBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMEBase</span></code></a>
  313. constructor.</p>
  314. <div class="versionchanged">
  315. <p><span class="versionmodified changed">Changed in version 3.6: </span>Added <em>policy</em> keyword-only parameter.</p>
  316. </div>
  317. </dd></dl>
  318. <dl class="py class" id="module-email.mime.message">
  319. <dt class="sig sig-object py" id="email.mime.message.MIMEMessage">
  320. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.mime.message.</span></span><span class="sig-name descname"><span class="pre">MIMEMessage</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">_subtype</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'rfc822'</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</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">compat32</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.mime.message.MIMEMessage" title="Link to this definition">¶</a></dt>
  321. <dd><p>Module: <a class="reference internal" href="#module-email.mime.message" title="email.mime.message"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.mime.message</span></code></a></p>
  322. <p>A subclass of <a class="reference internal" href="#email.mime.nonmultipart.MIMENonMultipart" title="email.mime.nonmultipart.MIMENonMultipart"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMENonMultipart</span></code></a>, the
  323. <a class="reference internal" href="#email.mime.message.MIMEMessage" title="email.mime.message.MIMEMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMEMessage</span></code></a> class is used to create MIME objects of main type
  324. <em class="mimetype">message</em>. <em>_msg</em> is used as the payload, and must be an instance
  325. of class <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> (or a subclass thereof), otherwise
  326. 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>
  327. <p>Optional <em>_subtype</em> sets the subtype of the message; it defaults to
  328. <em class="mimetype">rfc822</em>.</p>
  329. <p>Optional <em>policy</em> argument defaults to <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>.</p>
  330. <div class="versionchanged">
  331. <p><span class="versionmodified changed">Changed in version 3.6: </span>Added <em>policy</em> keyword-only parameter.</p>
  332. </div>
  333. </dd></dl>
  334. <dl class="py class" id="module-email.mime.text">
  335. <dt class="sig sig-object py" id="email.mime.text.MIMEText">
  336. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.mime.text.</span></span><span class="sig-name descname"><span class="pre">MIMEText</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">_text</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">_subtype</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'plain'</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="o"><span class="pre">*</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">compat32</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.mime.text.MIMEText" title="Link to this definition">¶</a></dt>
  337. <dd><p>Module: <a class="reference internal" href="#module-email.mime.text" title="email.mime.text"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.mime.text</span></code></a></p>
  338. <p>A subclass of <a class="reference internal" href="#email.mime.nonmultipart.MIMENonMultipart" title="email.mime.nonmultipart.MIMENonMultipart"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMENonMultipart</span></code></a>, the
  339. <a class="reference internal" href="#email.mime.text.MIMEText" title="email.mime.text.MIMEText"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMEText</span></code></a> class is used to create MIME objects of major type
  340. <em class="mimetype">text</em>. <em>_text</em> is the string for the payload. <em>_subtype</em> is the
  341. minor type and defaults to <em class="mimetype">plain</em>. <em>_charset</em> is the character
  342. set of the text and is passed as an argument to the
  343. <a class="reference internal" href="#email.mime.nonmultipart.MIMENonMultipart" title="email.mime.nonmultipart.MIMENonMultipart"><code class="xref py py-class docutils literal notranslate"><span class="pre">MIMENonMultipart</span></code></a> constructor; it defaults
  344. to <code class="docutils literal notranslate"><span class="pre">us-ascii</span></code> if the string contains only <code class="docutils literal notranslate"><span class="pre">ascii</span></code> code points, and
  345. <code class="docutils literal notranslate"><span class="pre">utf-8</span></code> otherwise. The <em>_charset</em> parameter accepts either a string or a
  346. <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.</p>
  347. <p>Unless the <em>_charset</em> argument is explicitly set to <code class="docutils literal notranslate"><span class="pre">None</span></code>, the
  348. MIMEText object created will have both a <em class="mailheader">Content-Type</em> header
  349. with a <code class="docutils literal notranslate"><span class="pre">charset</span></code> parameter, and a <em class="mailheader">Content-Transfer-Encoding</em>
  350. header. This means that a subsequent <code class="docutils literal notranslate"><span class="pre">set_payload</span></code> call will not result
  351. in an encoded payload, even if a charset is passed in the <code class="docutils literal notranslate"><span class="pre">set_payload</span></code>
  352. command. You can “reset” this behavior by deleting the
  353. <code class="docutils literal notranslate"><span class="pre">Content-Transfer-Encoding</span></code> header, after which a <code class="docutils literal notranslate"><span class="pre">set_payload</span></code> call
  354. will automatically encode the new payload (and add a new
  355. <em class="mailheader">Content-Transfer-Encoding</em> header).</p>
  356. <p>Optional <em>policy</em> argument defaults to <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>.</p>
  357. <div class="versionchanged">
  358. <p><span class="versionmodified changed">Changed in version 3.5: </span><em>_charset</em> also accepts <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> instances.</p>
  359. </div>
  360. <div class="versionchanged">
  361. <p><span class="versionmodified changed">Changed in version 3.6: </span>Added <em>policy</em> keyword-only parameter.</p>
  362. </div>
  363. </dd></dl>
  364. </section>
  365. <div class="clearer"></div>
  366. </div>
  367. </div>
  368. </div>
  369. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  370. <div class="sphinxsidebarwrapper">
  371. <div>
  372. <h4>Previous topic</h4>
  373. <p class="topless"><a href="email.compat32-message.html"
  374. title="previous chapter"><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></p>
  375. </div>
  376. <div>
  377. <h4>Next topic</h4>
  378. <p class="topless"><a href="email.header.html"
  379. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.header</span></code>: Internationalized headers</a></p>
  380. </div>
  381. <div role="note" aria-label="source link">
  382. <h3>This Page</h3>
  383. <ul class="this-page-menu">
  384. <li><a href="../bugs.html">Report a Bug</a></li>
  385. <li>
  386. <a href="https://github.com/python/cpython/blob/main/Doc/library/email.mime.rst"
  387. rel="nofollow">Show Source
  388. </a>
  389. </li>
  390. </ul>
  391. </div>
  392. </div>
  393. <div id="sidebarbutton" title="Collapse sidebar">
  394. <span>«</span>
  395. </div>
  396. </div>
  397. <div class="clearer"></div>
  398. </div>
  399. <div class="related" role="navigation" aria-label="related navigation">
  400. <h3>Navigation</h3>
  401. <ul>
  402. <li class="right" style="margin-right: 10px">
  403. <a href="../genindex.html" title="General Index"
  404. >index</a></li>
  405. <li class="right" >
  406. <a href="../py-modindex.html" title="Python Module Index"
  407. >modules</a> |</li>
  408. <li class="right" >
  409. <a href="email.header.html" title="email.header: Internationalized headers"
  410. >next</a> |</li>
  411. <li class="right" >
  412. <a href="email.compat32-message.html" title="email.message.Message: Representing an email message using the compat32 API"
  413. >previous</a> |</li>
  414. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  415. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  416. <li class="switchers">
  417. <div class="language_switcher_placeholder"></div>
  418. <div class="version_switcher_placeholder"></div>
  419. </li>
  420. <li>
  421. </li>
  422. <li id="cpython-language-and-version">
  423. <a href="../index.html">3.12.3 Documentation</a> &#187;
  424. </li>
  425. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  426. <li class="nav-item nav-item-2"><a href="netdata.html" >Internet Data Handling</a> &#187;</li>
  427. <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>
  428. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.mime</span></code>: Creating email and MIME objects from scratch</a></li>
  429. <li class="right">
  430. <div class="inline-search" role="search">
  431. <form class="inline-search" action="../search.html" method="get">
  432. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  433. <input type="submit" value="Go" />
  434. </form>
  435. </div>
  436. |
  437. </li>
  438. <li class="right">
  439. <label class="theme-selector-label">
  440. Theme
  441. <select class="theme-selector" oninput="activateTheme(this.value)">
  442. <option value="auto" selected>Auto</option>
  443. <option value="light">Light</option>
  444. <option value="dark">Dark</option>
  445. </select>
  446. </label> |</li>
  447. </ul>
  448. </div>
  449. <div class="footer">
  450. &copy;
  451. <a href="../copyright.html">
  452. Copyright
  453. </a>
  454. 2001-2024, Python Software Foundation.
  455. <br />
  456. This page is licensed under the Python Software Foundation License Version 2.
  457. <br />
  458. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  459. <br />
  460. See <a href="/license.html">History and License</a> for more information.<br />
  461. <br />
  462. The Python Software Foundation is a non-profit corporation.
  463. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  464. <br />
  465. <br />
  466. Last updated on Apr 09, 2024 (13:47 UTC).
  467. <a href="/bugs.html">Found a bug</a>?
  468. <br />
  469. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  470. </div>
  471. </body>
  472. </html>
上海开阖软件有限公司 沪ICP备12045867号-1