gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

845 line
69KB

  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.headerregistry: Custom Header Objects" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/email.headerregistry.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/email/headerregistry.py Headers are represented by customized subclasses of str. The particular class used to represent a given header is determined by the header_factory of the po..." />
  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/headerregistry.py Headers are represented by customized subclasses of str. The particular class used to represent a given header is determined by the header_factory of the po..." />
  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.headerregistry: Custom Header Objects &#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.contentmanager: Managing MIME Content" href="email.contentmanager.html" />
  33. <link rel="prev" title="email.errors: Exception and Defect classes" href="email.errors.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/email.headerregistry.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.errors.html"
  85. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.errors</span></code>: Exception and Defect classes</a></p>
  86. </div>
  87. <div>
  88. <h4>Next topic</h4>
  89. <p class="topless"><a href="email.contentmanager.html"
  90. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.contentmanager</span></code>: Managing MIME Content</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.headerregistry.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.contentmanager.html" title="email.contentmanager: Managing MIME Content"
  117. accesskey="N">next</a> |</li>
  118. <li class="right" >
  119. <a href="email.errors.html" title="email.errors: Exception and Defect classes"
  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.headerregistry</span></code>: Custom Header Objects</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.headerregistry">
  161. <span id="email-headerregistry-custom-header-objects"></span><h1><a class="reference internal" href="#module-email.headerregistry" title="email.headerregistry: Automatic Parsing of headers based on the field name"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.headerregistry</span></code></a>: Custom Header Objects<a class="headerlink" href="#module-email.headerregistry" 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/headerregistry.py">Lib/email/headerregistry.py</a></p>
  163. <hr class="docutils" />
  164. <div class="versionadded">
  165. <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>
  166. </div>
  167. <p>Headers are represented by customized subclasses of <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>. The
  168. particular class used to represent a given header is determined by the
  169. <a class="reference internal" href="email.policy.html#email.policy.EmailPolicy.header_factory" title="email.policy.EmailPolicy.header_factory"><code class="xref py py-attr docutils literal notranslate"><span class="pre">header_factory</span></code></a> of the <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> in
  170. effect when the headers are created. This section documents the particular
  171. <code class="docutils literal notranslate"><span class="pre">header_factory</span></code> implemented by the email package for handling <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>
  172. compliant email messages, which not only provides customized header objects for
  173. various header types, but also provides an extension mechanism for applications
  174. to add their own custom header types.</p>
  175. <p>When using any of the policy objects derived from
  176. <a class="reference internal" href="email.policy.html#email.policy.EmailPolicy" title="email.policy.EmailPolicy"><code class="xref py py-data docutils literal notranslate"><span class="pre">EmailPolicy</span></code></a>, all headers are produced by
  177. <a class="reference internal" href="#email.headerregistry.HeaderRegistry" title="email.headerregistry.HeaderRegistry"><code class="xref py py-class docutils literal notranslate"><span class="pre">HeaderRegistry</span></code></a> and have <a class="reference internal" href="#email.headerregistry.BaseHeader" title="email.headerregistry.BaseHeader"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHeader</span></code></a> as their last base
  178. class. Each header class has an additional base class that is determined by
  179. the type of the header. For example, many headers have the class
  180. <a class="reference internal" href="#email.headerregistry.UnstructuredHeader" title="email.headerregistry.UnstructuredHeader"><code class="xref py py-class docutils literal notranslate"><span class="pre">UnstructuredHeader</span></code></a> as their other base class. The specialized second
  181. class for a header is determined by the name of the header, using a lookup
  182. table stored in the <a class="reference internal" href="#email.headerregistry.HeaderRegistry" title="email.headerregistry.HeaderRegistry"><code class="xref py py-class docutils literal notranslate"><span class="pre">HeaderRegistry</span></code></a>. All of this is managed
  183. transparently for the typical application program, but interfaces are provided
  184. for modifying the default behavior for use by more complex applications.</p>
  185. <p>The sections below first document the header base classes and their attributes,
  186. followed by the API for modifying the behavior of <a class="reference internal" href="#email.headerregistry.HeaderRegistry" title="email.headerregistry.HeaderRegistry"><code class="xref py py-class docutils literal notranslate"><span class="pre">HeaderRegistry</span></code></a>, and
  187. finally the support classes used to represent the data parsed from structured
  188. headers.</p>
  189. <dl class="py class">
  190. <dt class="sig sig-object py" id="email.headerregistry.BaseHeader">
  191. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.headerregistry.</span></span><span class="sig-name descname"><span class="pre">BaseHeader</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.headerregistry.BaseHeader" title="Link to this definition">¶</a></dt>
  192. <dd><p><em>name</em> and <em>value</em> are passed to <code class="docutils literal notranslate"><span class="pre">BaseHeader</span></code> from the
  193. <a class="reference internal" href="email.policy.html#email.policy.EmailPolicy.header_factory" title="email.policy.EmailPolicy.header_factory"><code class="xref py py-attr docutils literal notranslate"><span class="pre">header_factory</span></code></a> call. The string value of
  194. any header object is the <em>value</em> fully decoded to unicode.</p>
  195. <p>This base class defines the following read-only properties:</p>
  196. <dl class="py attribute">
  197. <dt class="sig sig-object py" id="email.headerregistry.BaseHeader.name">
  198. <span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#email.headerregistry.BaseHeader.name" title="Link to this definition">¶</a></dt>
  199. <dd><p>The name of the header (the portion of the field before the ‘:’). This
  200. is exactly the value passed in the
  201. <a class="reference internal" href="email.policy.html#email.policy.EmailPolicy.header_factory" title="email.policy.EmailPolicy.header_factory"><code class="xref py py-attr docutils literal notranslate"><span class="pre">header_factory</span></code></a> call for <em>name</em>; that
  202. is, case is preserved.</p>
  203. </dd></dl>
  204. <dl class="py attribute">
  205. <dt class="sig sig-object py" id="email.headerregistry.BaseHeader.defects">
  206. <span class="sig-name descname"><span class="pre">defects</span></span><a class="headerlink" href="#email.headerregistry.BaseHeader.defects" title="Link to this definition">¶</a></dt>
  207. <dd><p>A tuple of <a class="reference internal" href="email.errors.html#email.errors.HeaderDefect" title="email.errors.HeaderDefect"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HeaderDefect</span></code></a> instances reporting any
  208. RFC compliance problems found during parsing. The email package tries to
  209. be complete about detecting compliance issues. See the <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">errors</span></code></a>
  210. module for a discussion of the types of defects that may be reported.</p>
  211. </dd></dl>
  212. <dl class="py attribute">
  213. <dt class="sig sig-object py" id="email.headerregistry.BaseHeader.max_count">
  214. <span class="sig-name descname"><span class="pre">max_count</span></span><a class="headerlink" href="#email.headerregistry.BaseHeader.max_count" title="Link to this definition">¶</a></dt>
  215. <dd><p>The maximum number of headers of this type that can have the same
  216. <code class="docutils literal notranslate"><span class="pre">name</span></code>. A value of <code class="docutils literal notranslate"><span class="pre">None</span></code> means unlimited. The <code class="docutils literal notranslate"><span class="pre">BaseHeader</span></code> value
  217. for this attribute is <code class="docutils literal notranslate"><span class="pre">None</span></code>; it is expected that specialized header
  218. classes will override this value as needed.</p>
  219. </dd></dl>
  220. <p><code class="docutils literal notranslate"><span class="pre">BaseHeader</span></code> also provides the following method, which is called by the
  221. email library code and should not in general be called by application
  222. programs:</p>
  223. <dl class="py method">
  224. <dt class="sig sig-object py" id="email.headerregistry.BaseHeader.fold">
  225. <span class="sig-name descname"><span class="pre">fold</span></span><span class="sig-paren">(</span><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></em><span class="sig-paren">)</span><a class="headerlink" href="#email.headerregistry.BaseHeader.fold" title="Link to this definition">¶</a></dt>
  226. <dd><p>Return a string containing <a class="reference internal" href="email.policy.html#email.policy.Policy.linesep" title="email.policy.Policy.linesep"><code class="xref py py-attr docutils literal notranslate"><span class="pre">linesep</span></code></a>
  227. characters as required to correctly fold the header according to
  228. <em>policy</em>. A <a class="reference internal" href="email.policy.html#email.policy.Policy.cte_type" title="email.policy.Policy.cte_type"><code class="xref py py-attr docutils literal notranslate"><span class="pre">cte_type</span></code></a> of <code class="docutils literal notranslate"><span class="pre">8bit</span></code> will be
  229. treated as if it were <code class="docutils literal notranslate"><span class="pre">7bit</span></code>, since headers may not contain arbitrary
  230. binary data. If <a class="reference internal" href="email.policy.html#email.policy.EmailPolicy.utf8" title="email.policy.EmailPolicy.utf8"><code class="xref py py-attr docutils literal notranslate"><span class="pre">utf8</span></code></a> is <code class="docutils literal notranslate"><span class="pre">False</span></code>,
  231. non-ASCII data will be <span class="target" id="index-1"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2047.html"><strong>RFC 2047</strong></a> encoded.</p>
  232. </dd></dl>
  233. <p><code class="docutils literal notranslate"><span class="pre">BaseHeader</span></code> by itself cannot be used to create a header object. It
  234. defines a protocol that each specialized header cooperates with in order to
  235. produce the header object. Specifically, <code class="docutils literal notranslate"><span class="pre">BaseHeader</span></code> requires that
  236. the specialized class provide a <a class="reference internal" href="functions.html#classmethod" title="classmethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">classmethod()</span></code></a> named <code class="docutils literal notranslate"><span class="pre">parse</span></code>. This
  237. method is called as follows:</p>
  238. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">parse</span><span class="p">(</span><span class="n">string</span><span class="p">,</span> <span class="n">kwds</span><span class="p">)</span>
  239. </pre></div>
  240. </div>
  241. <p><code class="docutils literal notranslate"><span class="pre">kwds</span></code> is a dictionary containing one pre-initialized key, <code class="docutils literal notranslate"><span class="pre">defects</span></code>.
  242. <code class="docutils literal notranslate"><span class="pre">defects</span></code> is an empty list. The parse method should append any detected
  243. defects to this list. On return, the <code class="docutils literal notranslate"><span class="pre">kwds</span></code> dictionary <em>must</em> contain
  244. values for at least the keys <code class="docutils literal notranslate"><span class="pre">decoded</span></code> and <code class="docutils literal notranslate"><span class="pre">defects</span></code>. <code class="docutils literal notranslate"><span class="pre">decoded</span></code>
  245. should be the string value for the header (that is, the header value fully
  246. decoded to unicode). The parse method should assume that <em>string</em> may
  247. contain content-transfer-encoded parts, but should correctly handle all valid
  248. unicode characters as well so that it can parse un-encoded header values.</p>
  249. <p><code class="docutils literal notranslate"><span class="pre">BaseHeader</span></code>’s <code class="docutils literal notranslate"><span class="pre">__new__</span></code> then creates the header instance, and calls its
  250. <code class="docutils literal notranslate"><span class="pre">init</span></code> method. The specialized class only needs to provide an <code class="docutils literal notranslate"><span class="pre">init</span></code>
  251. method if it wishes to set additional attributes beyond those provided by
  252. <code class="docutils literal notranslate"><span class="pre">BaseHeader</span></code> itself. Such an <code class="docutils literal notranslate"><span class="pre">init</span></code> method should look like this:</p>
  253. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">init</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">/</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">):</span>
  254. <span class="bp">self</span><span class="o">.</span><span class="n">_myattr</span> <span class="o">=</span> <span class="n">kw</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;myattr&#39;</span><span class="p">)</span>
  255. <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">init</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">)</span>
  256. </pre></div>
  257. </div>
  258. <p>That is, anything extra that the specialized class puts in to the <code class="docutils literal notranslate"><span class="pre">kwds</span></code>
  259. dictionary should be removed and handled, and the remaining contents of
  260. <code class="docutils literal notranslate"><span class="pre">kw</span></code> (and <code class="docutils literal notranslate"><span class="pre">args</span></code>) passed to the <code class="docutils literal notranslate"><span class="pre">BaseHeader</span></code> <code class="docutils literal notranslate"><span class="pre">init</span></code> method.</p>
  261. </dd></dl>
  262. <dl class="py class">
  263. <dt class="sig sig-object py" id="email.headerregistry.UnstructuredHeader">
  264. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.headerregistry.</span></span><span class="sig-name descname"><span class="pre">UnstructuredHeader</span></span><a class="headerlink" href="#email.headerregistry.UnstructuredHeader" title="Link to this definition">¶</a></dt>
  265. <dd><p>An “unstructured” header is the default type of header in <span class="target" id="index-2"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5322.html"><strong>RFC 5322</strong></a>.
  266. Any header that does not have a specified syntax is treated as
  267. unstructured. The classic example of an unstructured header is the
  268. <em class="mailheader">Subject</em> header.</p>
  269. <p>In <span class="target" id="index-3"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5322.html"><strong>RFC 5322</strong></a>, an unstructured header is a run of arbitrary text in the
  270. ASCII character set. <span class="target" id="index-4"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2047.html"><strong>RFC 2047</strong></a>, however, has an <span class="target" id="index-5"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5322.html"><strong>RFC 5322</strong></a> compatible
  271. mechanism for encoding non-ASCII text as ASCII characters within a header
  272. value. When a <em>value</em> containing encoded words is passed to the
  273. constructor, the <code class="docutils literal notranslate"><span class="pre">UnstructuredHeader</span></code> parser converts such encoded words
  274. into unicode, following the <span class="target" id="index-6"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2047.html"><strong>RFC 2047</strong></a> rules for unstructured text. The
  275. parser uses heuristics to attempt to decode certain non-compliant encoded
  276. words. Defects are registered in such cases, as well as defects for issues
  277. such as invalid characters within the encoded words or the non-encoded text.</p>
  278. <p>This header type provides no additional attributes.</p>
  279. </dd></dl>
  280. <dl class="py class">
  281. <dt class="sig sig-object py" id="email.headerregistry.DateHeader">
  282. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.headerregistry.</span></span><span class="sig-name descname"><span class="pre">DateHeader</span></span><a class="headerlink" href="#email.headerregistry.DateHeader" title="Link to this definition">¶</a></dt>
  283. <dd><p><span class="target" id="index-7"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5322.html"><strong>RFC 5322</strong></a> specifies a very specific format for dates within email headers.
  284. The <code class="docutils literal notranslate"><span class="pre">DateHeader</span></code> parser recognizes that date format, as well as
  285. recognizing a number of variant forms that are sometimes found “in the
  286. wild”.</p>
  287. <p>This header type provides the following additional attributes:</p>
  288. <dl class="py attribute">
  289. <dt class="sig sig-object py" id="email.headerregistry.DateHeader.datetime">
  290. <span class="sig-name descname"><span class="pre">datetime</span></span><a class="headerlink" href="#email.headerregistry.DateHeader.datetime" title="Link to this definition">¶</a></dt>
  291. <dd><p>If the header value can be recognized as a valid date of one form or
  292. another, this attribute will contain a <a class="reference internal" href="datetime.html#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>
  293. instance representing that date. If the timezone of the input date is
  294. specified as <code class="docutils literal notranslate"><span class="pre">-0000</span></code> (indicating it is in UTC but contains no
  295. information about the source timezone), then <a class="reference internal" href="#email.headerregistry.DateHeader.datetime" title="email.headerregistry.DateHeader.datetime"><code class="xref py py-attr docutils literal notranslate"><span class="pre">datetime</span></code></a> will be a
  296. naive <a class="reference internal" href="datetime.html#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a>. If a specific timezone offset is
  297. found (including <code class="docutils literal notranslate"><span class="pre">+0000</span></code>), then <a class="reference internal" href="#email.headerregistry.DateHeader.datetime" title="email.headerregistry.DateHeader.datetime"><code class="xref py py-attr docutils literal notranslate"><span class="pre">datetime</span></code></a> will contain an aware
  298. <code class="docutils literal notranslate"><span class="pre">datetime</span></code> that uses <a class="reference internal" href="datetime.html#datetime.timezone" title="datetime.timezone"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime.timezone</span></code></a> to record the timezone
  299. offset.</p>
  300. </dd></dl>
  301. <p>The <code class="docutils literal notranslate"><span class="pre">decoded</span></code> value of the header is determined by formatting the
  302. <code class="docutils literal notranslate"><span class="pre">datetime</span></code> according to the <span class="target" id="index-8"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5322.html"><strong>RFC 5322</strong></a> rules; that is, it is set to:</p>
  303. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">email</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">format_datetime</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">datetime</span><span class="p">)</span>
  304. </pre></div>
  305. </div>
  306. <p>When creating a <code class="docutils literal notranslate"><span class="pre">DateHeader</span></code>, <em>value</em> may be
  307. <a class="reference internal" href="datetime.html#datetime.datetime" title="datetime.datetime"><code class="xref py py-class docutils literal notranslate"><span class="pre">datetime</span></code></a> instance. This means, for example, that
  308. the following code is valid and does what one would expect:</p>
  309. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">msg</span><span class="p">[</span><span class="s1">&#39;Date&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">datetime</span><span class="p">(</span><span class="mi">2011</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">21</span><span class="p">)</span>
  310. </pre></div>
  311. </div>
  312. <p>Because this is a naive <code class="docutils literal notranslate"><span class="pre">datetime</span></code> it will be interpreted as a UTC
  313. timestamp, and the resulting value will have a timezone of <code class="docutils literal notranslate"><span class="pre">-0000</span></code>. Much
  314. more useful is to use the <a class="reference internal" href="email.utils.html#email.utils.localtime" title="email.utils.localtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">localtime()</span></code></a> function from the
  315. <a class="reference internal" href="email.utils.html#module-email.utils" title="email.utils: Miscellaneous email package utilities."><code class="xref py py-mod docutils literal notranslate"><span class="pre">utils</span></code></a> module:</p>
  316. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">msg</span><span class="p">[</span><span class="s1">&#39;Date&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">utils</span><span class="o">.</span><span class="n">localtime</span><span class="p">()</span>
  317. </pre></div>
  318. </div>
  319. <p>This example sets the date header to the current time and date using
  320. the current timezone offset.</p>
  321. </dd></dl>
  322. <dl class="py class">
  323. <dt class="sig sig-object py" id="email.headerregistry.AddressHeader">
  324. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.headerregistry.</span></span><span class="sig-name descname"><span class="pre">AddressHeader</span></span><a class="headerlink" href="#email.headerregistry.AddressHeader" title="Link to this definition">¶</a></dt>
  325. <dd><p>Address headers are one of the most complex structured header types.
  326. The <code class="docutils literal notranslate"><span class="pre">AddressHeader</span></code> class provides a generic interface to any address
  327. header.</p>
  328. <p>This header type provides the following additional attributes:</p>
  329. <dl class="py attribute">
  330. <dt class="sig sig-object py" id="email.headerregistry.AddressHeader.groups">
  331. <span class="sig-name descname"><span class="pre">groups</span></span><a class="headerlink" href="#email.headerregistry.AddressHeader.groups" title="Link to this definition">¶</a></dt>
  332. <dd><p>A tuple of <a class="reference internal" href="#email.headerregistry.Group" title="email.headerregistry.Group"><code class="xref py py-class docutils literal notranslate"><span class="pre">Group</span></code></a> objects encoding the
  333. addresses and groups found in the header value. Addresses that are
  334. not part of a group are represented in this list as single-address
  335. <code class="docutils literal notranslate"><span class="pre">Groups</span></code> whose <a class="reference internal" href="#email.headerregistry.Group.display_name" title="email.headerregistry.Group.display_name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">display_name</span></code></a> is <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  336. </dd></dl>
  337. <dl class="py attribute">
  338. <dt class="sig sig-object py" id="email.headerregistry.AddressHeader.addresses">
  339. <span class="sig-name descname"><span class="pre">addresses</span></span><a class="headerlink" href="#email.headerregistry.AddressHeader.addresses" title="Link to this definition">¶</a></dt>
  340. <dd><p>A tuple of <a class="reference internal" href="#email.headerregistry.Address" title="email.headerregistry.Address"><code class="xref py py-class docutils literal notranslate"><span class="pre">Address</span></code></a> objects encoding all
  341. of the individual addresses from the header value. If the header value
  342. contains any groups, the individual addresses from the group are included
  343. in the list at the point where the group occurs in the value (that is,
  344. the list of addresses is “flattened” into a one dimensional list).</p>
  345. </dd></dl>
  346. <p>The <code class="docutils literal notranslate"><span class="pre">decoded</span></code> value of the header will have all encoded words decoded to
  347. unicode. <a class="reference internal" href="codecs.html#module-encodings.idna" title="encodings.idna: Internationalized Domain Names implementation"><code class="xref py py-class docutils literal notranslate"><span class="pre">idna</span></code></a> encoded domain names are also decoded to
  348. unicode. The <code class="docutils literal notranslate"><span class="pre">decoded</span></code> value is set by <a class="reference internal" href="stdtypes.html#meth-str-join"><span class="std std-ref">joining</span></a> the
  349. <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> value of the elements of the <code class="docutils literal notranslate"><span class="pre">groups</span></code> attribute with <code class="docutils literal notranslate"><span class="pre">',</span>
  350. <span class="pre">'</span></code>.</p>
  351. <p>A list of <a class="reference internal" href="#email.headerregistry.Address" title="email.headerregistry.Address"><code class="xref py py-class docutils literal notranslate"><span class="pre">Address</span></code></a> and <a class="reference internal" href="#email.headerregistry.Group" title="email.headerregistry.Group"><code class="xref py py-class docutils literal notranslate"><span class="pre">Group</span></code></a> objects in any combination
  352. may be used to set the value of an address header. <code class="docutils literal notranslate"><span class="pre">Group</span></code> objects whose
  353. <code class="docutils literal notranslate"><span class="pre">display_name</span></code> is <code class="docutils literal notranslate"><span class="pre">None</span></code> will be interpreted as single addresses, which
  354. allows an address list to be copied with groups intact by using the list
  355. obtained from the <code class="docutils literal notranslate"><span class="pre">groups</span></code> attribute of the source header.</p>
  356. </dd></dl>
  357. <dl class="py class">
  358. <dt class="sig sig-object py" id="email.headerregistry.SingleAddressHeader">
  359. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.headerregistry.</span></span><span class="sig-name descname"><span class="pre">SingleAddressHeader</span></span><a class="headerlink" href="#email.headerregistry.SingleAddressHeader" title="Link to this definition">¶</a></dt>
  360. <dd><p>A subclass of <a class="reference internal" href="#email.headerregistry.AddressHeader" title="email.headerregistry.AddressHeader"><code class="xref py py-class docutils literal notranslate"><span class="pre">AddressHeader</span></code></a> that adds one
  361. additional attribute:</p>
  362. <dl class="py attribute">
  363. <dt class="sig sig-object py" id="email.headerregistry.SingleAddressHeader.address">
  364. <span class="sig-name descname"><span class="pre">address</span></span><a class="headerlink" href="#email.headerregistry.SingleAddressHeader.address" title="Link to this definition">¶</a></dt>
  365. <dd><p>The single address encoded by the header value. If the header value
  366. actually contains more than one address (which would be a violation of
  367. the RFC under the default <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>), accessing this attribute
  368. will result in 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>.</p>
  369. </dd></dl>
  370. </dd></dl>
  371. <p>Many of the above classes also have a <code class="docutils literal notranslate"><span class="pre">Unique</span></code> variant (for example,
  372. <code class="docutils literal notranslate"><span class="pre">UniqueUnstructuredHeader</span></code>). The only difference is that in the <code class="docutils literal notranslate"><span class="pre">Unique</span></code>
  373. variant, <a class="reference internal" href="#email.headerregistry.BaseHeader.max_count" title="email.headerregistry.BaseHeader.max_count"><code class="xref py py-attr docutils literal notranslate"><span class="pre">max_count</span></code></a> is set to 1.</p>
  374. <dl class="py class">
  375. <dt class="sig sig-object py" id="email.headerregistry.MIMEVersionHeader">
  376. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.headerregistry.</span></span><span class="sig-name descname"><span class="pre">MIMEVersionHeader</span></span><a class="headerlink" href="#email.headerregistry.MIMEVersionHeader" title="Link to this definition">¶</a></dt>
  377. <dd><p>There is really only one valid value for the <em class="mailheader">MIME-Version</em>
  378. header, and that is <code class="docutils literal notranslate"><span class="pre">1.0</span></code>. For future proofing, this header class
  379. supports other valid version numbers. If a version number has a valid value
  380. 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>, then the header object will have non-<code class="docutils literal notranslate"><span class="pre">None</span></code> values for
  381. the following attributes:</p>
  382. <dl class="py attribute">
  383. <dt class="sig sig-object py" id="email.headerregistry.MIMEVersionHeader.version">
  384. <span class="sig-name descname"><span class="pre">version</span></span><a class="headerlink" href="#email.headerregistry.MIMEVersionHeader.version" title="Link to this definition">¶</a></dt>
  385. <dd><p>The version number as a string, with any whitespace and/or comments
  386. removed.</p>
  387. </dd></dl>
  388. <dl class="py attribute">
  389. <dt class="sig sig-object py" id="email.headerregistry.MIMEVersionHeader.major">
  390. <span class="sig-name descname"><span class="pre">major</span></span><a class="headerlink" href="#email.headerregistry.MIMEVersionHeader.major" title="Link to this definition">¶</a></dt>
  391. <dd><p>The major version number as an integer</p>
  392. </dd></dl>
  393. <dl class="py attribute">
  394. <dt class="sig sig-object py" id="email.headerregistry.MIMEVersionHeader.minor">
  395. <span class="sig-name descname"><span class="pre">minor</span></span><a class="headerlink" href="#email.headerregistry.MIMEVersionHeader.minor" title="Link to this definition">¶</a></dt>
  396. <dd><p>The minor version number as an integer</p>
  397. </dd></dl>
  398. </dd></dl>
  399. <dl class="py class">
  400. <dt class="sig sig-object py" id="email.headerregistry.ParameterizedMIMEHeader">
  401. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.headerregistry.</span></span><span class="sig-name descname"><span class="pre">ParameterizedMIMEHeader</span></span><a class="headerlink" href="#email.headerregistry.ParameterizedMIMEHeader" title="Link to this definition">¶</a></dt>
  402. <dd><p>MIME headers all start with the prefix ‘Content-’. Each specific header has
  403. a certain value, described under the class for that header. Some can
  404. also take a list of supplemental parameters, which have a common format.
  405. This class serves as a base for all the MIME headers that take parameters.</p>
  406. <dl class="py attribute">
  407. <dt class="sig sig-object py" id="email.headerregistry.ParameterizedMIMEHeader.params">
  408. <span class="sig-name descname"><span class="pre">params</span></span><a class="headerlink" href="#email.headerregistry.ParameterizedMIMEHeader.params" title="Link to this definition">¶</a></dt>
  409. <dd><p>A dictionary mapping parameter names to parameter values.</p>
  410. </dd></dl>
  411. </dd></dl>
  412. <dl class="py class">
  413. <dt class="sig sig-object py" id="email.headerregistry.ContentTypeHeader">
  414. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.headerregistry.</span></span><span class="sig-name descname"><span class="pre">ContentTypeHeader</span></span><a class="headerlink" href="#email.headerregistry.ContentTypeHeader" title="Link to this definition">¶</a></dt>
  415. <dd><p>A <a class="reference internal" href="#email.headerregistry.ParameterizedMIMEHeader" title="email.headerregistry.ParameterizedMIMEHeader"><code class="xref py py-class docutils literal notranslate"><span class="pre">ParameterizedMIMEHeader</span></code></a> class that handles the
  416. <em class="mailheader">Content-Type</em> header.</p>
  417. <dl class="py attribute">
  418. <dt class="sig sig-object py" id="email.headerregistry.ContentTypeHeader.content_type">
  419. <span class="sig-name descname"><span class="pre">content_type</span></span><a class="headerlink" href="#email.headerregistry.ContentTypeHeader.content_type" title="Link to this definition">¶</a></dt>
  420. <dd><p>The content type string, in the form <code class="docutils literal notranslate"><span class="pre">maintype/subtype</span></code>.</p>
  421. </dd></dl>
  422. <dl class="py attribute">
  423. <dt class="sig sig-object py" id="email.headerregistry.ContentTypeHeader.maintype">
  424. <span class="sig-name descname"><span class="pre">maintype</span></span><a class="headerlink" href="#email.headerregistry.ContentTypeHeader.maintype" title="Link to this definition">¶</a></dt>
  425. <dd></dd></dl>
  426. <dl class="py attribute">
  427. <dt class="sig sig-object py" id="email.headerregistry.ContentTypeHeader.subtype">
  428. <span class="sig-name descname"><span class="pre">subtype</span></span><a class="headerlink" href="#email.headerregistry.ContentTypeHeader.subtype" title="Link to this definition">¶</a></dt>
  429. <dd></dd></dl>
  430. </dd></dl>
  431. <dl class="py class">
  432. <dt class="sig sig-object py" id="email.headerregistry.ContentDispositionHeader">
  433. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.headerregistry.</span></span><span class="sig-name descname"><span class="pre">ContentDispositionHeader</span></span><a class="headerlink" href="#email.headerregistry.ContentDispositionHeader" title="Link to this definition">¶</a></dt>
  434. <dd><p>A <a class="reference internal" href="#email.headerregistry.ParameterizedMIMEHeader" title="email.headerregistry.ParameterizedMIMEHeader"><code class="xref py py-class docutils literal notranslate"><span class="pre">ParameterizedMIMEHeader</span></code></a> class that handles the
  435. <em class="mailheader">Content-Disposition</em> header.</p>
  436. <dl class="py attribute">
  437. <dt class="sig sig-object py" id="email.headerregistry.ContentDispositionHeader.content_disposition">
  438. <span class="sig-name descname"><span class="pre">content_disposition</span></span><a class="headerlink" href="#email.headerregistry.ContentDispositionHeader.content_disposition" title="Link to this definition">¶</a></dt>
  439. <dd><p><code class="docutils literal notranslate"><span class="pre">inline</span></code> and <code class="docutils literal notranslate"><span class="pre">attachment</span></code> are the only valid values in common use.</p>
  440. </dd></dl>
  441. </dd></dl>
  442. <dl class="py class">
  443. <dt class="sig sig-object py" id="email.headerregistry.ContentTransferEncoding">
  444. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.headerregistry.</span></span><span class="sig-name descname"><span class="pre">ContentTransferEncoding</span></span><a class="headerlink" href="#email.headerregistry.ContentTransferEncoding" title="Link to this definition">¶</a></dt>
  445. <dd><p>Handles the <em class="mailheader">Content-Transfer-Encoding</em> header.</p>
  446. <dl class="py attribute">
  447. <dt class="sig sig-object py" id="email.headerregistry.ContentTransferEncoding.cte">
  448. <span class="sig-name descname"><span class="pre">cte</span></span><a class="headerlink" href="#email.headerregistry.ContentTransferEncoding.cte" title="Link to this definition">¶</a></dt>
  449. <dd><p>Valid values are <code class="docutils literal notranslate"><span class="pre">7bit</span></code>, <code class="docutils literal notranslate"><span class="pre">8bit</span></code>, <code class="docutils literal notranslate"><span class="pre">base64</span></code>, and
  450. <code class="docutils literal notranslate"><span class="pre">quoted-printable</span></code>. See <span class="target" id="index-10"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2045.html"><strong>RFC 2045</strong></a> for more information.</p>
  451. </dd></dl>
  452. </dd></dl>
  453. <dl class="py class">
  454. <dt class="sig sig-object py" id="email.headerregistry.HeaderRegistry">
  455. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.headerregistry.</span></span><span class="sig-name descname"><span class="pre">HeaderRegistry</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">base_class</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">BaseHeader</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default_class</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">UnstructuredHeader</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">use_default_map</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.headerregistry.HeaderRegistry" title="Link to this definition">¶</a></dt>
  456. <dd><p>This is the factory used by <a class="reference internal" href="email.policy.html#email.policy.EmailPolicy" title="email.policy.EmailPolicy"><code class="xref py py-class docutils literal notranslate"><span class="pre">EmailPolicy</span></code></a> by default.
  457. <code class="docutils literal notranslate"><span class="pre">HeaderRegistry</span></code> builds the class used to create a header instance
  458. dynamically, using <em>base_class</em> and a specialized class retrieved from a
  459. registry that it holds. When a given header name does not appear in the
  460. registry, the class specified by <em>default_class</em> is used as the specialized
  461. class. When <em>use_default_map</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> (the default), the standard
  462. mapping of header names to classes is copied in to the registry during
  463. initialization. <em>base_class</em> is always the last class in the generated
  464. class’s <code class="docutils literal notranslate"><span class="pre">__bases__</span></code> list.</p>
  465. <p>The default mappings are:</p>
  466. <blockquote>
  467. <div><dl class="field-list simple">
  468. <dt class="field-odd">subject<span class="colon">:</span></dt>
  469. <dd class="field-odd"><p>UniqueUnstructuredHeader</p>
  470. </dd>
  471. <dt class="field-even">date<span class="colon">:</span></dt>
  472. <dd class="field-even"><p>UniqueDateHeader</p>
  473. </dd>
  474. <dt class="field-odd">resent-date<span class="colon">:</span></dt>
  475. <dd class="field-odd"><p>DateHeader</p>
  476. </dd>
  477. <dt class="field-even">orig-date<span class="colon">:</span></dt>
  478. <dd class="field-even"><p>UniqueDateHeader</p>
  479. </dd>
  480. <dt class="field-odd">sender<span class="colon">:</span></dt>
  481. <dd class="field-odd"><p>UniqueSingleAddressHeader</p>
  482. </dd>
  483. <dt class="field-even">resent-sender<span class="colon">:</span></dt>
  484. <dd class="field-even"><p>SingleAddressHeader</p>
  485. </dd>
  486. <dt class="field-odd">to<span class="colon">:</span></dt>
  487. <dd class="field-odd"><p>UniqueAddressHeader</p>
  488. </dd>
  489. <dt class="field-even">resent-to<span class="colon">:</span></dt>
  490. <dd class="field-even"><p>AddressHeader</p>
  491. </dd>
  492. <dt class="field-odd">cc<span class="colon">:</span></dt>
  493. <dd class="field-odd"><p>UniqueAddressHeader</p>
  494. </dd>
  495. <dt class="field-even">resent-cc<span class="colon">:</span></dt>
  496. <dd class="field-even"><p>AddressHeader</p>
  497. </dd>
  498. <dt class="field-odd">bcc<span class="colon">:</span></dt>
  499. <dd class="field-odd"><p>UniqueAddressHeader</p>
  500. </dd>
  501. <dt class="field-even">resent-bcc<span class="colon">:</span></dt>
  502. <dd class="field-even"><p>AddressHeader</p>
  503. </dd>
  504. <dt class="field-odd">from<span class="colon">:</span></dt>
  505. <dd class="field-odd"><p>UniqueAddressHeader</p>
  506. </dd>
  507. <dt class="field-even">resent-from<span class="colon">:</span></dt>
  508. <dd class="field-even"><p>AddressHeader</p>
  509. </dd>
  510. <dt class="field-odd">reply-to<span class="colon">:</span></dt>
  511. <dd class="field-odd"><p>UniqueAddressHeader</p>
  512. </dd>
  513. <dt class="field-even">mime-version<span class="colon">:</span></dt>
  514. <dd class="field-even"><p>MIMEVersionHeader</p>
  515. </dd>
  516. <dt class="field-odd">content-type<span class="colon">:</span></dt>
  517. <dd class="field-odd"><p>ContentTypeHeader</p>
  518. </dd>
  519. <dt class="field-even">content-disposition<span class="colon">:</span></dt>
  520. <dd class="field-even"><p>ContentDispositionHeader</p>
  521. </dd>
  522. <dt class="field-odd">content-transfer-encoding<span class="colon">:</span></dt>
  523. <dd class="field-odd"><p>ContentTransferEncodingHeader</p>
  524. </dd>
  525. <dt class="field-even">message-id<span class="colon">:</span></dt>
  526. <dd class="field-even"><p>MessageIDHeader</p>
  527. </dd>
  528. </dl>
  529. </div></blockquote>
  530. <p><code class="docutils literal notranslate"><span class="pre">HeaderRegistry</span></code> has the following methods:</p>
  531. <dl class="py method">
  532. <dt class="sig sig-object py" id="email.headerregistry.HeaderRegistry.map_to_type">
  533. <span class="sig-name descname"><span class="pre">map_to_type</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">self</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cls</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#email.headerregistry.HeaderRegistry.map_to_type" title="Link to this definition">¶</a></dt>
  534. <dd><p><em>name</em> is the name of the header to be mapped. It will be converted to
  535. lower case in the registry. <em>cls</em> is the specialized class to be used,
  536. along with <em>base_class</em>, to create the class used to instantiate headers
  537. that match <em>name</em>.</p>
  538. </dd></dl>
  539. <dl class="py method">
  540. <dt class="sig sig-object py" id="email.headerregistry.HeaderRegistry.__getitem__">
  541. <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.headerregistry.HeaderRegistry.__getitem__" title="Link to this definition">¶</a></dt>
  542. <dd><p>Construct and return a class to handle creating a <em>name</em> header.</p>
  543. </dd></dl>
  544. <dl class="py method">
  545. <dt class="sig sig-object py" id="email.headerregistry.HeaderRegistry.__call__">
  546. <span class="sig-name descname"><span class="pre">__call__</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.headerregistry.HeaderRegistry.__call__" title="Link to this definition">¶</a></dt>
  547. <dd><p>Retrieves the specialized header associated with <em>name</em> from the
  548. registry (using <em>default_class</em> if <em>name</em> does not appear in the
  549. registry) and composes it with <em>base_class</em> to produce a class,
  550. calls the constructed class’s constructor, passing it the same
  551. argument list, and finally returns the class instance created thereby.</p>
  552. </dd></dl>
  553. </dd></dl>
  554. <p>The following classes are the classes used to represent data parsed from
  555. structured headers and can, in general, be used by an application program to
  556. construct structured values to assign to specific headers.</p>
  557. <dl class="py class">
  558. <dt class="sig sig-object py" id="email.headerregistry.Address">
  559. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.headerregistry.</span></span><span class="sig-name descname"><span class="pre">Address</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">display_name</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">username</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">domain</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">addr_spec</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.headerregistry.Address" title="Link to this definition">¶</a></dt>
  560. <dd><p>The class used to represent an email address. The general form of an
  561. address is:</p>
  562. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="n">display_name</span><span class="p">]</span> <span class="o">&lt;</span><span class="n">username</span><span class="nd">@domain</span><span class="o">&gt;</span>
  563. </pre></div>
  564. </div>
  565. <p>or:</p>
  566. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">username</span><span class="nd">@domain</span>
  567. </pre></div>
  568. </div>
  569. <p>where each part must conform to specific syntax rules spelled out in
  570. <span class="target" id="index-11"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5322.html"><strong>RFC 5322</strong></a>.</p>
  571. <p>As a convenience <em>addr_spec</em> can be specified instead of <em>username</em> and
  572. <em>domain</em>, in which case <em>username</em> and <em>domain</em> will be parsed from the
  573. <em>addr_spec</em>. An <em>addr_spec</em> must be a properly RFC quoted string; if it is
  574. not <code class="docutils literal notranslate"><span class="pre">Address</span></code> will raise an error. Unicode characters are allowed and
  575. will be property encoded when serialized. However, per the RFCs, unicode is
  576. <em>not</em> allowed in the username portion of the address.</p>
  577. <dl class="py attribute">
  578. <dt class="sig sig-object py" id="email.headerregistry.Address.display_name">
  579. <span class="sig-name descname"><span class="pre">display_name</span></span><a class="headerlink" href="#email.headerregistry.Address.display_name" title="Link to this definition">¶</a></dt>
  580. <dd><p>The display name portion of the address, if any, with all quoting
  581. removed. If the address does not have a display name, this attribute
  582. will be an empty string.</p>
  583. </dd></dl>
  584. <dl class="py attribute">
  585. <dt class="sig sig-object py" id="email.headerregistry.Address.username">
  586. <span class="sig-name descname"><span class="pre">username</span></span><a class="headerlink" href="#email.headerregistry.Address.username" title="Link to this definition">¶</a></dt>
  587. <dd><p>The <code class="docutils literal notranslate"><span class="pre">username</span></code> portion of the address, with all quoting removed.</p>
  588. </dd></dl>
  589. <dl class="py attribute">
  590. <dt class="sig sig-object py" id="email.headerregistry.Address.domain">
  591. <span class="sig-name descname"><span class="pre">domain</span></span><a class="headerlink" href="#email.headerregistry.Address.domain" title="Link to this definition">¶</a></dt>
  592. <dd><p>The <code class="docutils literal notranslate"><span class="pre">domain</span></code> portion of the address.</p>
  593. </dd></dl>
  594. <dl class="py attribute">
  595. <dt class="sig sig-object py" id="email.headerregistry.Address.addr_spec">
  596. <span class="sig-name descname"><span class="pre">addr_spec</span></span><a class="headerlink" href="#email.headerregistry.Address.addr_spec" title="Link to this definition">¶</a></dt>
  597. <dd><p>The <code class="docutils literal notranslate"><span class="pre">username&#64;domain</span></code> portion of the address, correctly quoted
  598. for use as a bare address (the second form shown above). This
  599. attribute is not mutable.</p>
  600. </dd></dl>
  601. <dl class="py method">
  602. <dt class="sig sig-object py" id="email.headerregistry.Address.__str__">
  603. <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.headerregistry.Address.__str__" title="Link to this definition">¶</a></dt>
  604. <dd><p>The <code class="docutils literal notranslate"><span class="pre">str</span></code> value of the object is the address quoted according to
  605. <span class="target" id="index-12"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5322.html"><strong>RFC 5322</strong></a> rules, but with no Content Transfer Encoding of any non-ASCII
  606. characters.</p>
  607. </dd></dl>
  608. <p>To support SMTP (<span class="target" id="index-13"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5321.html"><strong>RFC 5321</strong></a>), <code class="docutils literal notranslate"><span class="pre">Address</span></code> handles one special case: if
  609. <code class="docutils literal notranslate"><span class="pre">username</span></code> and <code class="docutils literal notranslate"><span class="pre">domain</span></code> are both the empty string (or <code class="docutils literal notranslate"><span class="pre">None</span></code>), then
  610. the string value of the <code class="docutils literal notranslate"><span class="pre">Address</span></code> is <code class="docutils literal notranslate"><span class="pre">&lt;&gt;</span></code>.</p>
  611. </dd></dl>
  612. <dl class="py class">
  613. <dt class="sig sig-object py" id="email.headerregistry.Group">
  614. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">email.headerregistry.</span></span><span class="sig-name descname"><span class="pre">Group</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">display_name</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">addresses</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.headerregistry.Group" title="Link to this definition">¶</a></dt>
  615. <dd><p>The class used to represent an address group. The general form of an
  616. address group is:</p>
  617. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">display_name</span><span class="p">:</span> <span class="p">[</span><span class="n">address</span><span class="o">-</span><span class="nb">list</span><span class="p">];</span>
  618. </pre></div>
  619. </div>
  620. <p>As a convenience for processing lists of addresses that consist of a mixture
  621. of groups and single addresses, a <code class="docutils literal notranslate"><span class="pre">Group</span></code> may also be used to represent
  622. single addresses that are not part of a group by setting <em>display_name</em> to
  623. <code class="docutils literal notranslate"><span class="pre">None</span></code> and providing a list of the single address as <em>addresses</em>.</p>
  624. <dl class="py attribute">
  625. <dt class="sig sig-object py" id="email.headerregistry.Group.display_name">
  626. <span class="sig-name descname"><span class="pre">display_name</span></span><a class="headerlink" href="#email.headerregistry.Group.display_name" title="Link to this definition">¶</a></dt>
  627. <dd><p>The <code class="docutils literal notranslate"><span class="pre">display_name</span></code> of the group. If it is <code class="docutils literal notranslate"><span class="pre">None</span></code> and there is
  628. exactly one <code class="docutils literal notranslate"><span class="pre">Address</span></code> in <code class="docutils literal notranslate"><span class="pre">addresses</span></code>, then the <code class="docutils literal notranslate"><span class="pre">Group</span></code> represents a
  629. single address that is not in a group.</p>
  630. </dd></dl>
  631. <dl class="py attribute">
  632. <dt class="sig sig-object py" id="email.headerregistry.Group.addresses">
  633. <span class="sig-name descname"><span class="pre">addresses</span></span><a class="headerlink" href="#email.headerregistry.Group.addresses" title="Link to this definition">¶</a></dt>
  634. <dd><p>A possibly empty tuple of <a class="reference internal" href="#email.headerregistry.Address" title="email.headerregistry.Address"><code class="xref py py-class docutils literal notranslate"><span class="pre">Address</span></code></a> objects representing the
  635. addresses in the group.</p>
  636. </dd></dl>
  637. <dl class="py method">
  638. <dt class="sig sig-object py" id="email.headerregistry.Group.__str__">
  639. <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.headerregistry.Group.__str__" title="Link to this definition">¶</a></dt>
  640. <dd><p>The <code class="docutils literal notranslate"><span class="pre">str</span></code> value of a <code class="docutils literal notranslate"><span class="pre">Group</span></code> is formatted according to <span class="target" id="index-14"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5322.html"><strong>RFC 5322</strong></a>,
  641. but with no Content Transfer Encoding of any non-ASCII characters. If
  642. <code class="docutils literal notranslate"><span class="pre">display_name</span></code> is none and there is a single <code class="docutils literal notranslate"><span class="pre">Address</span></code> in the
  643. <code class="docutils literal notranslate"><span class="pre">addresses</span></code> list, the <code class="docutils literal notranslate"><span class="pre">str</span></code> value will be the same as the <code class="docutils literal notranslate"><span class="pre">str</span></code> of
  644. that single <code class="docutils literal notranslate"><span class="pre">Address</span></code>.</p>
  645. </dd></dl>
  646. </dd></dl>
  647. <p class="rubric">Footnotes</p>
  648. <aside class="footnote-list brackets">
  649. <aside class="footnote brackets" id="id2" role="doc-footnote">
  650. <span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id1">1</a><span class="fn-bracket">]</span></span>
  651. <p>Originally added in 3.3 as a <a class="reference internal" href="../glossary.html#term-provisional-package"><span class="xref std std-term">provisional module</span></a></p>
  652. </aside>
  653. </aside>
  654. </section>
  655. <div class="clearer"></div>
  656. </div>
  657. </div>
  658. </div>
  659. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  660. <div class="sphinxsidebarwrapper">
  661. <div>
  662. <h4>Previous topic</h4>
  663. <p class="topless"><a href="email.errors.html"
  664. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.errors</span></code>: Exception and Defect classes</a></p>
  665. </div>
  666. <div>
  667. <h4>Next topic</h4>
  668. <p class="topless"><a href="email.contentmanager.html"
  669. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.contentmanager</span></code>: Managing MIME Content</a></p>
  670. </div>
  671. <div role="note" aria-label="source link">
  672. <h3>This Page</h3>
  673. <ul class="this-page-menu">
  674. <li><a href="../bugs.html">Report a Bug</a></li>
  675. <li>
  676. <a href="https://github.com/python/cpython/blob/main/Doc/library/email.headerregistry.rst"
  677. rel="nofollow">Show Source
  678. </a>
  679. </li>
  680. </ul>
  681. </div>
  682. </div>
  683. <div id="sidebarbutton" title="Collapse sidebar">
  684. <span>«</span>
  685. </div>
  686. </div>
  687. <div class="clearer"></div>
  688. </div>
  689. <div class="related" role="navigation" aria-label="related navigation">
  690. <h3>Navigation</h3>
  691. <ul>
  692. <li class="right" style="margin-right: 10px">
  693. <a href="../genindex.html" title="General Index"
  694. >index</a></li>
  695. <li class="right" >
  696. <a href="../py-modindex.html" title="Python Module Index"
  697. >modules</a> |</li>
  698. <li class="right" >
  699. <a href="email.contentmanager.html" title="email.contentmanager: Managing MIME Content"
  700. >next</a> |</li>
  701. <li class="right" >
  702. <a href="email.errors.html" title="email.errors: Exception and Defect classes"
  703. >previous</a> |</li>
  704. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  705. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  706. <li class="switchers">
  707. <div class="language_switcher_placeholder"></div>
  708. <div class="version_switcher_placeholder"></div>
  709. </li>
  710. <li>
  711. </li>
  712. <li id="cpython-language-and-version">
  713. <a href="../index.html">3.12.3 Documentation</a> &#187;
  714. </li>
  715. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  716. <li class="nav-item nav-item-2"><a href="netdata.html" >Internet Data Handling</a> &#187;</li>
  717. <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>
  718. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.headerregistry</span></code>: Custom Header Objects</a></li>
  719. <li class="right">
  720. <div class="inline-search" role="search">
  721. <form class="inline-search" action="../search.html" method="get">
  722. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  723. <input type="submit" value="Go" />
  724. </form>
  725. </div>
  726. |
  727. </li>
  728. <li class="right">
  729. <label class="theme-selector-label">
  730. Theme
  731. <select class="theme-selector" oninput="activateTheme(this.value)">
  732. <option value="auto" selected>Auto</option>
  733. <option value="light">Light</option>
  734. <option value="dark">Dark</option>
  735. </select>
  736. </label> |</li>
  737. </ul>
  738. </div>
  739. <div class="footer">
  740. &copy;
  741. <a href="../copyright.html">
  742. Copyright
  743. </a>
  744. 2001-2024, Python Software Foundation.
  745. <br />
  746. This page is licensed under the Python Software Foundation License Version 2.
  747. <br />
  748. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  749. <br />
  750. See <a href="/license.html">History and License</a> for more information.<br />
  751. <br />
  752. The Python Software Foundation is a non-profit corporation.
  753. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  754. <br />
  755. <br />
  756. Last updated on Apr 09, 2024 (13:47 UTC).
  757. <a href="/bugs.html">Found a bug</a>?
  758. <br />
  759. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  760. </div>
  761. </body>
  762. </html>
上海开阖软件有限公司 沪ICP备12045867号-1