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.

2197 lines
172KB

  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="mailbox — Manipulate mailboxes in various formats" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/mailbox.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/mailbox.py This module defines two classes, Mailbox and Message, for accessing and manipulating on-disk mailboxes and the messages they contain. Mailbox offers a dictionary-like ma..." />
  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/mailbox.py This module defines two classes, Mailbox and Message, for accessing and manipulating on-disk mailboxes and the messages they contain. Mailbox offers a dictionary-like ma..." />
  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>mailbox — Manipulate mailboxes in various formats &#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="mimetypes — Map filenames to MIME types" href="mimetypes.html" />
  33. <link rel="prev" title="json — JSON encoder and decoder" href="json.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/mailbox.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">mailbox</span></code> — Manipulate mailboxes in various formats</a><ul>
  86. <li><a class="reference internal" href="#mailbox-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code> objects</a><ul>
  87. <li><a class="reference internal" href="#maildir-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">Maildir</span></code> objects</a></li>
  88. <li><a class="reference internal" href="#mbox-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">mbox</span></code> objects</a></li>
  89. <li><a class="reference internal" href="#mh-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">MH</span></code> objects</a></li>
  90. <li><a class="reference internal" href="#babyl-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">Babyl</span></code> objects</a></li>
  91. <li><a class="reference internal" href="#mmdf-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">MMDF</span></code> objects</a></li>
  92. </ul>
  93. </li>
  94. <li><a class="reference internal" href="#message-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code> objects</a><ul>
  95. <li><a class="reference internal" href="#maildirmessage-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">MaildirMessage</span></code> objects</a></li>
  96. <li><a class="reference internal" href="#mboxmessage-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">mboxMessage</span></code> objects</a></li>
  97. <li><a class="reference internal" href="#mhmessage-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">MHMessage</span></code> objects</a></li>
  98. <li><a class="reference internal" href="#babylmessage-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">BabylMessage</span></code> objects</a></li>
  99. <li><a class="reference internal" href="#mmdfmessage-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">MMDFMessage</span></code> objects</a></li>
  100. </ul>
  101. </li>
  102. <li><a class="reference internal" href="#exceptions">Exceptions</a></li>
  103. <li><a class="reference internal" href="#examples">Examples</a></li>
  104. </ul>
  105. </li>
  106. </ul>
  107. </div>
  108. <div>
  109. <h4>Previous topic</h4>
  110. <p class="topless"><a href="json.html"
  111. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code> — JSON encoder and decoder</a></p>
  112. </div>
  113. <div>
  114. <h4>Next topic</h4>
  115. <p class="topless"><a href="mimetypes.html"
  116. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">mimetypes</span></code> — Map filenames to MIME types</a></p>
  117. </div>
  118. <div role="note" aria-label="source link">
  119. <h3>This Page</h3>
  120. <ul class="this-page-menu">
  121. <li><a href="../bugs.html">Report a Bug</a></li>
  122. <li>
  123. <a href="https://github.com/python/cpython/blob/main/Doc/library/mailbox.rst"
  124. rel="nofollow">Show Source
  125. </a>
  126. </li>
  127. </ul>
  128. </div>
  129. </nav>
  130. </div>
  131. </div>
  132. <div class="related" role="navigation" aria-label="related navigation">
  133. <h3>Navigation</h3>
  134. <ul>
  135. <li class="right" style="margin-right: 10px">
  136. <a href="../genindex.html" title="General Index"
  137. accesskey="I">index</a></li>
  138. <li class="right" >
  139. <a href="../py-modindex.html" title="Python Module Index"
  140. >modules</a> |</li>
  141. <li class="right" >
  142. <a href="mimetypes.html" title="mimetypes — Map filenames to MIME types"
  143. accesskey="N">next</a> |</li>
  144. <li class="right" >
  145. <a href="json.html" title="json — JSON encoder and decoder"
  146. accesskey="P">previous</a> |</li>
  147. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  148. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  149. <li class="switchers">
  150. <div class="language_switcher_placeholder"></div>
  151. <div class="version_switcher_placeholder"></div>
  152. </li>
  153. <li>
  154. </li>
  155. <li id="cpython-language-and-version">
  156. <a href="../index.html">3.12.3 Documentation</a> &#187;
  157. </li>
  158. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  159. <li class="nav-item nav-item-2"><a href="netdata.html" accesskey="U">Internet Data Handling</a> &#187;</li>
  160. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">mailbox</span></code> — Manipulate mailboxes in various formats</a></li>
  161. <li class="right">
  162. <div class="inline-search" role="search">
  163. <form class="inline-search" action="../search.html" method="get">
  164. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  165. <input type="submit" value="Go" />
  166. </form>
  167. </div>
  168. |
  169. </li>
  170. <li class="right">
  171. <label class="theme-selector-label">
  172. Theme
  173. <select class="theme-selector" oninput="activateTheme(this.value)">
  174. <option value="auto" selected>Auto</option>
  175. <option value="light">Light</option>
  176. <option value="dark">Dark</option>
  177. </select>
  178. </label> |</li>
  179. </ul>
  180. </div>
  181. <div class="document">
  182. <div class="documentwrapper">
  183. <div class="bodywrapper">
  184. <div class="body" role="main">
  185. <section id="module-mailbox">
  186. <span id="mailbox-manipulate-mailboxes-in-various-formats"></span><h1><a class="reference internal" href="#module-mailbox" title="mailbox: Manipulate mailboxes in various formats"><code class="xref py py-mod docutils literal notranslate"><span class="pre">mailbox</span></code></a> — Manipulate mailboxes in various formats<a class="headerlink" href="#module-mailbox" title="Link to this heading">¶</a></h1>
  187. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/mailbox.py">Lib/mailbox.py</a></p>
  188. <hr class="docutils" />
  189. <p>This module defines two classes, <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code></a> and <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a>, for
  190. accessing and manipulating on-disk mailboxes and the messages they contain.
  191. <code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code> offers a dictionary-like mapping from keys to messages.
  192. <code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code> extends the <a class="reference internal" href="email.message.html#module-email.message" title="email.message: The base class representing email messages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.message</span></code></a> module’s
  193. <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> class with format-specific state and behavior.
  194. Supported mailbox formats are Maildir, mbox, MH, Babyl, and MMDF.</p>
  195. <div class="admonition seealso">
  196. <p class="admonition-title">See also</p>
  197. <dl class="simple">
  198. <dt>Module <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></dt><dd><p>Represent and manipulate messages.</p>
  199. </dd>
  200. </dl>
  201. </div>
  202. <section id="mailbox-objects">
  203. <span id="id1"></span><h2><code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code> objects<a class="headerlink" href="#mailbox-objects" title="Link to this heading">¶</a></h2>
  204. <dl class="py class">
  205. <dt class="sig sig-object py" id="mailbox.Mailbox">
  206. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">mailbox.</span></span><span class="sig-name descname"><span class="pre">Mailbox</span></span><a class="headerlink" href="#mailbox.Mailbox" title="Link to this definition">¶</a></dt>
  207. <dd><p>A mailbox, which may be inspected and modified.</p>
  208. <p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code> class defines an interface and is not intended to be
  209. instantiated. Instead, format-specific subclasses should inherit from
  210. <code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code> and your code should instantiate a particular subclass.</p>
  211. <p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code> interface is dictionary-like, with small keys
  212. corresponding to messages. Keys are issued by the <code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code> instance
  213. with which they will be used and are only meaningful to that <code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code>
  214. instance. A key continues to identify a message even if the corresponding
  215. message is modified, such as by replacing it with another message.</p>
  216. <p>Messages may be added to a <code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code> instance using the set-like
  217. method <a class="reference internal" href="#mailbox.Mailbox.add" title="mailbox.Mailbox.add"><code class="xref py py-meth docutils literal notranslate"><span class="pre">add()</span></code></a> and removed using a <code class="docutils literal notranslate"><span class="pre">del</span></code> statement or the set-like
  218. methods <a class="reference internal" href="#mailbox.Mailbox.remove" title="mailbox.Mailbox.remove"><code class="xref py py-meth docutils literal notranslate"><span class="pre">remove()</span></code></a> and <a class="reference internal" href="#mailbox.Mailbox.discard" title="mailbox.Mailbox.discard"><code class="xref py py-meth docutils literal notranslate"><span class="pre">discard()</span></code></a>.</p>
  219. <p><code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code> interface semantics differ from dictionary semantics in some
  220. noteworthy ways. Each time a message is requested, a new representation
  221. (typically a <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> instance) is generated based upon the current
  222. state of the mailbox. Similarly, when a message is added to a
  223. <code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code> instance, the provided message representation’s contents are
  224. copied. In neither case is a reference to the message representation kept by
  225. the <code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code> instance.</p>
  226. <p>The default <code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code> <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a> iterates over message
  227. representations, not keys as the default <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dictionary</span></code></a>
  228. iterator does. Moreover, modification of a
  229. mailbox during iteration is safe and well-defined. Messages added to the
  230. mailbox after an iterator is created will not be seen by the
  231. iterator. Messages removed from the mailbox before the iterator yields them
  232. will be silently skipped, though using a key from an iterator may result in a
  233. <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> exception if the corresponding message is subsequently
  234. removed.</p>
  235. <div class="admonition warning">
  236. <p class="admonition-title">Warning</p>
  237. <p>Be very cautious when modifying mailboxes that might be simultaneously
  238. changed by some other process. The safest mailbox format to use for such
  239. tasks is <a class="reference internal" href="#mailbox.Maildir" title="mailbox.Maildir"><code class="xref py py-class docutils literal notranslate"><span class="pre">Maildir</span></code></a>; try to avoid using single-file formats such as
  240. <a class="reference internal" href="#mailbox.mbox" title="mailbox.mbox"><code class="xref py py-class docutils literal notranslate"><span class="pre">mbox</span></code></a> for
  241. concurrent writing. If you’re modifying a mailbox, you <em>must</em> lock it by
  242. calling the <a class="reference internal" href="#mailbox.Mailbox.lock" title="mailbox.Mailbox.lock"><code class="xref py py-meth docutils literal notranslate"><span class="pre">lock()</span></code></a> and <a class="reference internal" href="#mailbox.Mailbox.unlock" title="mailbox.Mailbox.unlock"><code class="xref py py-meth docutils literal notranslate"><span class="pre">unlock()</span></code></a> methods <em>before</em> reading any
  243. messages in the file or making any changes by adding or deleting a
  244. message. Failing to lock the mailbox runs the risk of losing messages or
  245. corrupting the entire mailbox.</p>
  246. </div>
  247. <p><code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code> instances have the following methods:</p>
  248. <dl class="py method">
  249. <dt class="sig sig-object py" id="mailbox.Mailbox.add">
  250. <span class="sig-name descname"><span class="pre">add</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">message</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.add" title="Link to this definition">¶</a></dt>
  251. <dd><p>Add <em>message</em> to the mailbox and return the key that has been assigned to
  252. it.</p>
  253. <p>Parameter <em>message</em> may be a <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> instance, an
  254. <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">email.message.Message</span></code></a> instance, a string, a byte string, or a
  255. file-like object (which should be open in binary mode). If <em>message</em> is
  256. an instance of the
  257. appropriate format-specific <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> subclass (e.g., if it’s an
  258. <a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">mboxMessage</span></code></a> instance and this is an <a class="reference internal" href="#mailbox.mbox" title="mailbox.mbox"><code class="xref py py-class docutils literal notranslate"><span class="pre">mbox</span></code></a> instance), its
  259. format-specific information is used. Otherwise, reasonable defaults for
  260. format-specific information are used.</p>
  261. <div class="versionchanged">
  262. <p><span class="versionmodified changed">Changed in version 3.2: </span>Support for binary input was added.</p>
  263. </div>
  264. </dd></dl>
  265. <dl class="py method">
  266. <dt class="sig sig-object py" id="mailbox.Mailbox.remove">
  267. <span class="sig-name descname"><span class="pre">remove</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.remove" title="Link to this definition">¶</a></dt>
  268. <dt class="sig sig-object py" id="mailbox.Mailbox.__delitem__">
  269. <span class="sig-name descname"><span class="pre">__delitem__</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.__delitem__" title="Link to this definition">¶</a></dt>
  270. <dt class="sig sig-object py" id="mailbox.Mailbox.discard">
  271. <span class="sig-name descname"><span class="pre">discard</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.discard" title="Link to this definition">¶</a></dt>
  272. <dd><p>Delete the message corresponding to <em>key</em> from the mailbox.</p>
  273. <p>If no such message exists, a <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> exception is raised if the
  274. method was called as <a class="reference internal" href="#mailbox.Mailbox.remove" title="mailbox.Mailbox.remove"><code class="xref py py-meth docutils literal notranslate"><span class="pre">remove()</span></code></a> or <a class="reference internal" href="#mailbox.Mailbox.__delitem__" title="mailbox.Mailbox.__delitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__delitem__()</span></code></a> but no
  275. exception is raised if the method was called as <a class="reference internal" href="#mailbox.Mailbox.discard" title="mailbox.Mailbox.discard"><code class="xref py py-meth docutils literal notranslate"><span class="pre">discard()</span></code></a>. The
  276. behavior of <a class="reference internal" href="#mailbox.Mailbox.discard" title="mailbox.Mailbox.discard"><code class="xref py py-meth docutils literal notranslate"><span class="pre">discard()</span></code></a> may be preferred if the underlying mailbox
  277. format supports concurrent modification by other processes.</p>
  278. </dd></dl>
  279. <dl class="py method">
  280. <dt class="sig sig-object py" id="mailbox.Mailbox.__setitem__">
  281. <span class="sig-name descname"><span class="pre">__setitem__</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">message</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.__setitem__" title="Link to this definition">¶</a></dt>
  282. <dd><p>Replace the message corresponding to <em>key</em> with <em>message</em>. Raise a
  283. <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> exception if no message already corresponds to <em>key</em>.</p>
  284. <p>As with <a class="reference internal" href="#mailbox.Mailbox.add" title="mailbox.Mailbox.add"><code class="xref py py-meth docutils literal notranslate"><span class="pre">add()</span></code></a>, parameter <em>message</em> may be a <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a>
  285. instance, an <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">email.message.Message</span></code></a> instance, a string, a byte
  286. string, or a file-like object (which should be open in binary mode). If
  287. <em>message</em> is an
  288. instance of the appropriate format-specific <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> subclass
  289. (e.g., if it’s an <a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">mboxMessage</span></code></a> instance and this is an
  290. <a class="reference internal" href="#mailbox.mbox" title="mailbox.mbox"><code class="xref py py-class docutils literal notranslate"><span class="pre">mbox</span></code></a> instance), its format-specific information is
  291. used. Otherwise, the format-specific information of the message that
  292. currently corresponds to <em>key</em> is left unchanged.</p>
  293. </dd></dl>
  294. <dl class="py method">
  295. <dt class="sig sig-object py" id="mailbox.Mailbox.iterkeys">
  296. <span class="sig-name descname"><span class="pre">iterkeys</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.iterkeys" title="Link to this definition">¶</a></dt>
  297. <dd><p>Return an <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a> over all keys</p>
  298. </dd></dl>
  299. <dl class="py method">
  300. <dt class="sig sig-object py" id="mailbox.Mailbox.keys">
  301. <span class="sig-name descname"><span class="pre">keys</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.keys" title="Link to this definition">¶</a></dt>
  302. <dd><p>The same as <a class="reference internal" href="#mailbox.Mailbox.iterkeys" title="mailbox.Mailbox.iterkeys"><code class="xref py py-meth docutils literal notranslate"><span class="pre">iterkeys()</span></code></a>, except that a <a class="reference internal" href="stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a> is returned
  303. rather than an <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a></p>
  304. </dd></dl>
  305. <dl class="py method">
  306. <dt class="sig sig-object py" id="mailbox.Mailbox.itervalues">
  307. <span class="sig-name descname"><span class="pre">itervalues</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.itervalues" title="Link to this definition">¶</a></dt>
  308. <dt class="sig sig-object py" id="mailbox.Mailbox.__iter__">
  309. <span class="sig-name descname"><span class="pre">__iter__</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.__iter__" title="Link to this definition">¶</a></dt>
  310. <dd><p>Return an <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a> over representations of all messages.
  311. The messages are represented
  312. as instances of the appropriate format-specific <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> subclass
  313. unless a custom message factory was specified when the <code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code>
  314. instance was initialized.</p>
  315. <div class="admonition note">
  316. <p class="admonition-title">Note</p>
  317. <p>The behavior of <a class="reference internal" href="#mailbox.Mailbox.__iter__" title="mailbox.Mailbox.__iter__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__iter__()</span></code></a> is unlike that of dictionaries, which
  318. iterate over keys.</p>
  319. </div>
  320. </dd></dl>
  321. <dl class="py method">
  322. <dt class="sig sig-object py" id="mailbox.Mailbox.values">
  323. <span class="sig-name descname"><span class="pre">values</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.values" title="Link to this definition">¶</a></dt>
  324. <dd><p>The same as <a class="reference internal" href="#mailbox.Mailbox.itervalues" title="mailbox.Mailbox.itervalues"><code class="xref py py-meth docutils literal notranslate"><span class="pre">itervalues()</span></code></a>, except that a <a class="reference internal" href="stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a> is returned
  325. rather than an <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a></p>
  326. </dd></dl>
  327. <dl class="py method">
  328. <dt class="sig sig-object py" id="mailbox.Mailbox.iteritems">
  329. <span class="sig-name descname"><span class="pre">iteritems</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.iteritems" title="Link to this definition">¶</a></dt>
  330. <dd><p>Return an <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a> over (<em>key</em>, <em>message</em>) pairs, where <em>key</em> is
  331. a key and <em>message</em> is a message representation. The messages are
  332. represented as instances of the appropriate format-specific
  333. <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> subclass unless a custom message factory was specified
  334. when the <code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code> instance was initialized.</p>
  335. </dd></dl>
  336. <dl class="py method">
  337. <dt class="sig sig-object py" id="mailbox.Mailbox.items">
  338. <span class="sig-name descname"><span class="pre">items</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.items" title="Link to this definition">¶</a></dt>
  339. <dd><p>The same as <a class="reference internal" href="#mailbox.Mailbox.iteritems" title="mailbox.Mailbox.iteritems"><code class="xref py py-meth docutils literal notranslate"><span class="pre">iteritems()</span></code></a>, except that a <a class="reference internal" href="stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a> of pairs is
  340. returned rather than an <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a> of pairs.</p>
  341. </dd></dl>
  342. <dl class="py method">
  343. <dt class="sig sig-object py" id="mailbox.Mailbox.get">
  344. <span class="sig-name descname"><span class="pre">get</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</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="#mailbox.Mailbox.get" title="Link to this definition">¶</a></dt>
  345. <dt class="sig sig-object py" id="mailbox.Mailbox.__getitem__">
  346. <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">key</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.__getitem__" title="Link to this definition">¶</a></dt>
  347. <dd><p>Return a representation of the message corresponding to <em>key</em>. If no such
  348. message exists, <em>default</em> is returned if the method was called as
  349. <a class="reference internal" href="#mailbox.Mailbox.get" title="mailbox.Mailbox.get"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code></a> and a <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> exception is raised if the method was
  350. called as <code class="xref py py-meth docutils literal notranslate"><span class="pre">__getitem__()</span></code>. The message is represented as an instance
  351. of the appropriate format-specific <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> subclass unless a
  352. custom message factory was specified when the <code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code> instance
  353. was initialized.</p>
  354. </dd></dl>
  355. <dl class="py method">
  356. <dt class="sig sig-object py" id="mailbox.Mailbox.get_message">
  357. <span class="sig-name descname"><span class="pre">get_message</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.get_message" title="Link to this definition">¶</a></dt>
  358. <dd><p>Return a representation of the message corresponding to <em>key</em> as an
  359. instance of the appropriate format-specific <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> subclass, or
  360. raise a <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> exception if no such message exists.</p>
  361. </dd></dl>
  362. <dl class="py method">
  363. <dt class="sig sig-object py" id="mailbox.Mailbox.get_bytes">
  364. <span class="sig-name descname"><span class="pre">get_bytes</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.get_bytes" title="Link to this definition">¶</a></dt>
  365. <dd><p>Return a byte representation of the message corresponding to <em>key</em>, or
  366. raise a <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> exception if no such message exists.</p>
  367. <div class="versionadded">
  368. <p><span class="versionmodified added">New in version 3.2.</span></p>
  369. </div>
  370. </dd></dl>
  371. <dl class="py method">
  372. <dt class="sig sig-object py" id="mailbox.Mailbox.get_string">
  373. <span class="sig-name descname"><span class="pre">get_string</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.get_string" title="Link to this definition">¶</a></dt>
  374. <dd><p>Return a string representation of the message corresponding to <em>key</em>, or
  375. raise a <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> exception if no such message exists. The
  376. message is processed through <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">email.message.Message</span></code></a> to
  377. convert it to a 7bit clean representation.</p>
  378. </dd></dl>
  379. <dl class="py method">
  380. <dt class="sig sig-object py" id="mailbox.Mailbox.get_file">
  381. <span class="sig-name descname"><span class="pre">get_file</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.get_file" title="Link to this definition">¶</a></dt>
  382. <dd><p>Return a <a class="reference internal" href="../glossary.html#term-file-like-object"><span class="xref std std-term">file-like</span></a> representation of the
  383. message corresponding to <em>key</em>,
  384. or raise a <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> exception if no such message exists. The
  385. file-like object behaves as if open in binary mode. This file should be
  386. closed once it is no longer needed.</p>
  387. <div class="versionchanged">
  388. <p><span class="versionmodified changed">Changed in version 3.2: </span>The file object really is a <a class="reference internal" href="../glossary.html#term-binary-file"><span class="xref std std-term">binary file</span></a>; previously it was
  389. incorrectly returned in text mode. Also, the <a class="reference internal" href="../glossary.html#term-file-like-object"><span class="xref std std-term">file-like object</span></a>
  390. now supports the <a class="reference internal" href="../glossary.html#term-context-manager"><span class="xref std std-term">context manager</span></a> protocol: you can use a
  391. <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> statement to automatically close it.</p>
  392. </div>
  393. <div class="admonition note">
  394. <p class="admonition-title">Note</p>
  395. <p>Unlike other representations of messages,
  396. <a class="reference internal" href="../glossary.html#term-file-like-object"><span class="xref std std-term">file-like</span></a> representations are not
  397. necessarily independent of the <code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code> instance that
  398. created them or of the underlying mailbox. More specific documentation
  399. is provided by each subclass.</p>
  400. </div>
  401. </dd></dl>
  402. <dl class="py method">
  403. <dt class="sig sig-object py" id="mailbox.Mailbox.__contains__">
  404. <span class="sig-name descname"><span class="pre">__contains__</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.__contains__" title="Link to this definition">¶</a></dt>
  405. <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if <em>key</em> corresponds to a message, <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>
  406. </dd></dl>
  407. <dl class="py method">
  408. <dt class="sig sig-object py" id="mailbox.Mailbox.__len__">
  409. <span class="sig-name descname"><span class="pre">__len__</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.__len__" title="Link to this definition">¶</a></dt>
  410. <dd><p>Return a count of messages in the mailbox.</p>
  411. </dd></dl>
  412. <dl class="py method">
  413. <dt class="sig sig-object py" id="mailbox.Mailbox.clear">
  414. <span class="sig-name descname"><span class="pre">clear</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.clear" title="Link to this definition">¶</a></dt>
  415. <dd><p>Delete all messages from the mailbox.</p>
  416. </dd></dl>
  417. <dl class="py method">
  418. <dt class="sig sig-object py" id="mailbox.Mailbox.pop">
  419. <span class="sig-name descname"><span class="pre">pop</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</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="#mailbox.Mailbox.pop" title="Link to this definition">¶</a></dt>
  420. <dd><p>Return a representation of the message corresponding to <em>key</em> and delete
  421. the message. If no such message exists, return <em>default</em>. The message is
  422. represented as an instance of the appropriate format-specific
  423. <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> subclass unless a custom message factory was specified
  424. when the <code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code> instance was initialized.</p>
  425. </dd></dl>
  426. <dl class="py method">
  427. <dt class="sig sig-object py" id="mailbox.Mailbox.popitem">
  428. <span class="sig-name descname"><span class="pre">popitem</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.popitem" title="Link to this definition">¶</a></dt>
  429. <dd><p>Return an arbitrary (<em>key</em>, <em>message</em>) pair, where <em>key</em> is a key and
  430. <em>message</em> is a message representation, and delete the corresponding
  431. message. If the mailbox is empty, raise a <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> exception. The
  432. message is represented as an instance of the appropriate format-specific
  433. <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> subclass unless a custom message factory was specified
  434. when the <code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code> instance was initialized.</p>
  435. </dd></dl>
  436. <dl class="py method">
  437. <dt class="sig sig-object py" id="mailbox.Mailbox.update">
  438. <span class="sig-name descname"><span class="pre">update</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">arg</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.update" title="Link to this definition">¶</a></dt>
  439. <dd><p>Parameter <em>arg</em> should be a <em>key</em>-to-<em>message</em> mapping or an iterable of
  440. (<em>key</em>, <em>message</em>) pairs. Updates the mailbox so that, for each given
  441. <em>key</em> and <em>message</em>, the message corresponding to <em>key</em> is set to
  442. <em>message</em> as if by using <a class="reference internal" href="#mailbox.Mailbox.__setitem__" title="mailbox.Mailbox.__setitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__setitem__()</span></code></a>. As with <a class="reference internal" href="#mailbox.Mailbox.__setitem__" title="mailbox.Mailbox.__setitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__setitem__()</span></code></a>,
  443. each <em>key</em> must already correspond to a message in the mailbox or else a
  444. <a class="reference internal" href="exceptions.html#KeyError" title="KeyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">KeyError</span></code></a> exception will be raised, so in general it is incorrect
  445. for <em>arg</em> to be a <code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code> instance.</p>
  446. <div class="admonition note">
  447. <p class="admonition-title">Note</p>
  448. <p>Unlike with dictionaries, keyword arguments are not supported.</p>
  449. </div>
  450. </dd></dl>
  451. <dl class="py method">
  452. <dt class="sig sig-object py" id="mailbox.Mailbox.flush">
  453. <span class="sig-name descname"><span class="pre">flush</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.flush" title="Link to this definition">¶</a></dt>
  454. <dd><p>Write any pending changes to the filesystem. For some <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code></a>
  455. subclasses, changes are always written immediately and <code class="xref py py-meth docutils literal notranslate"><span class="pre">flush()</span></code> does
  456. nothing, but you should still make a habit of calling this method.</p>
  457. </dd></dl>
  458. <dl class="py method">
  459. <dt class="sig sig-object py" id="mailbox.Mailbox.lock">
  460. <span class="sig-name descname"><span class="pre">lock</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.lock" title="Link to this definition">¶</a></dt>
  461. <dd><p>Acquire an exclusive advisory lock on the mailbox so that other processes
  462. know not to modify it. An <a class="reference internal" href="#mailbox.ExternalClashError" title="mailbox.ExternalClashError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ExternalClashError</span></code></a> is raised if the lock
  463. is not available. The particular locking mechanisms used depend upon the
  464. mailbox format. You should <em>always</em> lock the mailbox before making any
  465. modifications to its contents.</p>
  466. </dd></dl>
  467. <dl class="py method">
  468. <dt class="sig sig-object py" id="mailbox.Mailbox.unlock">
  469. <span class="sig-name descname"><span class="pre">unlock</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.unlock" title="Link to this definition">¶</a></dt>
  470. <dd><p>Release the lock on the mailbox, if any.</p>
  471. </dd></dl>
  472. <dl class="py method">
  473. <dt class="sig sig-object py" id="mailbox.Mailbox.close">
  474. <span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Mailbox.close" title="Link to this definition">¶</a></dt>
  475. <dd><p>Flush the mailbox, unlock it if necessary, and close any open files. For
  476. some <code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code> subclasses, this method does nothing.</p>
  477. </dd></dl>
  478. </dd></dl>
  479. <section id="maildir-objects">
  480. <span id="mailbox-maildir"></span><h3><code class="xref py py-class docutils literal notranslate"><span class="pre">Maildir</span></code> objects<a class="headerlink" href="#maildir-objects" title="Link to this heading">¶</a></h3>
  481. <dl class="py class">
  482. <dt class="sig sig-object py" id="mailbox.Maildir">
  483. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">mailbox.</span></span><span class="sig-name descname"><span class="pre">Maildir</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">dirname</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">factory</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">create</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="#mailbox.Maildir" title="Link to this definition">¶</a></dt>
  484. <dd><p>A subclass of <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code></a> for mailboxes in Maildir format. Parameter
  485. <em>factory</em> is a callable object that accepts a file-like message representation
  486. (which behaves as if opened in binary mode) and returns a custom representation.
  487. If <em>factory</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, <a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MaildirMessage</span></code></a> is used as the default message
  488. representation. If <em>create</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, the mailbox is created if it does not
  489. exist.</p>
  490. <p>If <em>create</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> and the <em>dirname</em> path exists, it will be treated as
  491. an existing maildir without attempting to verify its directory layout.</p>
  492. <p>It is for historical reasons that <em>dirname</em> is named as such rather than <em>path</em>.</p>
  493. <p>Maildir is a directory-based mailbox format invented for the qmail mail
  494. transfer agent and now widely supported by other programs. Messages in a
  495. Maildir mailbox are stored in separate files within a common directory
  496. structure. This design allows Maildir mailboxes to be accessed and modified
  497. by multiple unrelated programs without data corruption, so file locking is
  498. unnecessary.</p>
  499. <p>Maildir mailboxes contain three subdirectories, namely: <code class="file docutils literal notranslate"><span class="pre">tmp</span></code>,
  500. <code class="file docutils literal notranslate"><span class="pre">new</span></code>, and <code class="file docutils literal notranslate"><span class="pre">cur</span></code>. Messages are created momentarily in the
  501. <code class="file docutils literal notranslate"><span class="pre">tmp</span></code> subdirectory and then moved to the <code class="file docutils literal notranslate"><span class="pre">new</span></code> subdirectory to
  502. finalize delivery. A mail user agent may subsequently move the message to the
  503. <code class="file docutils literal notranslate"><span class="pre">cur</span></code> subdirectory and store information about the state of the message
  504. in a special “info” section appended to its file name.</p>
  505. <p>Folders of the style introduced by the Courier mail transfer agent are also
  506. supported. Any subdirectory of the main mailbox is considered a folder if
  507. <code class="docutils literal notranslate"><span class="pre">'.'</span></code> is the first character in its name. Folder names are represented by
  508. <code class="xref py py-class docutils literal notranslate"><span class="pre">Maildir</span></code> without the leading <code class="docutils literal notranslate"><span class="pre">'.'</span></code>. Each folder is itself a Maildir
  509. mailbox but should not contain other folders. Instead, a logical nesting is
  510. indicated using <code class="docutils literal notranslate"><span class="pre">'.'</span></code> to delimit levels, e.g., “Archived.2005.07”.</p>
  511. <dl class="py attribute">
  512. <dt class="sig sig-object py" id="mailbox.Maildir.colon">
  513. <span class="sig-name descname"><span class="pre">colon</span></span><a class="headerlink" href="#mailbox.Maildir.colon" title="Link to this definition">¶</a></dt>
  514. <dd><p>The Maildir specification requires the use of a colon (<code class="docutils literal notranslate"><span class="pre">':'</span></code>) in certain
  515. message file names. However, some operating systems do not permit this
  516. character in file names, If you wish to use a Maildir-like format on such
  517. an operating system, you should specify another character to use
  518. instead. The exclamation point (<code class="docutils literal notranslate"><span class="pre">'!'</span></code>) is a popular choice. For
  519. example:</p>
  520. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">mailbox</span>
  521. <span class="n">mailbox</span><span class="o">.</span><span class="n">Maildir</span><span class="o">.</span><span class="n">colon</span> <span class="o">=</span> <span class="s1">&#39;!&#39;</span>
  522. </pre></div>
  523. </div>
  524. <p>The <code class="xref py py-attr docutils literal notranslate"><span class="pre">colon</span></code> attribute may also be set on a per-instance basis.</p>
  525. </dd></dl>
  526. <p><code class="xref py py-class docutils literal notranslate"><span class="pre">Maildir</span></code> instances have all of the methods of <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code></a> in
  527. addition to the following:</p>
  528. <dl class="py method">
  529. <dt class="sig sig-object py" id="mailbox.Maildir.list_folders">
  530. <span class="sig-name descname"><span class="pre">list_folders</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Maildir.list_folders" title="Link to this definition">¶</a></dt>
  531. <dd><p>Return a list of the names of all folders.</p>
  532. </dd></dl>
  533. <dl class="py method">
  534. <dt class="sig sig-object py" id="mailbox.Maildir.get_folder">
  535. <span class="sig-name descname"><span class="pre">get_folder</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">folder</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Maildir.get_folder" title="Link to this definition">¶</a></dt>
  536. <dd><p>Return a <code class="xref py py-class docutils literal notranslate"><span class="pre">Maildir</span></code> instance representing the folder whose name is
  537. <em>folder</em>. A <a class="reference internal" href="#mailbox.NoSuchMailboxError" title="mailbox.NoSuchMailboxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NoSuchMailboxError</span></code></a> exception is raised if the folder
  538. does not exist.</p>
  539. </dd></dl>
  540. <dl class="py method">
  541. <dt class="sig sig-object py" id="mailbox.Maildir.add_folder">
  542. <span class="sig-name descname"><span class="pre">add_folder</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">folder</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Maildir.add_folder" title="Link to this definition">¶</a></dt>
  543. <dd><p>Create a folder whose name is <em>folder</em> and return a <code class="xref py py-class docutils literal notranslate"><span class="pre">Maildir</span></code>
  544. instance representing it.</p>
  545. </dd></dl>
  546. <dl class="py method">
  547. <dt class="sig sig-object py" id="mailbox.Maildir.remove_folder">
  548. <span class="sig-name descname"><span class="pre">remove_folder</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">folder</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Maildir.remove_folder" title="Link to this definition">¶</a></dt>
  549. <dd><p>Delete the folder whose name is <em>folder</em>. If the folder contains any
  550. messages, a <a class="reference internal" href="#mailbox.NotEmptyError" title="mailbox.NotEmptyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotEmptyError</span></code></a> exception will be raised and the folder
  551. will not be deleted.</p>
  552. </dd></dl>
  553. <dl class="py method">
  554. <dt class="sig sig-object py" id="mailbox.Maildir.clean">
  555. <span class="sig-name descname"><span class="pre">clean</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Maildir.clean" title="Link to this definition">¶</a></dt>
  556. <dd><p>Delete temporary files from the mailbox that have not been accessed in the
  557. last 36 hours. The Maildir specification says that mail-reading programs
  558. should do this occasionally.</p>
  559. </dd></dl>
  560. <p>Some <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code></a> methods implemented by <code class="xref py py-class docutils literal notranslate"><span class="pre">Maildir</span></code> deserve special
  561. remarks:</p>
  562. <dl class="py method">
  563. <dt class="sig sig-object py" id="mailbox.Maildir.add">
  564. <span class="sig-name descname"><span class="pre">add</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">message</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Maildir.add" title="Link to this definition">¶</a></dt>
  565. <dt class="sig sig-object py" id="mailbox.Maildir.__setitem__">
  566. <span class="sig-name descname"><span class="pre">__setitem__</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">message</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Maildir.__setitem__" title="Link to this definition">¶</a></dt>
  567. <dt class="sig sig-object py" id="mailbox.Maildir.update">
  568. <span class="sig-name descname"><span class="pre">update</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">arg</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Maildir.update" title="Link to this definition">¶</a></dt>
  569. <dd><div class="admonition warning">
  570. <p class="admonition-title">Warning</p>
  571. <p>These methods generate unique file names based upon the current process
  572. ID. When using multiple threads, undetected name clashes may occur and
  573. cause corruption of the mailbox unless threads are coordinated to avoid
  574. using these methods to manipulate the same mailbox simultaneously.</p>
  575. </div>
  576. </dd></dl>
  577. <dl class="py method">
  578. <dt class="sig sig-object py" id="mailbox.Maildir.flush">
  579. <span class="sig-name descname"><span class="pre">flush</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Maildir.flush" title="Link to this definition">¶</a></dt>
  580. <dd><p>All changes to Maildir mailboxes are immediately applied, so this method
  581. does nothing.</p>
  582. </dd></dl>
  583. <dl class="py method">
  584. <dt class="sig sig-object py" id="mailbox.Maildir.lock">
  585. <span class="sig-name descname"><span class="pre">lock</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Maildir.lock" title="Link to this definition">¶</a></dt>
  586. <dt class="sig sig-object py" id="mailbox.Maildir.unlock">
  587. <span class="sig-name descname"><span class="pre">unlock</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Maildir.unlock" title="Link to this definition">¶</a></dt>
  588. <dd><p>Maildir mailboxes do not support (or require) locking, so these methods do
  589. nothing.</p>
  590. </dd></dl>
  591. <dl class="py method">
  592. <dt class="sig sig-object py" id="mailbox.Maildir.close">
  593. <span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Maildir.close" title="Link to this definition">¶</a></dt>
  594. <dd><p><code class="xref py py-class docutils literal notranslate"><span class="pre">Maildir</span></code> instances do not keep any open files and the underlying
  595. mailboxes do not support locking, so this method does nothing.</p>
  596. </dd></dl>
  597. <dl class="py method">
  598. <dt class="sig sig-object py" id="mailbox.Maildir.get_file">
  599. <span class="sig-name descname"><span class="pre">get_file</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Maildir.get_file" title="Link to this definition">¶</a></dt>
  600. <dd><p>Depending upon the host platform, it may not be possible to modify or
  601. remove the underlying message while the returned file remains open.</p>
  602. </dd></dl>
  603. </dd></dl>
  604. <div class="admonition seealso">
  605. <p class="admonition-title">See also</p>
  606. <dl class="simple">
  607. <dt><a class="reference external" href="https://www.courier-mta.org/maildir.html">maildir man page from Courier</a></dt><dd><p>A specification of the format. Describes a common extension for
  608. supporting folders.</p>
  609. </dd>
  610. <dt><a class="reference external" href="https://cr.yp.to/proto/maildir.html">Using maildir format</a></dt><dd><p>Notes on Maildir by its inventor. Includes an updated name-creation scheme and
  611. details on “info” semantics.</p>
  612. </dd>
  613. </dl>
  614. </div>
  615. </section>
  616. <section id="mbox-objects">
  617. <span id="mailbox-mbox"></span><h3><code class="xref py py-class docutils literal notranslate"><span class="pre">mbox</span></code> objects<a class="headerlink" href="#mbox-objects" title="Link to this heading">¶</a></h3>
  618. <dl class="py class">
  619. <dt class="sig sig-object py" id="mailbox.mbox">
  620. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">mailbox.</span></span><span class="sig-name descname"><span class="pre">mbox</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">factory</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">create</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="#mailbox.mbox" title="Link to this definition">¶</a></dt>
  621. <dd><p>A subclass of <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code></a> for mailboxes in mbox format. Parameter <em>factory</em>
  622. is a callable object that accepts a file-like message representation (which
  623. behaves as if opened in binary mode) and returns a custom representation. If
  624. <em>factory</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, <a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">mboxMessage</span></code></a> is used as the default message
  625. representation. If <em>create</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, the mailbox is created if it does not
  626. exist.</p>
  627. <p>The mbox format is the classic format for storing mail on Unix systems. All
  628. messages in an mbox mailbox are stored in a single file with the beginning of
  629. each message indicated by a line whose first five characters are “From “.</p>
  630. <p>Several variations of the mbox format exist to address perceived shortcomings in
  631. the original. In the interest of compatibility, <code class="xref py py-class docutils literal notranslate"><span class="pre">mbox</span></code> implements the
  632. original format, which is sometimes referred to as <em class="dfn">mboxo</em>. This means that
  633. the <em class="mailheader">Content-Length</em> header, if present, is ignored and that any
  634. occurrences of “From “ at the beginning of a line in a message body are
  635. transformed to “&gt;From “ when storing the message, although occurrences of “&gt;From
  636. “ are not transformed to “From “ when reading the message.</p>
  637. <p>Some <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code></a> methods implemented by <code class="xref py py-class docutils literal notranslate"><span class="pre">mbox</span></code> deserve special
  638. remarks:</p>
  639. <dl class="py method">
  640. <dt class="sig sig-object py" id="mailbox.mbox.get_file">
  641. <span class="sig-name descname"><span class="pre">get_file</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.mbox.get_file" title="Link to this definition">¶</a></dt>
  642. <dd><p>Using the file after calling <a class="reference internal" href="#mailbox.Mailbox.flush" title="mailbox.Mailbox.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush()</span></code></a> or
  643. <a class="reference internal" href="#mailbox.Mailbox.close" title="mailbox.Mailbox.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> on the <code class="xref py py-class docutils literal notranslate"><span class="pre">mbox</span></code> instance may yield
  644. unpredictable results or raise an exception.</p>
  645. </dd></dl>
  646. <dl class="py method">
  647. <dt class="sig sig-object py" id="mailbox.mbox.lock">
  648. <span class="sig-name descname"><span class="pre">lock</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.mbox.lock" title="Link to this definition">¶</a></dt>
  649. <dt class="sig sig-object py" id="mailbox.mbox.unlock">
  650. <span class="sig-name descname"><span class="pre">unlock</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.mbox.unlock" title="Link to this definition">¶</a></dt>
  651. <dd><p>Three locking mechanisms are used—dot locking and, if available, the
  652. <code class="xref c c-func docutils literal notranslate"><span class="pre">flock()</span></code> and <code class="xref c c-func docutils literal notranslate"><span class="pre">lockf()</span></code> system calls.</p>
  653. </dd></dl>
  654. </dd></dl>
  655. <div class="admonition seealso">
  656. <p class="admonition-title">See also</p>
  657. <dl class="simple">
  658. <dt><a class="reference external" href="http://www.tin.org/bin/man.cgi?section=5&amp;topic=mbox">mbox man page from tin</a></dt><dd><p>A specification of the format, with details on locking.</p>
  659. </dd>
  660. <dt><a class="reference external" href="https://www.jwz.org/doc/content-length.html">Configuring Netscape Mail on Unix: Why The Content-Length Format is Bad</a></dt><dd><p>An argument for using the original mbox format rather than a variation.</p>
  661. </dd>
  662. <dt><a class="reference external" href="https://www.loc.gov/preservation/digital/formats/fdd/fdd000383.shtml">“mbox” is a family of several mutually incompatible mailbox formats</a></dt><dd><p>A history of mbox variations.</p>
  663. </dd>
  664. </dl>
  665. </div>
  666. </section>
  667. <section id="mh-objects">
  668. <span id="mailbox-mh"></span><h3><code class="xref py py-class docutils literal notranslate"><span class="pre">MH</span></code> objects<a class="headerlink" href="#mh-objects" title="Link to this heading">¶</a></h3>
  669. <dl class="py class">
  670. <dt class="sig sig-object py" id="mailbox.MH">
  671. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">mailbox.</span></span><span class="sig-name descname"><span class="pre">MH</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">factory</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">create</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="#mailbox.MH" title="Link to this definition">¶</a></dt>
  672. <dd><p>A subclass of <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code></a> for mailboxes in MH format. Parameter <em>factory</em>
  673. is a callable object that accepts a file-like message representation (which
  674. behaves as if opened in binary mode) and returns a custom representation. If
  675. <em>factory</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, <a class="reference internal" href="#mailbox.MHMessage" title="mailbox.MHMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MHMessage</span></code></a> is used as the default message
  676. representation. If <em>create</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, the mailbox is created if it does not
  677. exist.</p>
  678. <p>MH is a directory-based mailbox format invented for the MH Message Handling
  679. System, a mail user agent. Each message in an MH mailbox resides in its own
  680. file. An MH mailbox may contain other MH mailboxes (called <em class="dfn">folders</em>) in
  681. addition to messages. Folders may be nested indefinitely. MH mailboxes also
  682. support <em class="dfn">sequences</em>, which are named lists used to logically group
  683. messages without moving them to sub-folders. Sequences are defined in a file
  684. called <code class="file docutils literal notranslate"><span class="pre">.mh_sequences</span></code> in each folder.</p>
  685. <p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">MH</span></code> class manipulates MH mailboxes, but it does not attempt to
  686. emulate all of <strong class="program">mh</strong>’s behaviors. In particular, it does not modify
  687. and is not affected by the <code class="file docutils literal notranslate"><span class="pre">context</span></code> or <code class="file docutils literal notranslate"><span class="pre">.mh_profile</span></code> files that
  688. are used by <strong class="program">mh</strong> to store its state and configuration.</p>
  689. <p><code class="xref py py-class docutils literal notranslate"><span class="pre">MH</span></code> instances have all of the methods of <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code></a> in addition
  690. to the following:</p>
  691. <dl class="py method">
  692. <dt class="sig sig-object py" id="mailbox.MH.list_folders">
  693. <span class="sig-name descname"><span class="pre">list_folders</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MH.list_folders" title="Link to this definition">¶</a></dt>
  694. <dd><p>Return a list of the names of all folders.</p>
  695. </dd></dl>
  696. <dl class="py method">
  697. <dt class="sig sig-object py" id="mailbox.MH.get_folder">
  698. <span class="sig-name descname"><span class="pre">get_folder</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">folder</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MH.get_folder" title="Link to this definition">¶</a></dt>
  699. <dd><p>Return an <code class="xref py py-class docutils literal notranslate"><span class="pre">MH</span></code> instance representing the folder whose name is
  700. <em>folder</em>. A <a class="reference internal" href="#mailbox.NoSuchMailboxError" title="mailbox.NoSuchMailboxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NoSuchMailboxError</span></code></a> exception is raised if the folder
  701. does not exist.</p>
  702. </dd></dl>
  703. <dl class="py method">
  704. <dt class="sig sig-object py" id="mailbox.MH.add_folder">
  705. <span class="sig-name descname"><span class="pre">add_folder</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">folder</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MH.add_folder" title="Link to this definition">¶</a></dt>
  706. <dd><p>Create a folder whose name is <em>folder</em> and return an <code class="xref py py-class docutils literal notranslate"><span class="pre">MH</span></code> instance
  707. representing it.</p>
  708. </dd></dl>
  709. <dl class="py method">
  710. <dt class="sig sig-object py" id="mailbox.MH.remove_folder">
  711. <span class="sig-name descname"><span class="pre">remove_folder</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">folder</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MH.remove_folder" title="Link to this definition">¶</a></dt>
  712. <dd><p>Delete the folder whose name is <em>folder</em>. If the folder contains any
  713. messages, a <a class="reference internal" href="#mailbox.NotEmptyError" title="mailbox.NotEmptyError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NotEmptyError</span></code></a> exception will be raised and the folder
  714. will not be deleted.</p>
  715. </dd></dl>
  716. <dl class="py method">
  717. <dt class="sig sig-object py" id="mailbox.MH.get_sequences">
  718. <span class="sig-name descname"><span class="pre">get_sequences</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MH.get_sequences" title="Link to this definition">¶</a></dt>
  719. <dd><p>Return a dictionary of sequence names mapped to key lists. If there are no
  720. sequences, the empty dictionary is returned.</p>
  721. </dd></dl>
  722. <dl class="py method">
  723. <dt class="sig sig-object py" id="mailbox.MH.set_sequences">
  724. <span class="sig-name descname"><span class="pre">set_sequences</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sequences</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MH.set_sequences" title="Link to this definition">¶</a></dt>
  725. <dd><p>Re-define the sequences that exist in the mailbox based upon <em>sequences</em>,
  726. a dictionary of names mapped to key lists, like returned by
  727. <a class="reference internal" href="#mailbox.MH.get_sequences" title="mailbox.MH.get_sequences"><code class="xref py py-meth docutils literal notranslate"><span class="pre">get_sequences()</span></code></a>.</p>
  728. </dd></dl>
  729. <dl class="py method">
  730. <dt class="sig sig-object py" id="mailbox.MH.pack">
  731. <span class="sig-name descname"><span class="pre">pack</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MH.pack" title="Link to this definition">¶</a></dt>
  732. <dd><p>Rename messages in the mailbox as necessary to eliminate gaps in
  733. numbering. Entries in the sequences list are updated correspondingly.</p>
  734. <div class="admonition note">
  735. <p class="admonition-title">Note</p>
  736. <p>Already-issued keys are invalidated by this operation and should not be
  737. subsequently used.</p>
  738. </div>
  739. </dd></dl>
  740. <p>Some <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code></a> methods implemented by <code class="xref py py-class docutils literal notranslate"><span class="pre">MH</span></code> deserve special
  741. remarks:</p>
  742. <dl class="py method">
  743. <dt class="sig sig-object py" id="mailbox.MH.remove">
  744. <span class="sig-name descname"><span class="pre">remove</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MH.remove" title="Link to this definition">¶</a></dt>
  745. <dt class="sig sig-object py" id="mailbox.MH.__delitem__">
  746. <span class="sig-name descname"><span class="pre">__delitem__</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MH.__delitem__" title="Link to this definition">¶</a></dt>
  747. <dt class="sig sig-object py" id="mailbox.MH.discard">
  748. <span class="sig-name descname"><span class="pre">discard</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MH.discard" title="Link to this definition">¶</a></dt>
  749. <dd><p>These methods immediately delete the message. The MH convention of marking
  750. a message for deletion by prepending a comma to its name is not used.</p>
  751. </dd></dl>
  752. <dl class="py method">
  753. <dt class="sig sig-object py" id="mailbox.MH.lock">
  754. <span class="sig-name descname"><span class="pre">lock</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MH.lock" title="Link to this definition">¶</a></dt>
  755. <dt class="sig sig-object py" id="mailbox.MH.unlock">
  756. <span class="sig-name descname"><span class="pre">unlock</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MH.unlock" title="Link to this definition">¶</a></dt>
  757. <dd><p>Three locking mechanisms are used—dot locking and, if available, the
  758. <code class="xref c c-func docutils literal notranslate"><span class="pre">flock()</span></code> and <code class="xref c c-func docutils literal notranslate"><span class="pre">lockf()</span></code> system calls. For MH mailboxes, locking
  759. the mailbox means locking the <code class="file docutils literal notranslate"><span class="pre">.mh_sequences</span></code> file and, only for the
  760. duration of any operations that affect them, locking individual message
  761. files.</p>
  762. </dd></dl>
  763. <dl class="py method">
  764. <dt class="sig sig-object py" id="mailbox.MH.get_file">
  765. <span class="sig-name descname"><span class="pre">get_file</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MH.get_file" title="Link to this definition">¶</a></dt>
  766. <dd><p>Depending upon the host platform, it may not be possible to remove the
  767. underlying message while the returned file remains open.</p>
  768. </dd></dl>
  769. <dl class="py method">
  770. <dt class="sig sig-object py" id="mailbox.MH.flush">
  771. <span class="sig-name descname"><span class="pre">flush</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MH.flush" title="Link to this definition">¶</a></dt>
  772. <dd><p>All changes to MH mailboxes are immediately applied, so this method does
  773. nothing.</p>
  774. </dd></dl>
  775. <dl class="py method">
  776. <dt class="sig sig-object py" id="mailbox.MH.close">
  777. <span class="sig-name descname"><span class="pre">close</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MH.close" title="Link to this definition">¶</a></dt>
  778. <dd><p><code class="xref py py-class docutils literal notranslate"><span class="pre">MH</span></code> instances do not keep any open files, so this method is
  779. equivalent to <a class="reference internal" href="#mailbox.MH.unlock" title="mailbox.MH.unlock"><code class="xref py py-meth docutils literal notranslate"><span class="pre">unlock()</span></code></a>.</p>
  780. </dd></dl>
  781. </dd></dl>
  782. <div class="admonition seealso">
  783. <p class="admonition-title">See also</p>
  784. <dl class="simple">
  785. <dt><a class="reference external" href="https://www.nongnu.org/nmh/">nmh - Message Handling System</a></dt><dd><p>Home page of <strong class="program">nmh</strong>, an updated version of the original <strong class="program">mh</strong>.</p>
  786. </dd>
  787. <dt><a class="reference external" href="https://rand-mh.sourceforge.io/book/">MH &amp; nmh: Email for Users &amp; Programmers</a></dt><dd><p>A GPL-licensed book on <strong class="program">mh</strong> and <strong class="program">nmh</strong>, with some information
  788. on the mailbox format.</p>
  789. </dd>
  790. </dl>
  791. </div>
  792. </section>
  793. <section id="babyl-objects">
  794. <span id="mailbox-babyl"></span><h3><code class="xref py py-class docutils literal notranslate"><span class="pre">Babyl</span></code> objects<a class="headerlink" href="#babyl-objects" title="Link to this heading">¶</a></h3>
  795. <dl class="py class">
  796. <dt class="sig sig-object py" id="mailbox.Babyl">
  797. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">mailbox.</span></span><span class="sig-name descname"><span class="pre">Babyl</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">factory</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">create</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="#mailbox.Babyl" title="Link to this definition">¶</a></dt>
  798. <dd><p>A subclass of <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code></a> for mailboxes in Babyl format. Parameter
  799. <em>factory</em> is a callable object that accepts a file-like message representation
  800. (which behaves as if opened in binary mode) and returns a custom representation.
  801. If <em>factory</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, <a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">BabylMessage</span></code></a> is used as the default message
  802. representation. If <em>create</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, the mailbox is created if it does not
  803. exist.</p>
  804. <p>Babyl is a single-file mailbox format used by the Rmail mail user agent
  805. included with Emacs. The beginning of a message is indicated by a line
  806. containing the two characters Control-Underscore (<code class="docutils literal notranslate"><span class="pre">'\037'</span></code>) and Control-L
  807. (<code class="docutils literal notranslate"><span class="pre">'\014'</span></code>). The end of a message is indicated by the start of the next
  808. message or, in the case of the last message, a line containing a
  809. Control-Underscore (<code class="docutils literal notranslate"><span class="pre">'\037'</span></code>) character.</p>
  810. <p>Messages in a Babyl mailbox have two sets of headers, original headers and
  811. so-called visible headers. Visible headers are typically a subset of the
  812. original headers that have been reformatted or abridged to be more
  813. attractive. Each message in a Babyl mailbox also has an accompanying list of
  814. <em class="dfn">labels</em>, or short strings that record extra information about the
  815. message, and a list of all user-defined labels found in the mailbox is kept
  816. in the Babyl options section.</p>
  817. <p><code class="xref py py-class docutils literal notranslate"><span class="pre">Babyl</span></code> instances have all of the methods of <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code></a> in
  818. addition to the following:</p>
  819. <dl class="py method">
  820. <dt class="sig sig-object py" id="mailbox.Babyl.get_labels">
  821. <span class="sig-name descname"><span class="pre">get_labels</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Babyl.get_labels" title="Link to this definition">¶</a></dt>
  822. <dd><p>Return a list of the names of all user-defined labels used in the mailbox.</p>
  823. <div class="admonition note">
  824. <p class="admonition-title">Note</p>
  825. <p>The actual messages are inspected to determine which labels exist in
  826. the mailbox rather than consulting the list of labels in the Babyl
  827. options section, but the Babyl section is updated whenever the mailbox
  828. is modified.</p>
  829. </div>
  830. </dd></dl>
  831. <p>Some <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code></a> methods implemented by <code class="xref py py-class docutils literal notranslate"><span class="pre">Babyl</span></code> deserve special
  832. remarks:</p>
  833. <dl class="py method">
  834. <dt class="sig sig-object py" id="mailbox.Babyl.get_file">
  835. <span class="sig-name descname"><span class="pre">get_file</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Babyl.get_file" title="Link to this definition">¶</a></dt>
  836. <dd><p>In Babyl mailboxes, the headers of a message are not stored contiguously
  837. with the body of the message. To generate a file-like representation, the
  838. headers and body are copied together into an <a class="reference internal" href="io.html#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BytesIO</span></code></a> instance,
  839. which has an API identical to that of a
  840. file. As a result, the file-like object is truly independent of the
  841. underlying mailbox but does not save memory compared to a string
  842. representation.</p>
  843. </dd></dl>
  844. <dl class="py method">
  845. <dt class="sig sig-object py" id="mailbox.Babyl.lock">
  846. <span class="sig-name descname"><span class="pre">lock</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Babyl.lock" title="Link to this definition">¶</a></dt>
  847. <dt class="sig sig-object py" id="mailbox.Babyl.unlock">
  848. <span class="sig-name descname"><span class="pre">unlock</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.Babyl.unlock" title="Link to this definition">¶</a></dt>
  849. <dd><p>Three locking mechanisms are used—dot locking and, if available, the
  850. <code class="xref c c-func docutils literal notranslate"><span class="pre">flock()</span></code> and <code class="xref c c-func docutils literal notranslate"><span class="pre">lockf()</span></code> system calls.</p>
  851. </dd></dl>
  852. </dd></dl>
  853. <div class="admonition seealso">
  854. <p class="admonition-title">See also</p>
  855. <dl class="simple">
  856. <dt><a class="reference external" href="https://quimby.gnus.org/notes/BABYL">Format of Version 5 Babyl Files</a></dt><dd><p>A specification of the Babyl format.</p>
  857. </dd>
  858. <dt><a class="reference external" href="https://www.gnu.org/software/emacs/manual/html_node/emacs/Rmail.html">Reading Mail with Rmail</a></dt><dd><p>The Rmail manual, with some information on Babyl semantics.</p>
  859. </dd>
  860. </dl>
  861. </div>
  862. </section>
  863. <section id="mmdf-objects">
  864. <span id="mailbox-mmdf"></span><h3><code class="xref py py-class docutils literal notranslate"><span class="pre">MMDF</span></code> objects<a class="headerlink" href="#mmdf-objects" title="Link to this heading">¶</a></h3>
  865. <dl class="py class">
  866. <dt class="sig sig-object py" id="mailbox.MMDF">
  867. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">mailbox.</span></span><span class="sig-name descname"><span class="pre">MMDF</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">factory</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">create</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="#mailbox.MMDF" title="Link to this definition">¶</a></dt>
  868. <dd><p>A subclass of <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code></a> for mailboxes in MMDF format. Parameter <em>factory</em>
  869. is a callable object that accepts a file-like message representation (which
  870. behaves as if opened in binary mode) and returns a custom representation. If
  871. <em>factory</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, <a class="reference internal" href="#mailbox.MMDFMessage" title="mailbox.MMDFMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MMDFMessage</span></code></a> is used as the default message
  872. representation. If <em>create</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, the mailbox is created if it does not
  873. exist.</p>
  874. <p>MMDF is a single-file mailbox format invented for the Multichannel Memorandum
  875. Distribution Facility, a mail transfer agent. Each message is in the same
  876. form as an mbox message but is bracketed before and after by lines containing
  877. four Control-A (<code class="docutils literal notranslate"><span class="pre">'\001'</span></code>) characters. As with the mbox format, the
  878. beginning of each message is indicated by a line whose first five characters
  879. are “From “, but additional occurrences of “From “ are not transformed to
  880. “&gt;From “ when storing messages because the extra message separator lines
  881. prevent mistaking such occurrences for the starts of subsequent messages.</p>
  882. <p>Some <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code></a> methods implemented by <code class="xref py py-class docutils literal notranslate"><span class="pre">MMDF</span></code> deserve special
  883. remarks:</p>
  884. <dl class="py method">
  885. <dt class="sig sig-object py" id="mailbox.MMDF.get_file">
  886. <span class="sig-name descname"><span class="pre">get_file</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">key</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MMDF.get_file" title="Link to this definition">¶</a></dt>
  887. <dd><p>Using the file after calling <a class="reference internal" href="#mailbox.Mailbox.flush" title="mailbox.Mailbox.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush()</span></code></a> or
  888. <a class="reference internal" href="#mailbox.Mailbox.close" title="mailbox.Mailbox.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> on the <code class="xref py py-class docutils literal notranslate"><span class="pre">MMDF</span></code> instance may yield
  889. unpredictable results or raise an exception.</p>
  890. </dd></dl>
  891. <dl class="py method">
  892. <dt class="sig sig-object py" id="mailbox.MMDF.lock">
  893. <span class="sig-name descname"><span class="pre">lock</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MMDF.lock" title="Link to this definition">¶</a></dt>
  894. <dt class="sig sig-object py" id="mailbox.MMDF.unlock">
  895. <span class="sig-name descname"><span class="pre">unlock</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MMDF.unlock" title="Link to this definition">¶</a></dt>
  896. <dd><p>Three locking mechanisms are used—dot locking and, if available, the
  897. <code class="xref c c-func docutils literal notranslate"><span class="pre">flock()</span></code> and <code class="xref c c-func docutils literal notranslate"><span class="pre">lockf()</span></code> system calls.</p>
  898. </dd></dl>
  899. </dd></dl>
  900. <div class="admonition seealso">
  901. <p class="admonition-title">See also</p>
  902. <dl class="simple">
  903. <dt><a class="reference external" href="http://www.tin.org/bin/man.cgi?section=5&amp;topic=mmdf">mmdf man page from tin</a></dt><dd><p>A specification of MMDF format from the documentation of tin, a newsreader.</p>
  904. </dd>
  905. <dt><a class="reference external" href="https://en.wikipedia.org/wiki/MMDF">MMDF</a></dt><dd><p>A Wikipedia article describing the Multichannel Memorandum Distribution
  906. Facility.</p>
  907. </dd>
  908. </dl>
  909. </div>
  910. </section>
  911. </section>
  912. <section id="message-objects">
  913. <span id="mailbox-message-objects"></span><h2><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code> objects<a class="headerlink" href="#message-objects" title="Link to this heading">¶</a></h2>
  914. <dl class="py class">
  915. <dt class="sig sig-object py" id="mailbox.Message">
  916. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">mailbox.</span></span><span class="sig-name descname"><span class="pre">Message</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">message</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="#mailbox.Message" title="Link to this definition">¶</a></dt>
  917. <dd><p>A subclass of the <a class="reference internal" href="email.message.html#module-email.message" title="email.message: The base class representing email messages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.message</span></code></a> module’s
  918. <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>. Subclasses of <code class="xref py py-class docutils literal notranslate"><span class="pre">mailbox.Message</span></code> add
  919. mailbox-format-specific state and behavior.</p>
  920. <p>If <em>message</em> is omitted, the new instance is created in a default, empty state.
  921. If <em>message</em> is an <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">email.message.Message</span></code></a> instance, its contents are
  922. copied; furthermore, any format-specific information is converted insofar as
  923. possible if <em>message</em> is a <code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code> instance. If <em>message</em> is a string,
  924. a byte string,
  925. or a file, it should contain an <span class="target" id="index-0"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2822.html"><strong>RFC 2822</strong></a>-compliant message, which is read
  926. and parsed. Files should be open in binary mode, but text mode files
  927. are accepted for backward compatibility.</p>
  928. <p>The format-specific state and behaviors offered by subclasses vary, but in
  929. general it is only the properties that are not specific to a particular
  930. mailbox that are supported (although presumably the properties are specific
  931. to a particular mailbox format). For example, file offsets for single-file
  932. mailbox formats and file names for directory-based mailbox formats are not
  933. retained, because they are only applicable to the original mailbox. But state
  934. such as whether a message has been read by the user or marked as important is
  935. retained, because it applies to the message itself.</p>
  936. <p>There is no requirement that <code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code> instances be used to represent
  937. messages retrieved using <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code></a> instances. In some situations, the
  938. time and memory required to generate <code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code> representations might
  939. not be acceptable. For such situations, <code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code> instances also
  940. offer string and file-like representations, and a custom message factory may
  941. be specified when a <code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code> instance is initialized.</p>
  942. </dd></dl>
  943. <section id="maildirmessage-objects">
  944. <span id="mailbox-maildirmessage"></span><h3><code class="xref py py-class docutils literal notranslate"><span class="pre">MaildirMessage</span></code> objects<a class="headerlink" href="#maildirmessage-objects" title="Link to this heading">¶</a></h3>
  945. <dl class="py class">
  946. <dt class="sig sig-object py" id="mailbox.MaildirMessage">
  947. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">mailbox.</span></span><span class="sig-name descname"><span class="pre">MaildirMessage</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">message</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="#mailbox.MaildirMessage" title="Link to this definition">¶</a></dt>
  948. <dd><p>A message with Maildir-specific behaviors. Parameter <em>message</em> has the same
  949. meaning as with the <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> constructor.</p>
  950. <p>Typically, a mail user agent application moves all of the messages in the
  951. <code class="file docutils literal notranslate"><span class="pre">new</span></code> subdirectory to the <code class="file docutils literal notranslate"><span class="pre">cur</span></code> subdirectory after the first time
  952. the user opens and closes the mailbox, recording that the messages are old
  953. whether or not they’ve actually been read. Each message in <code class="file docutils literal notranslate"><span class="pre">cur</span></code> has an
  954. “info” section added to its file name to store information about its state.
  955. (Some mail readers may also add an “info” section to messages in
  956. <code class="file docutils literal notranslate"><span class="pre">new</span></code>.) The “info” section may take one of two forms: it may contain
  957. “2,” followed by a list of standardized flags (e.g., “2,FR”) or it may
  958. contain “1,” followed by so-called experimental information. Standard flags
  959. for Maildir messages are as follows:</p>
  960. <table class="docutils align-default">
  961. <thead>
  962. <tr class="row-odd"><th class="head"><p>Flag</p></th>
  963. <th class="head"><p>Meaning</p></th>
  964. <th class="head"><p>Explanation</p></th>
  965. </tr>
  966. </thead>
  967. <tbody>
  968. <tr class="row-even"><td><p>D</p></td>
  969. <td><p>Draft</p></td>
  970. <td><p>Under composition</p></td>
  971. </tr>
  972. <tr class="row-odd"><td><p>F</p></td>
  973. <td><p>Flagged</p></td>
  974. <td><p>Marked as important</p></td>
  975. </tr>
  976. <tr class="row-even"><td><p>P</p></td>
  977. <td><p>Passed</p></td>
  978. <td><p>Forwarded, resent, or bounced</p></td>
  979. </tr>
  980. <tr class="row-odd"><td><p>R</p></td>
  981. <td><p>Replied</p></td>
  982. <td><p>Replied to</p></td>
  983. </tr>
  984. <tr class="row-even"><td><p>S</p></td>
  985. <td><p>Seen</p></td>
  986. <td><p>Read</p></td>
  987. </tr>
  988. <tr class="row-odd"><td><p>T</p></td>
  989. <td><p>Trashed</p></td>
  990. <td><p>Marked for subsequent deletion</p></td>
  991. </tr>
  992. </tbody>
  993. </table>
  994. <p><code class="xref py py-class docutils literal notranslate"><span class="pre">MaildirMessage</span></code> instances offer the following methods:</p>
  995. <dl class="py method">
  996. <dt class="sig sig-object py" id="mailbox.MaildirMessage.get_subdir">
  997. <span class="sig-name descname"><span class="pre">get_subdir</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MaildirMessage.get_subdir" title="Link to this definition">¶</a></dt>
  998. <dd><p>Return either “new” (if the message should be stored in the <code class="file docutils literal notranslate"><span class="pre">new</span></code>
  999. subdirectory) or “cur” (if the message should be stored in the <code class="file docutils literal notranslate"><span class="pre">cur</span></code>
  1000. subdirectory).</p>
  1001. <div class="admonition note">
  1002. <p class="admonition-title">Note</p>
  1003. <p>A message is typically moved from <code class="file docutils literal notranslate"><span class="pre">new</span></code> to <code class="file docutils literal notranslate"><span class="pre">cur</span></code> after its
  1004. mailbox has been accessed, whether or not the message is has been
  1005. read. A message <code class="docutils literal notranslate"><span class="pre">msg</span></code> has been read if <code class="docutils literal notranslate"><span class="pre">&quot;S&quot;</span> <span class="pre">in</span> <span class="pre">msg.get_flags()</span></code> is
  1006. <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
  1007. </div>
  1008. </dd></dl>
  1009. <dl class="py method">
  1010. <dt class="sig sig-object py" id="mailbox.MaildirMessage.set_subdir">
  1011. <span class="sig-name descname"><span class="pre">set_subdir</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">subdir</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MaildirMessage.set_subdir" title="Link to this definition">¶</a></dt>
  1012. <dd><p>Set the subdirectory the message should be stored in. Parameter <em>subdir</em>
  1013. must be either “new” or “cur”.</p>
  1014. </dd></dl>
  1015. <dl class="py method">
  1016. <dt class="sig sig-object py" id="mailbox.MaildirMessage.get_flags">
  1017. <span class="sig-name descname"><span class="pre">get_flags</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MaildirMessage.get_flags" title="Link to this definition">¶</a></dt>
  1018. <dd><p>Return a string specifying the flags that are currently set. If the
  1019. message complies with the standard Maildir format, the result is the
  1020. concatenation in alphabetical order of zero or one occurrence of each of
  1021. <code class="docutils literal notranslate"><span class="pre">'D'</span></code>, <code class="docutils literal notranslate"><span class="pre">'F'</span></code>, <code class="docutils literal notranslate"><span class="pre">'P'</span></code>, <code class="docutils literal notranslate"><span class="pre">'R'</span></code>, <code class="docutils literal notranslate"><span class="pre">'S'</span></code>, and <code class="docutils literal notranslate"><span class="pre">'T'</span></code>. The empty string
  1022. is returned if no flags are set or if “info” contains experimental
  1023. semantics.</p>
  1024. </dd></dl>
  1025. <dl class="py method">
  1026. <dt class="sig sig-object py" id="mailbox.MaildirMessage.set_flags">
  1027. <span class="sig-name descname"><span class="pre">set_flags</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">flags</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MaildirMessage.set_flags" title="Link to this definition">¶</a></dt>
  1028. <dd><p>Set the flags specified by <em>flags</em> and unset all others.</p>
  1029. </dd></dl>
  1030. <dl class="py method">
  1031. <dt class="sig sig-object py" id="mailbox.MaildirMessage.add_flag">
  1032. <span class="sig-name descname"><span class="pre">add_flag</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">flag</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MaildirMessage.add_flag" title="Link to this definition">¶</a></dt>
  1033. <dd><p>Set the flag(s) specified by <em>flag</em> without changing other flags. To add
  1034. more than one flag at a time, <em>flag</em> may be a string of more than one
  1035. character. The current “info” is overwritten whether or not it contains
  1036. experimental information rather than flags.</p>
  1037. </dd></dl>
  1038. <dl class="py method">
  1039. <dt class="sig sig-object py" id="mailbox.MaildirMessage.remove_flag">
  1040. <span class="sig-name descname"><span class="pre">remove_flag</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">flag</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MaildirMessage.remove_flag" title="Link to this definition">¶</a></dt>
  1041. <dd><p>Unset the flag(s) specified by <em>flag</em> without changing other flags. To
  1042. remove more than one flag at a time, <em>flag</em> maybe a string of more than
  1043. one character. If “info” contains experimental information rather than
  1044. flags, the current “info” is not modified.</p>
  1045. </dd></dl>
  1046. <dl class="py method">
  1047. <dt class="sig sig-object py" id="mailbox.MaildirMessage.get_date">
  1048. <span class="sig-name descname"><span class="pre">get_date</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MaildirMessage.get_date" title="Link to this definition">¶</a></dt>
  1049. <dd><p>Return the delivery date of the message as a floating-point number
  1050. representing seconds since the epoch.</p>
  1051. </dd></dl>
  1052. <dl class="py method">
  1053. <dt class="sig sig-object py" id="mailbox.MaildirMessage.set_date">
  1054. <span class="sig-name descname"><span class="pre">set_date</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">date</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MaildirMessage.set_date" title="Link to this definition">¶</a></dt>
  1055. <dd><p>Set the delivery date of the message to <em>date</em>, a floating-point number
  1056. representing seconds since the epoch.</p>
  1057. </dd></dl>
  1058. <dl class="py method">
  1059. <dt class="sig sig-object py" id="mailbox.MaildirMessage.get_info">
  1060. <span class="sig-name descname"><span class="pre">get_info</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MaildirMessage.get_info" title="Link to this definition">¶</a></dt>
  1061. <dd><p>Return a string containing the “info” for a message. This is useful for
  1062. accessing and modifying “info” that is experimental (i.e., not a list of
  1063. flags).</p>
  1064. </dd></dl>
  1065. <dl class="py method">
  1066. <dt class="sig sig-object py" id="mailbox.MaildirMessage.set_info">
  1067. <span class="sig-name descname"><span class="pre">set_info</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">info</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MaildirMessage.set_info" title="Link to this definition">¶</a></dt>
  1068. <dd><p>Set “info” to <em>info</em>, which should be a string.</p>
  1069. </dd></dl>
  1070. </dd></dl>
  1071. <p>When a <code class="xref py py-class docutils literal notranslate"><span class="pre">MaildirMessage</span></code> instance is created based upon an
  1072. <a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">mboxMessage</span></code></a> or <a class="reference internal" href="#mailbox.MMDFMessage" title="mailbox.MMDFMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MMDFMessage</span></code></a> instance, the <em class="mailheader">Status</em>
  1073. and <em class="mailheader">X-Status</em> headers are omitted and the following conversions
  1074. take place:</p>
  1075. <table class="docutils align-default">
  1076. <thead>
  1077. <tr class="row-odd"><th class="head"><p>Resulting state</p></th>
  1078. <th class="head"><p><a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">mboxMessage</span></code></a> or <a class="reference internal" href="#mailbox.MMDFMessage" title="mailbox.MMDFMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MMDFMessage</span></code></a>
  1079. state</p></th>
  1080. </tr>
  1081. </thead>
  1082. <tbody>
  1083. <tr class="row-even"><td><p>“cur” subdirectory</p></td>
  1084. <td><p>O flag</p></td>
  1085. </tr>
  1086. <tr class="row-odd"><td><p>F flag</p></td>
  1087. <td><p>F flag</p></td>
  1088. </tr>
  1089. <tr class="row-even"><td><p>R flag</p></td>
  1090. <td><p>A flag</p></td>
  1091. </tr>
  1092. <tr class="row-odd"><td><p>S flag</p></td>
  1093. <td><p>R flag</p></td>
  1094. </tr>
  1095. <tr class="row-even"><td><p>T flag</p></td>
  1096. <td><p>D flag</p></td>
  1097. </tr>
  1098. </tbody>
  1099. </table>
  1100. <p>When a <code class="xref py py-class docutils literal notranslate"><span class="pre">MaildirMessage</span></code> instance is created based upon an
  1101. <a class="reference internal" href="#mailbox.MHMessage" title="mailbox.MHMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MHMessage</span></code></a> instance, the following conversions take place:</p>
  1102. <table class="docutils align-default">
  1103. <thead>
  1104. <tr class="row-odd"><th class="head"><p>Resulting state</p></th>
  1105. <th class="head"><p><a class="reference internal" href="#mailbox.MHMessage" title="mailbox.MHMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MHMessage</span></code></a> state</p></th>
  1106. </tr>
  1107. </thead>
  1108. <tbody>
  1109. <tr class="row-even"><td><p>“cur” subdirectory</p></td>
  1110. <td><p>“unseen” sequence</p></td>
  1111. </tr>
  1112. <tr class="row-odd"><td><p>“cur” subdirectory and S flag</p></td>
  1113. <td><p>no “unseen” sequence</p></td>
  1114. </tr>
  1115. <tr class="row-even"><td><p>F flag</p></td>
  1116. <td><p>“flagged” sequence</p></td>
  1117. </tr>
  1118. <tr class="row-odd"><td><p>R flag</p></td>
  1119. <td><p>“replied” sequence</p></td>
  1120. </tr>
  1121. </tbody>
  1122. </table>
  1123. <p>When a <code class="xref py py-class docutils literal notranslate"><span class="pre">MaildirMessage</span></code> instance is created based upon a
  1124. <a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">BabylMessage</span></code></a> instance, the following conversions take place:</p>
  1125. <table class="docutils align-default">
  1126. <thead>
  1127. <tr class="row-odd"><th class="head"><p>Resulting state</p></th>
  1128. <th class="head"><p><a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">BabylMessage</span></code></a> state</p></th>
  1129. </tr>
  1130. </thead>
  1131. <tbody>
  1132. <tr class="row-even"><td><p>“cur” subdirectory</p></td>
  1133. <td><p>“unseen” label</p></td>
  1134. </tr>
  1135. <tr class="row-odd"><td><p>“cur” subdirectory and S flag</p></td>
  1136. <td><p>no “unseen” label</p></td>
  1137. </tr>
  1138. <tr class="row-even"><td><p>P flag</p></td>
  1139. <td><p>“forwarded” or “resent” label</p></td>
  1140. </tr>
  1141. <tr class="row-odd"><td><p>R flag</p></td>
  1142. <td><p>“answered” label</p></td>
  1143. </tr>
  1144. <tr class="row-even"><td><p>T flag</p></td>
  1145. <td><p>“deleted” label</p></td>
  1146. </tr>
  1147. </tbody>
  1148. </table>
  1149. </section>
  1150. <section id="mboxmessage-objects">
  1151. <span id="mailbox-mboxmessage"></span><h3><code class="xref py py-class docutils literal notranslate"><span class="pre">mboxMessage</span></code> objects<a class="headerlink" href="#mboxmessage-objects" title="Link to this heading">¶</a></h3>
  1152. <dl class="py class">
  1153. <dt class="sig sig-object py" id="mailbox.mboxMessage">
  1154. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">mailbox.</span></span><span class="sig-name descname"><span class="pre">mboxMessage</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">message</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="#mailbox.mboxMessage" title="Link to this definition">¶</a></dt>
  1155. <dd><p>A message with mbox-specific behaviors. Parameter <em>message</em> has the same meaning
  1156. as with the <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> constructor.</p>
  1157. <p>Messages in an mbox mailbox are stored together in a single file. The
  1158. sender’s envelope address and the time of delivery are typically stored in a
  1159. line beginning with “From “ that is used to indicate the start of a message,
  1160. though there is considerable variation in the exact format of this data among
  1161. mbox implementations. Flags that indicate the state of the message, such as
  1162. whether it has been read or marked as important, are typically stored in
  1163. <em class="mailheader">Status</em> and <em class="mailheader">X-Status</em> headers.</p>
  1164. <p>Conventional flags for mbox messages are as follows:</p>
  1165. <table class="docutils align-default">
  1166. <thead>
  1167. <tr class="row-odd"><th class="head"><p>Flag</p></th>
  1168. <th class="head"><p>Meaning</p></th>
  1169. <th class="head"><p>Explanation</p></th>
  1170. </tr>
  1171. </thead>
  1172. <tbody>
  1173. <tr class="row-even"><td><p>R</p></td>
  1174. <td><p>Read</p></td>
  1175. <td><p>Read</p></td>
  1176. </tr>
  1177. <tr class="row-odd"><td><p>O</p></td>
  1178. <td><p>Old</p></td>
  1179. <td><p>Previously detected by MUA</p></td>
  1180. </tr>
  1181. <tr class="row-even"><td><p>D</p></td>
  1182. <td><p>Deleted</p></td>
  1183. <td><p>Marked for subsequent deletion</p></td>
  1184. </tr>
  1185. <tr class="row-odd"><td><p>F</p></td>
  1186. <td><p>Flagged</p></td>
  1187. <td><p>Marked as important</p></td>
  1188. </tr>
  1189. <tr class="row-even"><td><p>A</p></td>
  1190. <td><p>Answered</p></td>
  1191. <td><p>Replied to</p></td>
  1192. </tr>
  1193. </tbody>
  1194. </table>
  1195. <p>The “R” and “O” flags are stored in the <em class="mailheader">Status</em> header, and the
  1196. “D”, “F”, and “A” flags are stored in the <em class="mailheader">X-Status</em> header. The
  1197. flags and headers typically appear in the order mentioned.</p>
  1198. <p><code class="xref py py-class docutils literal notranslate"><span class="pre">mboxMessage</span></code> instances offer the following methods:</p>
  1199. <dl class="py method">
  1200. <dt class="sig sig-object py" id="mailbox.mboxMessage.get_from">
  1201. <span class="sig-name descname"><span class="pre">get_from</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.mboxMessage.get_from" title="Link to this definition">¶</a></dt>
  1202. <dd><p>Return a string representing the “From “ line that marks the start of the
  1203. message in an mbox mailbox. The leading “From “ and the trailing newline
  1204. are excluded.</p>
  1205. </dd></dl>
  1206. <dl class="py method">
  1207. <dt class="sig sig-object py" id="mailbox.mboxMessage.set_from">
  1208. <span class="sig-name descname"><span class="pre">set_from</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">from_</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">time_</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="#mailbox.mboxMessage.set_from" title="Link to this definition">¶</a></dt>
  1209. <dd><p>Set the “From “ line to <em>from_</em>, which should be specified without a
  1210. leading “From “ or trailing newline. For convenience, <em>time_</em> may be
  1211. specified and will be formatted appropriately and appended to <em>from_</em>. If
  1212. <em>time_</em> is specified, it should be a <a class="reference internal" href="time.html#time.struct_time" title="time.struct_time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time.struct_time</span></code></a> instance, a
  1213. tuple suitable for passing to <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a>, or <code class="docutils literal notranslate"><span class="pre">True</span></code> (to use
  1214. <a class="reference internal" href="time.html#time.gmtime" title="time.gmtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.gmtime()</span></code></a>).</p>
  1215. </dd></dl>
  1216. <dl class="py method">
  1217. <dt class="sig sig-object py" id="mailbox.mboxMessage.get_flags">
  1218. <span class="sig-name descname"><span class="pre">get_flags</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.mboxMessage.get_flags" title="Link to this definition">¶</a></dt>
  1219. <dd><p>Return a string specifying the flags that are currently set. If the
  1220. message complies with the conventional format, the result is the
  1221. concatenation in the following order of zero or one occurrence of each of
  1222. <code class="docutils literal notranslate"><span class="pre">'R'</span></code>, <code class="docutils literal notranslate"><span class="pre">'O'</span></code>, <code class="docutils literal notranslate"><span class="pre">'D'</span></code>, <code class="docutils literal notranslate"><span class="pre">'F'</span></code>, and <code class="docutils literal notranslate"><span class="pre">'A'</span></code>.</p>
  1223. </dd></dl>
  1224. <dl class="py method">
  1225. <dt class="sig sig-object py" id="mailbox.mboxMessage.set_flags">
  1226. <span class="sig-name descname"><span class="pre">set_flags</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">flags</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.mboxMessage.set_flags" title="Link to this definition">¶</a></dt>
  1227. <dd><p>Set the flags specified by <em>flags</em> and unset all others. Parameter <em>flags</em>
  1228. should be the concatenation in any order of zero or more occurrences of
  1229. each of <code class="docutils literal notranslate"><span class="pre">'R'</span></code>, <code class="docutils literal notranslate"><span class="pre">'O'</span></code>, <code class="docutils literal notranslate"><span class="pre">'D'</span></code>, <code class="docutils literal notranslate"><span class="pre">'F'</span></code>, and <code class="docutils literal notranslate"><span class="pre">'A'</span></code>.</p>
  1230. </dd></dl>
  1231. <dl class="py method">
  1232. <dt class="sig sig-object py" id="mailbox.mboxMessage.add_flag">
  1233. <span class="sig-name descname"><span class="pre">add_flag</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">flag</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.mboxMessage.add_flag" title="Link to this definition">¶</a></dt>
  1234. <dd><p>Set the flag(s) specified by <em>flag</em> without changing other flags. To add
  1235. more than one flag at a time, <em>flag</em> may be a string of more than one
  1236. character.</p>
  1237. </dd></dl>
  1238. <dl class="py method">
  1239. <dt class="sig sig-object py" id="mailbox.mboxMessage.remove_flag">
  1240. <span class="sig-name descname"><span class="pre">remove_flag</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">flag</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.mboxMessage.remove_flag" title="Link to this definition">¶</a></dt>
  1241. <dd><p>Unset the flag(s) specified by <em>flag</em> without changing other flags. To
  1242. remove more than one flag at a time, <em>flag</em> maybe a string of more than
  1243. one character.</p>
  1244. </dd></dl>
  1245. </dd></dl>
  1246. <p>When an <code class="xref py py-class docutils literal notranslate"><span class="pre">mboxMessage</span></code> instance is created based upon a
  1247. <a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MaildirMessage</span></code></a> instance, a “From “ line is generated based upon the
  1248. <a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MaildirMessage</span></code></a> instance’s delivery date, and the following conversions
  1249. take place:</p>
  1250. <table class="docutils align-default">
  1251. <thead>
  1252. <tr class="row-odd"><th class="head"><p>Resulting state</p></th>
  1253. <th class="head"><p><a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MaildirMessage</span></code></a> state</p></th>
  1254. </tr>
  1255. </thead>
  1256. <tbody>
  1257. <tr class="row-even"><td><p>R flag</p></td>
  1258. <td><p>S flag</p></td>
  1259. </tr>
  1260. <tr class="row-odd"><td><p>O flag</p></td>
  1261. <td><p>“cur” subdirectory</p></td>
  1262. </tr>
  1263. <tr class="row-even"><td><p>D flag</p></td>
  1264. <td><p>T flag</p></td>
  1265. </tr>
  1266. <tr class="row-odd"><td><p>F flag</p></td>
  1267. <td><p>F flag</p></td>
  1268. </tr>
  1269. <tr class="row-even"><td><p>A flag</p></td>
  1270. <td><p>R flag</p></td>
  1271. </tr>
  1272. </tbody>
  1273. </table>
  1274. <p>When an <code class="xref py py-class docutils literal notranslate"><span class="pre">mboxMessage</span></code> instance is created based upon an
  1275. <a class="reference internal" href="#mailbox.MHMessage" title="mailbox.MHMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MHMessage</span></code></a> instance, the following conversions take place:</p>
  1276. <table class="docutils align-default">
  1277. <thead>
  1278. <tr class="row-odd"><th class="head"><p>Resulting state</p></th>
  1279. <th class="head"><p><a class="reference internal" href="#mailbox.MHMessage" title="mailbox.MHMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MHMessage</span></code></a> state</p></th>
  1280. </tr>
  1281. </thead>
  1282. <tbody>
  1283. <tr class="row-even"><td><p>R flag and O flag</p></td>
  1284. <td><p>no “unseen” sequence</p></td>
  1285. </tr>
  1286. <tr class="row-odd"><td><p>O flag</p></td>
  1287. <td><p>“unseen” sequence</p></td>
  1288. </tr>
  1289. <tr class="row-even"><td><p>F flag</p></td>
  1290. <td><p>“flagged” sequence</p></td>
  1291. </tr>
  1292. <tr class="row-odd"><td><p>A flag</p></td>
  1293. <td><p>“replied” sequence</p></td>
  1294. </tr>
  1295. </tbody>
  1296. </table>
  1297. <p>When an <code class="xref py py-class docutils literal notranslate"><span class="pre">mboxMessage</span></code> instance is created based upon a
  1298. <a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">BabylMessage</span></code></a> instance, the following conversions take place:</p>
  1299. <table class="docutils align-default">
  1300. <thead>
  1301. <tr class="row-odd"><th class="head"><p>Resulting state</p></th>
  1302. <th class="head"><p><a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">BabylMessage</span></code></a> state</p></th>
  1303. </tr>
  1304. </thead>
  1305. <tbody>
  1306. <tr class="row-even"><td><p>R flag and O flag</p></td>
  1307. <td><p>no “unseen” label</p></td>
  1308. </tr>
  1309. <tr class="row-odd"><td><p>O flag</p></td>
  1310. <td><p>“unseen” label</p></td>
  1311. </tr>
  1312. <tr class="row-even"><td><p>D flag</p></td>
  1313. <td><p>“deleted” label</p></td>
  1314. </tr>
  1315. <tr class="row-odd"><td><p>A flag</p></td>
  1316. <td><p>“answered” label</p></td>
  1317. </tr>
  1318. </tbody>
  1319. </table>
  1320. <p>When a <code class="xref py py-class docutils literal notranslate"><span class="pre">mboxMessage</span></code> instance is created based upon an
  1321. <a class="reference internal" href="#mailbox.MMDFMessage" title="mailbox.MMDFMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MMDFMessage</span></code></a>
  1322. instance, the “From “ line is copied and all flags directly correspond:</p>
  1323. <table class="docutils align-default">
  1324. <thead>
  1325. <tr class="row-odd"><th class="head"><p>Resulting state</p></th>
  1326. <th class="head"><p><a class="reference internal" href="#mailbox.MMDFMessage" title="mailbox.MMDFMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MMDFMessage</span></code></a> state</p></th>
  1327. </tr>
  1328. </thead>
  1329. <tbody>
  1330. <tr class="row-even"><td><p>R flag</p></td>
  1331. <td><p>R flag</p></td>
  1332. </tr>
  1333. <tr class="row-odd"><td><p>O flag</p></td>
  1334. <td><p>O flag</p></td>
  1335. </tr>
  1336. <tr class="row-even"><td><p>D flag</p></td>
  1337. <td><p>D flag</p></td>
  1338. </tr>
  1339. <tr class="row-odd"><td><p>F flag</p></td>
  1340. <td><p>F flag</p></td>
  1341. </tr>
  1342. <tr class="row-even"><td><p>A flag</p></td>
  1343. <td><p>A flag</p></td>
  1344. </tr>
  1345. </tbody>
  1346. </table>
  1347. </section>
  1348. <section id="mhmessage-objects">
  1349. <span id="mailbox-mhmessage"></span><h3><code class="xref py py-class docutils literal notranslate"><span class="pre">MHMessage</span></code> objects<a class="headerlink" href="#mhmessage-objects" title="Link to this heading">¶</a></h3>
  1350. <dl class="py class">
  1351. <dt class="sig sig-object py" id="mailbox.MHMessage">
  1352. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">mailbox.</span></span><span class="sig-name descname"><span class="pre">MHMessage</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">message</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="#mailbox.MHMessage" title="Link to this definition">¶</a></dt>
  1353. <dd><p>A message with MH-specific behaviors. Parameter <em>message</em> has the same meaning
  1354. as with the <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> constructor.</p>
  1355. <p>MH messages do not support marks or flags in the traditional sense, but they
  1356. do support sequences, which are logical groupings of arbitrary messages. Some
  1357. mail reading programs (although not the standard <strong class="program">mh</strong> and
  1358. <strong class="program">nmh</strong>) use sequences in much the same way flags are used with other
  1359. formats, as follows:</p>
  1360. <table class="docutils align-default">
  1361. <thead>
  1362. <tr class="row-odd"><th class="head"><p>Sequence</p></th>
  1363. <th class="head"><p>Explanation</p></th>
  1364. </tr>
  1365. </thead>
  1366. <tbody>
  1367. <tr class="row-even"><td><p>unseen</p></td>
  1368. <td><p>Not read, but previously detected by MUA</p></td>
  1369. </tr>
  1370. <tr class="row-odd"><td><p>replied</p></td>
  1371. <td><p>Replied to</p></td>
  1372. </tr>
  1373. <tr class="row-even"><td><p>flagged</p></td>
  1374. <td><p>Marked as important</p></td>
  1375. </tr>
  1376. </tbody>
  1377. </table>
  1378. <p><code class="xref py py-class docutils literal notranslate"><span class="pre">MHMessage</span></code> instances offer the following methods:</p>
  1379. <dl class="py method">
  1380. <dt class="sig sig-object py" id="mailbox.MHMessage.get_sequences">
  1381. <span class="sig-name descname"><span class="pre">get_sequences</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MHMessage.get_sequences" title="Link to this definition">¶</a></dt>
  1382. <dd><p>Return a list of the names of sequences that include this message.</p>
  1383. </dd></dl>
  1384. <dl class="py method">
  1385. <dt class="sig sig-object py" id="mailbox.MHMessage.set_sequences">
  1386. <span class="sig-name descname"><span class="pre">set_sequences</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sequences</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MHMessage.set_sequences" title="Link to this definition">¶</a></dt>
  1387. <dd><p>Set the list of sequences that include this message.</p>
  1388. </dd></dl>
  1389. <dl class="py method">
  1390. <dt class="sig sig-object py" id="mailbox.MHMessage.add_sequence">
  1391. <span class="sig-name descname"><span class="pre">add_sequence</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sequence</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MHMessage.add_sequence" title="Link to this definition">¶</a></dt>
  1392. <dd><p>Add <em>sequence</em> to the list of sequences that include this message.</p>
  1393. </dd></dl>
  1394. <dl class="py method">
  1395. <dt class="sig sig-object py" id="mailbox.MHMessage.remove_sequence">
  1396. <span class="sig-name descname"><span class="pre">remove_sequence</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sequence</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MHMessage.remove_sequence" title="Link to this definition">¶</a></dt>
  1397. <dd><p>Remove <em>sequence</em> from the list of sequences that include this message.</p>
  1398. </dd></dl>
  1399. </dd></dl>
  1400. <p>When an <code class="xref py py-class docutils literal notranslate"><span class="pre">MHMessage</span></code> instance is created based upon a
  1401. <a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MaildirMessage</span></code></a> instance, the following conversions take place:</p>
  1402. <table class="docutils align-default">
  1403. <thead>
  1404. <tr class="row-odd"><th class="head"><p>Resulting state</p></th>
  1405. <th class="head"><p><a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MaildirMessage</span></code></a> state</p></th>
  1406. </tr>
  1407. </thead>
  1408. <tbody>
  1409. <tr class="row-even"><td><p>“unseen” sequence</p></td>
  1410. <td><p>no S flag</p></td>
  1411. </tr>
  1412. <tr class="row-odd"><td><p>“replied” sequence</p></td>
  1413. <td><p>R flag</p></td>
  1414. </tr>
  1415. <tr class="row-even"><td><p>“flagged” sequence</p></td>
  1416. <td><p>F flag</p></td>
  1417. </tr>
  1418. </tbody>
  1419. </table>
  1420. <p>When an <code class="xref py py-class docutils literal notranslate"><span class="pre">MHMessage</span></code> instance is created based upon an
  1421. <a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">mboxMessage</span></code></a> or <a class="reference internal" href="#mailbox.MMDFMessage" title="mailbox.MMDFMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MMDFMessage</span></code></a> instance, the <em class="mailheader">Status</em>
  1422. and <em class="mailheader">X-Status</em> headers are omitted and the following conversions
  1423. take place:</p>
  1424. <table class="docutils align-default">
  1425. <thead>
  1426. <tr class="row-odd"><th class="head"><p>Resulting state</p></th>
  1427. <th class="head"><p><a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">mboxMessage</span></code></a> or <a class="reference internal" href="#mailbox.MMDFMessage" title="mailbox.MMDFMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MMDFMessage</span></code></a>
  1428. state</p></th>
  1429. </tr>
  1430. </thead>
  1431. <tbody>
  1432. <tr class="row-even"><td><p>“unseen” sequence</p></td>
  1433. <td><p>no R flag</p></td>
  1434. </tr>
  1435. <tr class="row-odd"><td><p>“replied” sequence</p></td>
  1436. <td><p>A flag</p></td>
  1437. </tr>
  1438. <tr class="row-even"><td><p>“flagged” sequence</p></td>
  1439. <td><p>F flag</p></td>
  1440. </tr>
  1441. </tbody>
  1442. </table>
  1443. <p>When an <code class="xref py py-class docutils literal notranslate"><span class="pre">MHMessage</span></code> instance is created based upon a
  1444. <a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">BabylMessage</span></code></a> instance, the following conversions take place:</p>
  1445. <table class="docutils align-default">
  1446. <thead>
  1447. <tr class="row-odd"><th class="head"><p>Resulting state</p></th>
  1448. <th class="head"><p><a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">BabylMessage</span></code></a> state</p></th>
  1449. </tr>
  1450. </thead>
  1451. <tbody>
  1452. <tr class="row-even"><td><p>“unseen” sequence</p></td>
  1453. <td><p>“unseen” label</p></td>
  1454. </tr>
  1455. <tr class="row-odd"><td><p>“replied” sequence</p></td>
  1456. <td><p>“answered” label</p></td>
  1457. </tr>
  1458. </tbody>
  1459. </table>
  1460. </section>
  1461. <section id="babylmessage-objects">
  1462. <span id="mailbox-babylmessage"></span><h3><code class="xref py py-class docutils literal notranslate"><span class="pre">BabylMessage</span></code> objects<a class="headerlink" href="#babylmessage-objects" title="Link to this heading">¶</a></h3>
  1463. <dl class="py class">
  1464. <dt class="sig sig-object py" id="mailbox.BabylMessage">
  1465. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">mailbox.</span></span><span class="sig-name descname"><span class="pre">BabylMessage</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">message</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="#mailbox.BabylMessage" title="Link to this definition">¶</a></dt>
  1466. <dd><p>A message with Babyl-specific behaviors. Parameter <em>message</em> has the same
  1467. meaning as with the <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> constructor.</p>
  1468. <p>Certain message labels, called <em class="dfn">attributes</em>, are defined by convention
  1469. to have special meanings. The attributes are as follows:</p>
  1470. <table class="docutils align-default">
  1471. <thead>
  1472. <tr class="row-odd"><th class="head"><p>Label</p></th>
  1473. <th class="head"><p>Explanation</p></th>
  1474. </tr>
  1475. </thead>
  1476. <tbody>
  1477. <tr class="row-even"><td><p>unseen</p></td>
  1478. <td><p>Not read, but previously detected by MUA</p></td>
  1479. </tr>
  1480. <tr class="row-odd"><td><p>deleted</p></td>
  1481. <td><p>Marked for subsequent deletion</p></td>
  1482. </tr>
  1483. <tr class="row-even"><td><p>filed</p></td>
  1484. <td><p>Copied to another file or mailbox</p></td>
  1485. </tr>
  1486. <tr class="row-odd"><td><p>answered</p></td>
  1487. <td><p>Replied to</p></td>
  1488. </tr>
  1489. <tr class="row-even"><td><p>forwarded</p></td>
  1490. <td><p>Forwarded</p></td>
  1491. </tr>
  1492. <tr class="row-odd"><td><p>edited</p></td>
  1493. <td><p>Modified by the user</p></td>
  1494. </tr>
  1495. <tr class="row-even"><td><p>resent</p></td>
  1496. <td><p>Resent</p></td>
  1497. </tr>
  1498. </tbody>
  1499. </table>
  1500. <p>By default, Rmail displays only visible headers. The <code class="xref py py-class docutils literal notranslate"><span class="pre">BabylMessage</span></code>
  1501. class, though, uses the original headers because they are more
  1502. complete. Visible headers may be accessed explicitly if desired.</p>
  1503. <p><code class="xref py py-class docutils literal notranslate"><span class="pre">BabylMessage</span></code> instances offer the following methods:</p>
  1504. <dl class="py method">
  1505. <dt class="sig sig-object py" id="mailbox.BabylMessage.get_labels">
  1506. <span class="sig-name descname"><span class="pre">get_labels</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.BabylMessage.get_labels" title="Link to this definition">¶</a></dt>
  1507. <dd><p>Return a list of labels on the message.</p>
  1508. </dd></dl>
  1509. <dl class="py method">
  1510. <dt class="sig sig-object py" id="mailbox.BabylMessage.set_labels">
  1511. <span class="sig-name descname"><span class="pre">set_labels</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">labels</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.BabylMessage.set_labels" title="Link to this definition">¶</a></dt>
  1512. <dd><p>Set the list of labels on the message to <em>labels</em>.</p>
  1513. </dd></dl>
  1514. <dl class="py method">
  1515. <dt class="sig sig-object py" id="mailbox.BabylMessage.add_label">
  1516. <span class="sig-name descname"><span class="pre">add_label</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">label</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.BabylMessage.add_label" title="Link to this definition">¶</a></dt>
  1517. <dd><p>Add <em>label</em> to the list of labels on the message.</p>
  1518. </dd></dl>
  1519. <dl class="py method">
  1520. <dt class="sig sig-object py" id="mailbox.BabylMessage.remove_label">
  1521. <span class="sig-name descname"><span class="pre">remove_label</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">label</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.BabylMessage.remove_label" title="Link to this definition">¶</a></dt>
  1522. <dd><p>Remove <em>label</em> from the list of labels on the message.</p>
  1523. </dd></dl>
  1524. <dl class="py method">
  1525. <dt class="sig sig-object py" id="mailbox.BabylMessage.get_visible">
  1526. <span class="sig-name descname"><span class="pre">get_visible</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.BabylMessage.get_visible" title="Link to this definition">¶</a></dt>
  1527. <dd><p>Return an <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> instance whose headers are the message’s
  1528. visible headers and whose body is empty.</p>
  1529. </dd></dl>
  1530. <dl class="py method">
  1531. <dt class="sig sig-object py" id="mailbox.BabylMessage.set_visible">
  1532. <span class="sig-name descname"><span class="pre">set_visible</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">visible</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.BabylMessage.set_visible" title="Link to this definition">¶</a></dt>
  1533. <dd><p>Set the message’s visible headers to be the same as the headers in
  1534. <em>message</em>. Parameter <em>visible</em> should be a <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> instance, an
  1535. <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">email.message.Message</span></code></a> instance, a string, or a file-like object
  1536. (which should be open in text mode).</p>
  1537. </dd></dl>
  1538. <dl class="py method">
  1539. <dt class="sig sig-object py" id="mailbox.BabylMessage.update_visible">
  1540. <span class="sig-name descname"><span class="pre">update_visible</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.BabylMessage.update_visible" title="Link to this definition">¶</a></dt>
  1541. <dd><p>When a <code class="xref py py-class docutils literal notranslate"><span class="pre">BabylMessage</span></code> instance’s original headers are modified, the
  1542. visible headers are not automatically modified to correspond. This method
  1543. updates the visible headers as follows: each visible header with a
  1544. corresponding original header is set to the value of the original header,
  1545. each visible header without a corresponding original header is removed,
  1546. and any of <em class="mailheader">Date</em>, <em class="mailheader">From</em>, <em class="mailheader">Reply-To</em>,
  1547. <em class="mailheader">To</em>, <em class="mailheader">CC</em>, and <em class="mailheader">Subject</em> that are
  1548. present in the original headers but not the visible headers are added to
  1549. the visible headers.</p>
  1550. </dd></dl>
  1551. </dd></dl>
  1552. <p>When a <code class="xref py py-class docutils literal notranslate"><span class="pre">BabylMessage</span></code> instance is created based upon a
  1553. <a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MaildirMessage</span></code></a> instance, the following conversions take place:</p>
  1554. <table class="docutils align-default">
  1555. <thead>
  1556. <tr class="row-odd"><th class="head"><p>Resulting state</p></th>
  1557. <th class="head"><p><a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MaildirMessage</span></code></a> state</p></th>
  1558. </tr>
  1559. </thead>
  1560. <tbody>
  1561. <tr class="row-even"><td><p>“unseen” label</p></td>
  1562. <td><p>no S flag</p></td>
  1563. </tr>
  1564. <tr class="row-odd"><td><p>“deleted” label</p></td>
  1565. <td><p>T flag</p></td>
  1566. </tr>
  1567. <tr class="row-even"><td><p>“answered” label</p></td>
  1568. <td><p>R flag</p></td>
  1569. </tr>
  1570. <tr class="row-odd"><td><p>“forwarded” label</p></td>
  1571. <td><p>P flag</p></td>
  1572. </tr>
  1573. </tbody>
  1574. </table>
  1575. <p>When a <code class="xref py py-class docutils literal notranslate"><span class="pre">BabylMessage</span></code> instance is created based upon an
  1576. <a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">mboxMessage</span></code></a> or <a class="reference internal" href="#mailbox.MMDFMessage" title="mailbox.MMDFMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MMDFMessage</span></code></a> instance, the <em class="mailheader">Status</em>
  1577. and <em class="mailheader">X-Status</em> headers are omitted and the following conversions
  1578. take place:</p>
  1579. <table class="docutils align-default">
  1580. <thead>
  1581. <tr class="row-odd"><th class="head"><p>Resulting state</p></th>
  1582. <th class="head"><p><a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">mboxMessage</span></code></a> or <a class="reference internal" href="#mailbox.MMDFMessage" title="mailbox.MMDFMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MMDFMessage</span></code></a>
  1583. state</p></th>
  1584. </tr>
  1585. </thead>
  1586. <tbody>
  1587. <tr class="row-even"><td><p>“unseen” label</p></td>
  1588. <td><p>no R flag</p></td>
  1589. </tr>
  1590. <tr class="row-odd"><td><p>“deleted” label</p></td>
  1591. <td><p>D flag</p></td>
  1592. </tr>
  1593. <tr class="row-even"><td><p>“answered” label</p></td>
  1594. <td><p>A flag</p></td>
  1595. </tr>
  1596. </tbody>
  1597. </table>
  1598. <p>When a <code class="xref py py-class docutils literal notranslate"><span class="pre">BabylMessage</span></code> instance is created based upon an
  1599. <a class="reference internal" href="#mailbox.MHMessage" title="mailbox.MHMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MHMessage</span></code></a> instance, the following conversions take place:</p>
  1600. <table class="docutils align-default">
  1601. <thead>
  1602. <tr class="row-odd"><th class="head"><p>Resulting state</p></th>
  1603. <th class="head"><p><a class="reference internal" href="#mailbox.MHMessage" title="mailbox.MHMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MHMessage</span></code></a> state</p></th>
  1604. </tr>
  1605. </thead>
  1606. <tbody>
  1607. <tr class="row-even"><td><p>“unseen” label</p></td>
  1608. <td><p>“unseen” sequence</p></td>
  1609. </tr>
  1610. <tr class="row-odd"><td><p>“answered” label</p></td>
  1611. <td><p>“replied” sequence</p></td>
  1612. </tr>
  1613. </tbody>
  1614. </table>
  1615. </section>
  1616. <section id="mmdfmessage-objects">
  1617. <span id="mailbox-mmdfmessage"></span><h3><code class="xref py py-class docutils literal notranslate"><span class="pre">MMDFMessage</span></code> objects<a class="headerlink" href="#mmdfmessage-objects" title="Link to this heading">¶</a></h3>
  1618. <dl class="py class">
  1619. <dt class="sig sig-object py" id="mailbox.MMDFMessage">
  1620. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">mailbox.</span></span><span class="sig-name descname"><span class="pre">MMDFMessage</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">message</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="#mailbox.MMDFMessage" title="Link to this definition">¶</a></dt>
  1621. <dd><p>A message with MMDF-specific behaviors. Parameter <em>message</em> has the same meaning
  1622. as with the <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code></a> constructor.</p>
  1623. <p>As with message in an mbox mailbox, MMDF messages are stored with the
  1624. sender’s address and the delivery date in an initial line beginning with
  1625. “From “. Likewise, flags that indicate the state of the message are
  1626. typically stored in <em class="mailheader">Status</em> and <em class="mailheader">X-Status</em> headers.</p>
  1627. <p>Conventional flags for MMDF messages are identical to those of mbox message
  1628. and are as follows:</p>
  1629. <table class="docutils align-default">
  1630. <thead>
  1631. <tr class="row-odd"><th class="head"><p>Flag</p></th>
  1632. <th class="head"><p>Meaning</p></th>
  1633. <th class="head"><p>Explanation</p></th>
  1634. </tr>
  1635. </thead>
  1636. <tbody>
  1637. <tr class="row-even"><td><p>R</p></td>
  1638. <td><p>Read</p></td>
  1639. <td><p>Read</p></td>
  1640. </tr>
  1641. <tr class="row-odd"><td><p>O</p></td>
  1642. <td><p>Old</p></td>
  1643. <td><p>Previously detected by MUA</p></td>
  1644. </tr>
  1645. <tr class="row-even"><td><p>D</p></td>
  1646. <td><p>Deleted</p></td>
  1647. <td><p>Marked for subsequent deletion</p></td>
  1648. </tr>
  1649. <tr class="row-odd"><td><p>F</p></td>
  1650. <td><p>Flagged</p></td>
  1651. <td><p>Marked as important</p></td>
  1652. </tr>
  1653. <tr class="row-even"><td><p>A</p></td>
  1654. <td><p>Answered</p></td>
  1655. <td><p>Replied to</p></td>
  1656. </tr>
  1657. </tbody>
  1658. </table>
  1659. <p>The “R” and “O” flags are stored in the <em class="mailheader">Status</em> header, and the
  1660. “D”, “F”, and “A” flags are stored in the <em class="mailheader">X-Status</em> header. The
  1661. flags and headers typically appear in the order mentioned.</p>
  1662. <p><code class="xref py py-class docutils literal notranslate"><span class="pre">MMDFMessage</span></code> instances offer the following methods, which are
  1663. identical to those offered by <a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">mboxMessage</span></code></a>:</p>
  1664. <dl class="py method">
  1665. <dt class="sig sig-object py" id="mailbox.MMDFMessage.get_from">
  1666. <span class="sig-name descname"><span class="pre">get_from</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MMDFMessage.get_from" title="Link to this definition">¶</a></dt>
  1667. <dd><p>Return a string representing the “From “ line that marks the start of the
  1668. message in an mbox mailbox. The leading “From “ and the trailing newline
  1669. are excluded.</p>
  1670. </dd></dl>
  1671. <dl class="py method">
  1672. <dt class="sig sig-object py" id="mailbox.MMDFMessage.set_from">
  1673. <span class="sig-name descname"><span class="pre">set_from</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">from_</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">time_</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="#mailbox.MMDFMessage.set_from" title="Link to this definition">¶</a></dt>
  1674. <dd><p>Set the “From “ line to <em>from_</em>, which should be specified without a
  1675. leading “From “ or trailing newline. For convenience, <em>time_</em> may be
  1676. specified and will be formatted appropriately and appended to <em>from_</em>. If
  1677. <em>time_</em> is specified, it should be a <a class="reference internal" href="time.html#time.struct_time" title="time.struct_time"><code class="xref py py-class docutils literal notranslate"><span class="pre">time.struct_time</span></code></a> instance, a
  1678. tuple suitable for passing to <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.strftime()</span></code></a>, or <code class="docutils literal notranslate"><span class="pre">True</span></code> (to use
  1679. <a class="reference internal" href="time.html#time.gmtime" title="time.gmtime"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.gmtime()</span></code></a>).</p>
  1680. </dd></dl>
  1681. <dl class="py method">
  1682. <dt class="sig sig-object py" id="mailbox.MMDFMessage.get_flags">
  1683. <span class="sig-name descname"><span class="pre">get_flags</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MMDFMessage.get_flags" title="Link to this definition">¶</a></dt>
  1684. <dd><p>Return a string specifying the flags that are currently set. If the
  1685. message complies with the conventional format, the result is the
  1686. concatenation in the following order of zero or one occurrence of each of
  1687. <code class="docutils literal notranslate"><span class="pre">'R'</span></code>, <code class="docutils literal notranslate"><span class="pre">'O'</span></code>, <code class="docutils literal notranslate"><span class="pre">'D'</span></code>, <code class="docutils literal notranslate"><span class="pre">'F'</span></code>, and <code class="docutils literal notranslate"><span class="pre">'A'</span></code>.</p>
  1688. </dd></dl>
  1689. <dl class="py method">
  1690. <dt class="sig sig-object py" id="mailbox.MMDFMessage.set_flags">
  1691. <span class="sig-name descname"><span class="pre">set_flags</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">flags</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MMDFMessage.set_flags" title="Link to this definition">¶</a></dt>
  1692. <dd><p>Set the flags specified by <em>flags</em> and unset all others. Parameter <em>flags</em>
  1693. should be the concatenation in any order of zero or more occurrences of
  1694. each of <code class="docutils literal notranslate"><span class="pre">'R'</span></code>, <code class="docutils literal notranslate"><span class="pre">'O'</span></code>, <code class="docutils literal notranslate"><span class="pre">'D'</span></code>, <code class="docutils literal notranslate"><span class="pre">'F'</span></code>, and <code class="docutils literal notranslate"><span class="pre">'A'</span></code>.</p>
  1695. </dd></dl>
  1696. <dl class="py method">
  1697. <dt class="sig sig-object py" id="mailbox.MMDFMessage.add_flag">
  1698. <span class="sig-name descname"><span class="pre">add_flag</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">flag</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MMDFMessage.add_flag" title="Link to this definition">¶</a></dt>
  1699. <dd><p>Set the flag(s) specified by <em>flag</em> without changing other flags. To add
  1700. more than one flag at a time, <em>flag</em> may be a string of more than one
  1701. character.</p>
  1702. </dd></dl>
  1703. <dl class="py method">
  1704. <dt class="sig sig-object py" id="mailbox.MMDFMessage.remove_flag">
  1705. <span class="sig-name descname"><span class="pre">remove_flag</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">flag</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#mailbox.MMDFMessage.remove_flag" title="Link to this definition">¶</a></dt>
  1706. <dd><p>Unset the flag(s) specified by <em>flag</em> without changing other flags. To
  1707. remove more than one flag at a time, <em>flag</em> maybe a string of more than
  1708. one character.</p>
  1709. </dd></dl>
  1710. </dd></dl>
  1711. <p>When an <code class="xref py py-class docutils literal notranslate"><span class="pre">MMDFMessage</span></code> instance is created based upon a
  1712. <a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MaildirMessage</span></code></a> instance, a “From “ line is generated based upon the
  1713. <a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MaildirMessage</span></code></a> instance’s delivery date, and the following conversions
  1714. take place:</p>
  1715. <table class="docutils align-default">
  1716. <thead>
  1717. <tr class="row-odd"><th class="head"><p>Resulting state</p></th>
  1718. <th class="head"><p><a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MaildirMessage</span></code></a> state</p></th>
  1719. </tr>
  1720. </thead>
  1721. <tbody>
  1722. <tr class="row-even"><td><p>R flag</p></td>
  1723. <td><p>S flag</p></td>
  1724. </tr>
  1725. <tr class="row-odd"><td><p>O flag</p></td>
  1726. <td><p>“cur” subdirectory</p></td>
  1727. </tr>
  1728. <tr class="row-even"><td><p>D flag</p></td>
  1729. <td><p>T flag</p></td>
  1730. </tr>
  1731. <tr class="row-odd"><td><p>F flag</p></td>
  1732. <td><p>F flag</p></td>
  1733. </tr>
  1734. <tr class="row-even"><td><p>A flag</p></td>
  1735. <td><p>R flag</p></td>
  1736. </tr>
  1737. </tbody>
  1738. </table>
  1739. <p>When an <code class="xref py py-class docutils literal notranslate"><span class="pre">MMDFMessage</span></code> instance is created based upon an
  1740. <a class="reference internal" href="#mailbox.MHMessage" title="mailbox.MHMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MHMessage</span></code></a> instance, the following conversions take place:</p>
  1741. <table class="docutils align-default">
  1742. <thead>
  1743. <tr class="row-odd"><th class="head"><p>Resulting state</p></th>
  1744. <th class="head"><p><a class="reference internal" href="#mailbox.MHMessage" title="mailbox.MHMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">MHMessage</span></code></a> state</p></th>
  1745. </tr>
  1746. </thead>
  1747. <tbody>
  1748. <tr class="row-even"><td><p>R flag and O flag</p></td>
  1749. <td><p>no “unseen” sequence</p></td>
  1750. </tr>
  1751. <tr class="row-odd"><td><p>O flag</p></td>
  1752. <td><p>“unseen” sequence</p></td>
  1753. </tr>
  1754. <tr class="row-even"><td><p>F flag</p></td>
  1755. <td><p>“flagged” sequence</p></td>
  1756. </tr>
  1757. <tr class="row-odd"><td><p>A flag</p></td>
  1758. <td><p>“replied” sequence</p></td>
  1759. </tr>
  1760. </tbody>
  1761. </table>
  1762. <p>When an <code class="xref py py-class docutils literal notranslate"><span class="pre">MMDFMessage</span></code> instance is created based upon a
  1763. <a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">BabylMessage</span></code></a> instance, the following conversions take place:</p>
  1764. <table class="docutils align-default">
  1765. <thead>
  1766. <tr class="row-odd"><th class="head"><p>Resulting state</p></th>
  1767. <th class="head"><p><a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">BabylMessage</span></code></a> state</p></th>
  1768. </tr>
  1769. </thead>
  1770. <tbody>
  1771. <tr class="row-even"><td><p>R flag and O flag</p></td>
  1772. <td><p>no “unseen” label</p></td>
  1773. </tr>
  1774. <tr class="row-odd"><td><p>O flag</p></td>
  1775. <td><p>“unseen” label</p></td>
  1776. </tr>
  1777. <tr class="row-even"><td><p>D flag</p></td>
  1778. <td><p>“deleted” label</p></td>
  1779. </tr>
  1780. <tr class="row-odd"><td><p>A flag</p></td>
  1781. <td><p>“answered” label</p></td>
  1782. </tr>
  1783. </tbody>
  1784. </table>
  1785. <p>When an <code class="xref py py-class docutils literal notranslate"><span class="pre">MMDFMessage</span></code> instance is created based upon an
  1786. <a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">mboxMessage</span></code></a> instance, the “From “ line is copied and all flags directly
  1787. correspond:</p>
  1788. <table class="docutils align-default">
  1789. <thead>
  1790. <tr class="row-odd"><th class="head"><p>Resulting state</p></th>
  1791. <th class="head"><p><a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">mboxMessage</span></code></a> state</p></th>
  1792. </tr>
  1793. </thead>
  1794. <tbody>
  1795. <tr class="row-even"><td><p>R flag</p></td>
  1796. <td><p>R flag</p></td>
  1797. </tr>
  1798. <tr class="row-odd"><td><p>O flag</p></td>
  1799. <td><p>O flag</p></td>
  1800. </tr>
  1801. <tr class="row-even"><td><p>D flag</p></td>
  1802. <td><p>D flag</p></td>
  1803. </tr>
  1804. <tr class="row-odd"><td><p>F flag</p></td>
  1805. <td><p>F flag</p></td>
  1806. </tr>
  1807. <tr class="row-even"><td><p>A flag</p></td>
  1808. <td><p>A flag</p></td>
  1809. </tr>
  1810. </tbody>
  1811. </table>
  1812. </section>
  1813. </section>
  1814. <section id="exceptions">
  1815. <h2>Exceptions<a class="headerlink" href="#exceptions" title="Link to this heading">¶</a></h2>
  1816. <p>The following exception classes are defined in the <code class="xref py py-mod docutils literal notranslate"><span class="pre">mailbox</span></code> module:</p>
  1817. <dl class="py exception">
  1818. <dt class="sig sig-object py" id="mailbox.Error">
  1819. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">mailbox.</span></span><span class="sig-name descname"><span class="pre">Error</span></span><a class="headerlink" href="#mailbox.Error" title="Link to this definition">¶</a></dt>
  1820. <dd><p>The based class for all other module-specific exceptions.</p>
  1821. </dd></dl>
  1822. <dl class="py exception">
  1823. <dt class="sig sig-object py" id="mailbox.NoSuchMailboxError">
  1824. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">mailbox.</span></span><span class="sig-name descname"><span class="pre">NoSuchMailboxError</span></span><a class="headerlink" href="#mailbox.NoSuchMailboxError" title="Link to this definition">¶</a></dt>
  1825. <dd><p>Raised when a mailbox is expected but is not found, such as when instantiating a
  1826. <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code></a> subclass with a path that does not exist (and with the <em>create</em>
  1827. parameter set to <code class="docutils literal notranslate"><span class="pre">False</span></code>), or when opening a folder that does not exist.</p>
  1828. </dd></dl>
  1829. <dl class="py exception">
  1830. <dt class="sig sig-object py" id="mailbox.NotEmptyError">
  1831. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">mailbox.</span></span><span class="sig-name descname"><span class="pre">NotEmptyError</span></span><a class="headerlink" href="#mailbox.NotEmptyError" title="Link to this definition">¶</a></dt>
  1832. <dd><p>Raised when a mailbox is not empty but is expected to be, such as when deleting
  1833. a folder that contains messages.</p>
  1834. </dd></dl>
  1835. <dl class="py exception">
  1836. <dt class="sig sig-object py" id="mailbox.ExternalClashError">
  1837. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">mailbox.</span></span><span class="sig-name descname"><span class="pre">ExternalClashError</span></span><a class="headerlink" href="#mailbox.ExternalClashError" title="Link to this definition">¶</a></dt>
  1838. <dd><p>Raised when some mailbox-related condition beyond the control of the program
  1839. causes it to be unable to proceed, such as when failing to acquire a lock that
  1840. another program already holds a lock, or when a uniquely generated file name
  1841. already exists.</p>
  1842. </dd></dl>
  1843. <dl class="py exception">
  1844. <dt class="sig sig-object py" id="mailbox.FormatError">
  1845. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">mailbox.</span></span><span class="sig-name descname"><span class="pre">FormatError</span></span><a class="headerlink" href="#mailbox.FormatError" title="Link to this definition">¶</a></dt>
  1846. <dd><p>Raised when the data in a file cannot be parsed, such as when an <a class="reference internal" href="#mailbox.MH" title="mailbox.MH"><code class="xref py py-class docutils literal notranslate"><span class="pre">MH</span></code></a>
  1847. instance attempts to read a corrupted <code class="file docutils literal notranslate"><span class="pre">.mh_sequences</span></code> file.</p>
  1848. </dd></dl>
  1849. </section>
  1850. <section id="examples">
  1851. <span id="mailbox-examples"></span><h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>
  1852. <p>A simple example of printing the subjects of all messages in a mailbox that seem
  1853. interesting:</p>
  1854. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">mailbox</span>
  1855. <span class="k">for</span> <span class="n">message</span> <span class="ow">in</span> <span class="n">mailbox</span><span class="o">.</span><span class="n">mbox</span><span class="p">(</span><span class="s1">&#39;~/mbox&#39;</span><span class="p">):</span>
  1856. <span class="n">subject</span> <span class="o">=</span> <span class="n">message</span><span class="p">[</span><span class="s1">&#39;subject&#39;</span><span class="p">]</span> <span class="c1"># Could possibly be None.</span>
  1857. <span class="k">if</span> <span class="n">subject</span> <span class="ow">and</span> <span class="s1">&#39;python&#39;</span> <span class="ow">in</span> <span class="n">subject</span><span class="o">.</span><span class="n">lower</span><span class="p">():</span>
  1858. <span class="nb">print</span><span class="p">(</span><span class="n">subject</span><span class="p">)</span>
  1859. </pre></div>
  1860. </div>
  1861. <p>To copy all mail from a Babyl mailbox to an MH mailbox, converting all of the
  1862. format-specific information that can be converted:</p>
  1863. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">mailbox</span>
  1864. <span class="n">destination</span> <span class="o">=</span> <span class="n">mailbox</span><span class="o">.</span><span class="n">MH</span><span class="p">(</span><span class="s1">&#39;~/Mail&#39;</span><span class="p">)</span>
  1865. <span class="n">destination</span><span class="o">.</span><span class="n">lock</span><span class="p">()</span>
  1866. <span class="k">for</span> <span class="n">message</span> <span class="ow">in</span> <span class="n">mailbox</span><span class="o">.</span><span class="n">Babyl</span><span class="p">(</span><span class="s1">&#39;~/RMAIL&#39;</span><span class="p">):</span>
  1867. <span class="n">destination</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">mailbox</span><span class="o">.</span><span class="n">MHMessage</span><span class="p">(</span><span class="n">message</span><span class="p">))</span>
  1868. <span class="n">destination</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
  1869. <span class="n">destination</span><span class="o">.</span><span class="n">unlock</span><span class="p">()</span>
  1870. </pre></div>
  1871. </div>
  1872. <p>This example sorts mail from several mailing lists into different mailboxes,
  1873. being careful to avoid mail corruption due to concurrent modification by other
  1874. programs, mail loss due to interruption of the program, or premature termination
  1875. due to malformed messages in the mailbox:</p>
  1876. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">mailbox</span>
  1877. <span class="kn">import</span> <span class="nn">email.errors</span>
  1878. <span class="n">list_names</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;python-list&#39;</span><span class="p">,</span> <span class="s1">&#39;python-dev&#39;</span><span class="p">,</span> <span class="s1">&#39;python-bugs&#39;</span><span class="p">)</span>
  1879. <span class="n">boxes</span> <span class="o">=</span> <span class="p">{</span><span class="n">name</span><span class="p">:</span> <span class="n">mailbox</span><span class="o">.</span><span class="n">mbox</span><span class="p">(</span><span class="s1">&#39;~/email/</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span> <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">list_names</span><span class="p">}</span>
  1880. <span class="n">inbox</span> <span class="o">=</span> <span class="n">mailbox</span><span class="o">.</span><span class="n">Maildir</span><span class="p">(</span><span class="s1">&#39;~/Maildir&#39;</span><span class="p">,</span> <span class="n">factory</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
  1881. <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">inbox</span><span class="o">.</span><span class="n">iterkeys</span><span class="p">():</span>
  1882. <span class="k">try</span><span class="p">:</span>
  1883. <span class="n">message</span> <span class="o">=</span> <span class="n">inbox</span><span class="p">[</span><span class="n">key</span><span class="p">]</span>
  1884. <span class="k">except</span> <span class="n">email</span><span class="o">.</span><span class="n">errors</span><span class="o">.</span><span class="n">MessageParseError</span><span class="p">:</span>
  1885. <span class="k">continue</span> <span class="c1"># The message is malformed. Just leave it.</span>
  1886. <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">list_names</span><span class="p">:</span>
  1887. <span class="n">list_id</span> <span class="o">=</span> <span class="n">message</span><span class="p">[</span><span class="s1">&#39;list-id&#39;</span><span class="p">]</span>
  1888. <span class="k">if</span> <span class="n">list_id</span> <span class="ow">and</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">list_id</span><span class="p">:</span>
  1889. <span class="c1"># Get mailbox to use</span>
  1890. <span class="n">box</span> <span class="o">=</span> <span class="n">boxes</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
  1891. <span class="c1"># Write copy to disk before removing original.</span>
  1892. <span class="c1"># If there&#39;s a crash, you might duplicate a message, but</span>
  1893. <span class="c1"># that&#39;s better than losing a message completely.</span>
  1894. <span class="n">box</span><span class="o">.</span><span class="n">lock</span><span class="p">()</span>
  1895. <span class="n">box</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
  1896. <span class="n">box</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
  1897. <span class="n">box</span><span class="o">.</span><span class="n">unlock</span><span class="p">()</span>
  1898. <span class="c1"># Remove original message</span>
  1899. <span class="n">inbox</span><span class="o">.</span><span class="n">lock</span><span class="p">()</span>
  1900. <span class="n">inbox</span><span class="o">.</span><span class="n">discard</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
  1901. <span class="n">inbox</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
  1902. <span class="n">inbox</span><span class="o">.</span><span class="n">unlock</span><span class="p">()</span>
  1903. <span class="k">break</span> <span class="c1"># Found destination, so stop looking.</span>
  1904. <span class="k">for</span> <span class="n">box</span> <span class="ow">in</span> <span class="n">boxes</span><span class="o">.</span><span class="n">itervalues</span><span class="p">():</span>
  1905. <span class="n">box</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
  1906. </pre></div>
  1907. </div>
  1908. </section>
  1909. </section>
  1910. <div class="clearer"></div>
  1911. </div>
  1912. </div>
  1913. </div>
  1914. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1915. <div class="sphinxsidebarwrapper">
  1916. <div>
  1917. <h3><a href="../contents.html">Table of Contents</a></h3>
  1918. <ul>
  1919. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">mailbox</span></code> — Manipulate mailboxes in various formats</a><ul>
  1920. <li><a class="reference internal" href="#mailbox-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">Mailbox</span></code> objects</a><ul>
  1921. <li><a class="reference internal" href="#maildir-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">Maildir</span></code> objects</a></li>
  1922. <li><a class="reference internal" href="#mbox-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">mbox</span></code> objects</a></li>
  1923. <li><a class="reference internal" href="#mh-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">MH</span></code> objects</a></li>
  1924. <li><a class="reference internal" href="#babyl-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">Babyl</span></code> objects</a></li>
  1925. <li><a class="reference internal" href="#mmdf-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">MMDF</span></code> objects</a></li>
  1926. </ul>
  1927. </li>
  1928. <li><a class="reference internal" href="#message-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">Message</span></code> objects</a><ul>
  1929. <li><a class="reference internal" href="#maildirmessage-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">MaildirMessage</span></code> objects</a></li>
  1930. <li><a class="reference internal" href="#mboxmessage-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">mboxMessage</span></code> objects</a></li>
  1931. <li><a class="reference internal" href="#mhmessage-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">MHMessage</span></code> objects</a></li>
  1932. <li><a class="reference internal" href="#babylmessage-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">BabylMessage</span></code> objects</a></li>
  1933. <li><a class="reference internal" href="#mmdfmessage-objects"><code class="xref py py-class docutils literal notranslate"><span class="pre">MMDFMessage</span></code> objects</a></li>
  1934. </ul>
  1935. </li>
  1936. <li><a class="reference internal" href="#exceptions">Exceptions</a></li>
  1937. <li><a class="reference internal" href="#examples">Examples</a></li>
  1938. </ul>
  1939. </li>
  1940. </ul>
  1941. </div>
  1942. <div>
  1943. <h4>Previous topic</h4>
  1944. <p class="topless"><a href="json.html"
  1945. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code> — JSON encoder and decoder</a></p>
  1946. </div>
  1947. <div>
  1948. <h4>Next topic</h4>
  1949. <p class="topless"><a href="mimetypes.html"
  1950. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">mimetypes</span></code> — Map filenames to MIME types</a></p>
  1951. </div>
  1952. <div role="note" aria-label="source link">
  1953. <h3>This Page</h3>
  1954. <ul class="this-page-menu">
  1955. <li><a href="../bugs.html">Report a Bug</a></li>
  1956. <li>
  1957. <a href="https://github.com/python/cpython/blob/main/Doc/library/mailbox.rst"
  1958. rel="nofollow">Show Source
  1959. </a>
  1960. </li>
  1961. </ul>
  1962. </div>
  1963. </div>
  1964. <div id="sidebarbutton" title="Collapse sidebar">
  1965. <span>«</span>
  1966. </div>
  1967. </div>
  1968. <div class="clearer"></div>
  1969. </div>
  1970. <div class="related" role="navigation" aria-label="related navigation">
  1971. <h3>Navigation</h3>
  1972. <ul>
  1973. <li class="right" style="margin-right: 10px">
  1974. <a href="../genindex.html" title="General Index"
  1975. >index</a></li>
  1976. <li class="right" >
  1977. <a href="../py-modindex.html" title="Python Module Index"
  1978. >modules</a> |</li>
  1979. <li class="right" >
  1980. <a href="mimetypes.html" title="mimetypes — Map filenames to MIME types"
  1981. >next</a> |</li>
  1982. <li class="right" >
  1983. <a href="json.html" title="json — JSON encoder and decoder"
  1984. >previous</a> |</li>
  1985. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1986. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1987. <li class="switchers">
  1988. <div class="language_switcher_placeholder"></div>
  1989. <div class="version_switcher_placeholder"></div>
  1990. </li>
  1991. <li>
  1992. </li>
  1993. <li id="cpython-language-and-version">
  1994. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1995. </li>
  1996. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  1997. <li class="nav-item nav-item-2"><a href="netdata.html" >Internet Data Handling</a> &#187;</li>
  1998. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">mailbox</span></code> — Manipulate mailboxes in various formats</a></li>
  1999. <li class="right">
  2000. <div class="inline-search" role="search">
  2001. <form class="inline-search" action="../search.html" method="get">
  2002. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  2003. <input type="submit" value="Go" />
  2004. </form>
  2005. </div>
  2006. |
  2007. </li>
  2008. <li class="right">
  2009. <label class="theme-selector-label">
  2010. Theme
  2011. <select class="theme-selector" oninput="activateTheme(this.value)">
  2012. <option value="auto" selected>Auto</option>
  2013. <option value="light">Light</option>
  2014. <option value="dark">Dark</option>
  2015. </select>
  2016. </label> |</li>
  2017. </ul>
  2018. </div>
  2019. <div class="footer">
  2020. &copy;
  2021. <a href="../copyright.html">
  2022. Copyright
  2023. </a>
  2024. 2001-2024, Python Software Foundation.
  2025. <br />
  2026. This page is licensed under the Python Software Foundation License Version 2.
  2027. <br />
  2028. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  2029. <br />
  2030. See <a href="/license.html">History and License</a> for more information.<br />
  2031. <br />
  2032. The Python Software Foundation is a non-profit corporation.
  2033. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  2034. <br />
  2035. <br />
  2036. Last updated on Apr 09, 2024 (13:47 UTC).
  2037. <a href="/bugs.html">Found a bug</a>?
  2038. <br />
  2039. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  2040. </div>
  2041. </body>
  2042. </html>
上海开阖软件有限公司 沪ICP备12045867号-1