Sh3ll
OdayForums


Server : Apache
System : Linux server1.cgrithy.com 3.10.0-1160.95.1.el7.x86_64 #1 SMP Mon Jul 24 13:59:37 UTC 2023 x86_64
User : nobody ( 99)
PHP Version : 8.1.23
Disable Function : NONE
Directory :  /usr/share/doc/python-docs-2.7.5/html/library/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //usr/share/doc/python-docs-2.7.5/html/library/mailbox.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>18.4. mailbox — Manipulate mailboxes in various formats &mdash; Python 2.7.5 documentation</title>
    
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.7.5',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python 2.7.5 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python 2.7.5 documentation" href="../index.html" />
    <link rel="up" title="18. Internet Data Handling" href="netdata.html" />
    <link rel="next" title="18.5. mhlib — Access to MH mailboxes" href="mhlib.html" />
    <link rel="prev" title="18.3. mailcap — Mailcap file handling" href="mailcap.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="mhlib.html" title="18.5. mhlib — Access to MH mailboxes"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="mailcap.html" title="18.3. mailcap — Mailcap file handling"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="http://www.python.org/">Python</a> &raquo;</li>
        <li>
          <a href="../index.html">Python 2.7.5 documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="netdata.html" accesskey="U">18. Internet Data Handling</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-mailbox">
<span id="mailbox-manipulate-mailboxes-in-various-formats"></span><h1>18.4. <a class="reference internal" href="#module-mailbox" title="mailbox: Manipulate mailboxes in various formats"><tt class="xref py py-mod docutils literal"><span class="pre">mailbox</span></tt></a> &#8212; Manipulate mailboxes in various formats<a class="headerlink" href="#module-mailbox" title="Permalink to this headline">¶</a></h1>
<p>This module defines two classes, <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> and <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a>, for
accessing and manipulating on-disk mailboxes and the messages they contain.
<a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> offers a dictionary-like mapping from keys to messages.
<a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a> extends the <a class="reference internal" href="email.message.html#module-email.message" title="email.message: The base class representing email messages."><tt class="xref py py-mod docutils literal"><span class="pre">email.message</span></tt></a> module&#8217;s
<a class="reference internal" href="email.message.html#email.message.Message" title="email.message.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a> class with format-specific state and behavior.
Supported mailbox formats are
Maildir, mbox, MH, Babyl, and MMDF.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt>Module <a class="reference internal" href="email.html#module-email" title="email: Package supporting the parsing, manipulating, and generating email messages, including MIME documents."><tt class="xref py py-mod docutils literal"><span class="pre">email</span></tt></a></dt>
<dd>Represent and manipulate messages.</dd>
</dl>
</div>
<div class="section" id="mailbox-objects">
<span id="id1"></span><h2>18.4.1. <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> objects<a class="headerlink" href="#mailbox-objects" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="mailbox.Mailbox">
<em class="property">class </em><tt class="descclassname">mailbox.</tt><tt class="descname">Mailbox</tt><a class="headerlink" href="#mailbox.Mailbox" title="Permalink to this definition">¶</a></dt>
<dd><p>A mailbox, which may be inspected and modified.</p>
<p>The <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> class defines an interface and is not intended to be
instantiated.  Instead, format-specific subclasses should inherit from
<a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> and your code should instantiate a particular subclass.</p>
<p>The <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> interface is dictionary-like, with small keys
corresponding to messages. Keys are issued by the <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> instance
with which they will be used and are only meaningful to that <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a>
instance. A key continues to identify a message even if the corresponding
message is modified, such as by replacing it with another message.</p>
<p>Messages may be added to a <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> instance using the set-like
method <a class="reference internal" href="#mailbox.Mailbox.add" title="mailbox.Mailbox.add"><tt class="xref py py-meth docutils literal"><span class="pre">add()</span></tt></a> and removed using a <tt class="docutils literal"><span class="pre">del</span></tt> statement or the set-like
methods <a class="reference internal" href="#mailbox.Mailbox.remove" title="mailbox.Mailbox.remove"><tt class="xref py py-meth docutils literal"><span class="pre">remove()</span></tt></a> and <a class="reference internal" href="#mailbox.Mailbox.discard" title="mailbox.Mailbox.discard"><tt class="xref py py-meth docutils literal"><span class="pre">discard()</span></tt></a>.</p>
<p><a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> interface semantics differ from dictionary semantics in some
noteworthy ways. Each time a message is requested, a new representation
(typically a <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a> instance) is generated based upon the current
state of the mailbox. Similarly, when a message is added to a
<a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> instance, the provided message representation&#8217;s contents are
copied. In neither case is a reference to the message representation kept by
the <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> instance.</p>
<p>The default <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> iterator iterates over message representations,
not keys as the default dictionary iterator does. Moreover, modification of a
mailbox during iteration is safe and well-defined. Messages added to the
mailbox after an iterator is created will not be seen by the
iterator. Messages removed from the mailbox before the iterator yields them
will be silently skipped, though using a key from an iterator may result in a
<a class="reference internal" href="exceptions.html#exceptions.KeyError" title="exceptions.KeyError"><tt class="xref py py-exc docutils literal"><span class="pre">KeyError</span></tt></a> exception if the corresponding message is subsequently
removed.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">Be very cautious when modifying mailboxes that might be simultaneously
changed by some other process.  The safest mailbox format to use for such
tasks is Maildir; try to avoid using single-file formats such as mbox for
concurrent writing.  If you&#8217;re modifying a mailbox, you <em>must</em> lock it by
calling the <a class="reference internal" href="#mailbox.Mailbox.lock" title="mailbox.Mailbox.lock"><tt class="xref py py-meth docutils literal"><span class="pre">lock()</span></tt></a> and <a class="reference internal" href="#mailbox.Mailbox.unlock" title="mailbox.Mailbox.unlock"><tt class="xref py py-meth docutils literal"><span class="pre">unlock()</span></tt></a> methods <em>before</em> reading any
messages in the file or making any changes by adding or deleting a
message.  Failing to lock the mailbox runs the risk of losing messages or
corrupting the entire mailbox.</p>
</div>
<p><a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> instances have the following methods:</p>
<dl class="method">
<dt id="mailbox.Mailbox.add">
<tt class="descname">add</tt><big>(</big><em>message</em><big>)</big><a class="headerlink" href="#mailbox.Mailbox.add" title="Permalink to this definition">¶</a></dt>
<dd><p>Add <em>message</em> to the mailbox and return the key that has been assigned to
it.</p>
<p>Parameter <em>message</em> may be a <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a> instance, an
<a class="reference internal" href="email.message.html#email.message.Message" title="email.message.Message"><tt class="xref py py-class docutils literal"><span class="pre">email.message.Message</span></tt></a> instance, a string, or a file-like object
(which should be open in text mode). If <em>message</em> is an instance of the
appropriate format-specific <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a> subclass (e.g., if it&#8217;s an
<a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><tt class="xref py py-class docutils literal"><span class="pre">mboxMessage</span></tt></a> instance and this is an <a class="reference internal" href="#mailbox.mbox" title="mailbox.mbox"><tt class="xref py py-class docutils literal"><span class="pre">mbox</span></tt></a> instance), its
format-specific information is used. Otherwise, reasonable defaults for
format-specific information are used.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.Mailbox.remove">
<tt class="descname">remove</tt><big>(</big><em>key</em><big>)</big><a class="headerlink" href="#mailbox.Mailbox.remove" title="Permalink to this definition">¶</a></dt>
<dt id="mailbox.Mailbox.__delitem__">
<tt class="descname">__delitem__</tt><big>(</big><em>key</em><big>)</big><a class="headerlink" href="#mailbox.Mailbox.__delitem__" title="Permalink to this definition">¶</a></dt>
<dt id="mailbox.Mailbox.discard">
<tt class="descname">discard</tt><big>(</big><em>key</em><big>)</big><a class="headerlink" href="#mailbox.Mailbox.discard" title="Permalink to this definition">¶</a></dt>
<dd><p>Delete the message corresponding to <em>key</em> from the mailbox.</p>
<p>If no such message exists, a <a class="reference internal" href="exceptions.html#exceptions.KeyError" title="exceptions.KeyError"><tt class="xref py py-exc docutils literal"><span class="pre">KeyError</span></tt></a> exception is raised if the
method was called as <a class="reference internal" href="#mailbox.Mailbox.remove" title="mailbox.Mailbox.remove"><tt class="xref py py-meth docutils literal"><span class="pre">remove()</span></tt></a> or <a class="reference internal" href="#mailbox.Mailbox.__delitem__" title="mailbox.Mailbox.__delitem__"><tt class="xref py py-meth docutils literal"><span class="pre">__delitem__()</span></tt></a> but no
exception is raised if the method was called as <a class="reference internal" href="#mailbox.Mailbox.discard" title="mailbox.Mailbox.discard"><tt class="xref py py-meth docutils literal"><span class="pre">discard()</span></tt></a>. The
behavior of <a class="reference internal" href="#mailbox.Mailbox.discard" title="mailbox.Mailbox.discard"><tt class="xref py py-meth docutils literal"><span class="pre">discard()</span></tt></a> may be preferred if the underlying mailbox
format supports concurrent modification by other processes.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.Mailbox.__setitem__">
<tt class="descname">__setitem__</tt><big>(</big><em>key</em>, <em>message</em><big>)</big><a class="headerlink" href="#mailbox.Mailbox.__setitem__" title="Permalink to this definition">¶</a></dt>
<dd><p>Replace the message corresponding to <em>key</em> with <em>message</em>. Raise a
<a class="reference internal" href="exceptions.html#exceptions.KeyError" title="exceptions.KeyError"><tt class="xref py py-exc docutils literal"><span class="pre">KeyError</span></tt></a> exception if no message already corresponds to <em>key</em>.</p>
<p>As with <a class="reference internal" href="#mailbox.Mailbox.add" title="mailbox.Mailbox.add"><tt class="xref py py-meth docutils literal"><span class="pre">add()</span></tt></a>, parameter <em>message</em> may be a <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a>
instance, an <a class="reference internal" href="email.message.html#email.message.Message" title="email.message.Message"><tt class="xref py py-class docutils literal"><span class="pre">email.message.Message</span></tt></a> instance, a string, or a
file-like object (which should be open in text mode). If <em>message</em> is an
instance of the appropriate format-specific <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a> subclass
(e.g., if it&#8217;s an <a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><tt class="xref py py-class docutils literal"><span class="pre">mboxMessage</span></tt></a> instance and this is an
<a class="reference internal" href="#mailbox.mbox" title="mailbox.mbox"><tt class="xref py py-class docutils literal"><span class="pre">mbox</span></tt></a> instance), its format-specific information is
used. Otherwise, the format-specific information of the message that
currently corresponds to <em>key</em> is left unchanged.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.Mailbox.iterkeys">
<tt class="descname">iterkeys</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.Mailbox.iterkeys" title="Permalink to this definition">¶</a></dt>
<dt id="mailbox.Mailbox.keys">
<tt class="descname">keys</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.Mailbox.keys" title="Permalink to this definition">¶</a></dt>
<dd><p>Return an iterator over all keys if called as <a class="reference internal" href="#mailbox.Mailbox.iterkeys" title="mailbox.Mailbox.iterkeys"><tt class="xref py py-meth docutils literal"><span class="pre">iterkeys()</span></tt></a> or return a
list of keys if called as <a class="reference internal" href="#mailbox.Mailbox.keys" title="mailbox.Mailbox.keys"><tt class="xref py py-meth docutils literal"><span class="pre">keys()</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.Mailbox.itervalues">
<tt class="descname">itervalues</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.Mailbox.itervalues" title="Permalink to this definition">¶</a></dt>
<dt id="mailbox.Mailbox.__iter__">
<tt class="descname">__iter__</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.Mailbox.__iter__" title="Permalink to this definition">¶</a></dt>
<dt id="mailbox.Mailbox.values">
<tt class="descname">values</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.Mailbox.values" title="Permalink to this definition">¶</a></dt>
<dd><p>Return an iterator over representations of all messages if called as
<a class="reference internal" href="#mailbox.Mailbox.itervalues" title="mailbox.Mailbox.itervalues"><tt class="xref py py-meth docutils literal"><span class="pre">itervalues()</span></tt></a> or <a class="reference internal" href="#mailbox.Mailbox.__iter__" title="mailbox.Mailbox.__iter__"><tt class="xref py py-meth docutils literal"><span class="pre">__iter__()</span></tt></a> or return a list of such
representations if called as <a class="reference internal" href="#mailbox.Mailbox.values" title="mailbox.Mailbox.values"><tt class="xref py py-meth docutils literal"><span class="pre">values()</span></tt></a>. The messages are represented
as instances of the appropriate format-specific <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a> subclass
unless a custom message factory was specified when the <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a>
instance was initialized.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The behavior of <a class="reference internal" href="#mailbox.Mailbox.__iter__" title="mailbox.Mailbox.__iter__"><tt class="xref py py-meth docutils literal"><span class="pre">__iter__()</span></tt></a> is unlike that of dictionaries, which
iterate over keys.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="mailbox.Mailbox.iteritems">
<tt class="descname">iteritems</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.Mailbox.iteritems" title="Permalink to this definition">¶</a></dt>
<dt id="mailbox.Mailbox.items">
<tt class="descname">items</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.Mailbox.items" title="Permalink to this definition">¶</a></dt>
<dd><p>Return an iterator over (<em>key</em>, <em>message</em>) pairs, where <em>key</em> is a key and
<em>message</em> is a message representation, if called as <a class="reference internal" href="#mailbox.Mailbox.iteritems" title="mailbox.Mailbox.iteritems"><tt class="xref py py-meth docutils literal"><span class="pre">iteritems()</span></tt></a> or
return a list of such pairs if called as <a class="reference internal" href="#mailbox.Mailbox.items" title="mailbox.Mailbox.items"><tt class="xref py py-meth docutils literal"><span class="pre">items()</span></tt></a>. The messages are
represented as instances of the appropriate format-specific
<a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a> subclass unless a custom message factory was specified
when the <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> instance was initialized.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.Mailbox.get">
<tt class="descname">get</tt><big>(</big><em>key</em>, <em>default=None</em><big>)</big><a class="headerlink" href="#mailbox.Mailbox.get" title="Permalink to this definition">¶</a></dt>
<dt id="mailbox.Mailbox.__getitem__">
<tt class="descname">__getitem__</tt><big>(</big><em>key</em><big>)</big><a class="headerlink" href="#mailbox.Mailbox.__getitem__" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a representation of the message corresponding to <em>key</em>. If no such
message exists, <em>default</em> is returned if the method was called as
<a class="reference internal" href="#mailbox.Mailbox.get" title="mailbox.Mailbox.get"><tt class="xref py py-meth docutils literal"><span class="pre">get()</span></tt></a> and a <a class="reference internal" href="exceptions.html#exceptions.KeyError" title="exceptions.KeyError"><tt class="xref py py-exc docutils literal"><span class="pre">KeyError</span></tt></a> exception is raised if the method was
called as <a class="reference internal" href="#mailbox.Mailbox.__getitem__" title="mailbox.Mailbox.__getitem__"><tt class="xref py py-meth docutils literal"><span class="pre">__getitem__()</span></tt></a>. The message is represented as an instance
of the appropriate format-specific <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a> subclass unless a
custom message factory was specified when the <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> instance
was initialized.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.Mailbox.get_message">
<tt class="descname">get_message</tt><big>(</big><em>key</em><big>)</big><a class="headerlink" href="#mailbox.Mailbox.get_message" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a representation of the message corresponding to <em>key</em> as an
instance of the appropriate format-specific <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a> subclass, or
raise a <a class="reference internal" href="exceptions.html#exceptions.KeyError" title="exceptions.KeyError"><tt class="xref py py-exc docutils literal"><span class="pre">KeyError</span></tt></a> exception if no such message exists.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.Mailbox.get_string">
<tt class="descname">get_string</tt><big>(</big><em>key</em><big>)</big><a class="headerlink" href="#mailbox.Mailbox.get_string" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a string representation of the message corresponding to <em>key</em>, or
raise a <a class="reference internal" href="exceptions.html#exceptions.KeyError" title="exceptions.KeyError"><tt class="xref py py-exc docutils literal"><span class="pre">KeyError</span></tt></a> exception if no such message exists.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.Mailbox.get_file">
<tt class="descname">get_file</tt><big>(</big><em>key</em><big>)</big><a class="headerlink" href="#mailbox.Mailbox.get_file" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a file-like representation of the message corresponding to <em>key</em>,
or raise a <a class="reference internal" href="exceptions.html#exceptions.KeyError" title="exceptions.KeyError"><tt class="xref py py-exc docutils literal"><span class="pre">KeyError</span></tt></a> exception if no such message exists. The
file-like object behaves as if open in binary mode. This file should be
closed once it is no longer needed.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Unlike other representations of messages, file-like representations are
not necessarily independent of the <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> instance that
created them or of the underlying mailbox. More specific documentation
is provided by each subclass.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="mailbox.Mailbox.has_key">
<tt class="descname">has_key</tt><big>(</big><em>key</em><big>)</big><a class="headerlink" href="#mailbox.Mailbox.has_key" title="Permalink to this definition">¶</a></dt>
<dt id="mailbox.Mailbox.__contains__">
<tt class="descname">__contains__</tt><big>(</big><em>key</em><big>)</big><a class="headerlink" href="#mailbox.Mailbox.__contains__" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <tt class="docutils literal"><span class="pre">True</span></tt> if <em>key</em> corresponds to a message, <tt class="docutils literal"><span class="pre">False</span></tt> otherwise.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.Mailbox.__len__">
<tt class="descname">__len__</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.Mailbox.__len__" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a count of messages in the mailbox.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.Mailbox.clear">
<tt class="descname">clear</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.Mailbox.clear" title="Permalink to this definition">¶</a></dt>
<dd><p>Delete all messages from the mailbox.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.Mailbox.pop">
<tt class="descname">pop</tt><big>(</big><em>key</em><span class="optional">[</span>, <em>default</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#mailbox.Mailbox.pop" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a representation of the message corresponding to <em>key</em> and delete
the message. If no such message exists, return <em>default</em> if it was
supplied or else raise a <a class="reference internal" href="exceptions.html#exceptions.KeyError" title="exceptions.KeyError"><tt class="xref py py-exc docutils literal"><span class="pre">KeyError</span></tt></a> exception. The message is
represented as an instance of the appropriate format-specific
<a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a> subclass unless a custom message factory was specified
when the <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> instance was initialized.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.Mailbox.popitem">
<tt class="descname">popitem</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.Mailbox.popitem" title="Permalink to this definition">¶</a></dt>
<dd><p>Return an arbitrary (<em>key</em>, <em>message</em>) pair, where <em>key</em> is a key and
<em>message</em> is a message representation, and delete the corresponding
message. If the mailbox is empty, raise a <a class="reference internal" href="exceptions.html#exceptions.KeyError" title="exceptions.KeyError"><tt class="xref py py-exc docutils literal"><span class="pre">KeyError</span></tt></a> exception. The
message is represented as an instance of the appropriate format-specific
<a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a> subclass unless a custom message factory was specified
when the <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> instance was initialized.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.Mailbox.update">
<tt class="descname">update</tt><big>(</big><em>arg</em><big>)</big><a class="headerlink" href="#mailbox.Mailbox.update" title="Permalink to this definition">¶</a></dt>
<dd><p>Parameter <em>arg</em> should be a <em>key</em>-to-<em>message</em> mapping or an iterable of
(<em>key</em>, <em>message</em>) pairs. Updates the mailbox so that, for each given
<em>key</em> and <em>message</em>, the message corresponding to <em>key</em> is set to
<em>message</em> as if by using <a class="reference internal" href="#mailbox.Mailbox.__setitem__" title="mailbox.Mailbox.__setitem__"><tt class="xref py py-meth docutils literal"><span class="pre">__setitem__()</span></tt></a>. As with <a class="reference internal" href="#mailbox.Mailbox.__setitem__" title="mailbox.Mailbox.__setitem__"><tt class="xref py py-meth docutils literal"><span class="pre">__setitem__()</span></tt></a>,
each <em>key</em> must already correspond to a message in the mailbox or else a
<a class="reference internal" href="exceptions.html#exceptions.KeyError" title="exceptions.KeyError"><tt class="xref py py-exc docutils literal"><span class="pre">KeyError</span></tt></a> exception will be raised, so in general it is incorrect
for <em>arg</em> to be a <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> instance.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Unlike with dictionaries, keyword arguments are not supported.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="mailbox.Mailbox.flush">
<tt class="descname">flush</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.Mailbox.flush" title="Permalink to this definition">¶</a></dt>
<dd><p>Write any pending changes to the filesystem. For some <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a>
subclasses, changes are always written immediately and <a class="reference internal" href="#mailbox.Mailbox.flush" title="mailbox.Mailbox.flush"><tt class="xref py py-meth docutils literal"><span class="pre">flush()</span></tt></a> does
nothing, but you should still make a habit of calling this method.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.Mailbox.lock">
<tt class="descname">lock</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.Mailbox.lock" title="Permalink to this definition">¶</a></dt>
<dd><p>Acquire an exclusive advisory lock on the mailbox so that other processes
know not to modify it. An <a class="reference internal" href="#mailbox.ExternalClashError" title="mailbox.ExternalClashError"><tt class="xref py py-exc docutils literal"><span class="pre">ExternalClashError</span></tt></a> is raised if the lock
is not available. The particular locking mechanisms used depend upon the
mailbox format.  You should <em>always</em> lock the mailbox before making any
modifications to its contents.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.Mailbox.unlock">
<tt class="descname">unlock</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.Mailbox.unlock" title="Permalink to this definition">¶</a></dt>
<dd><p>Release the lock on the mailbox, if any.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.Mailbox.close">
<tt class="descname">close</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.Mailbox.close" title="Permalink to this definition">¶</a></dt>
<dd><p>Flush the mailbox, unlock it if necessary, and close any open files. For
some <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> subclasses, this method does nothing.</p>
</dd></dl>

</dd></dl>

<div class="section" id="maildir">
<span id="mailbox-maildir"></span><h3>18.4.1.1. <a class="reference internal" href="#mailbox.Maildir" title="mailbox.Maildir"><tt class="xref py py-class docutils literal"><span class="pre">Maildir</span></tt></a><a class="headerlink" href="#maildir" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="mailbox.Maildir">
<em class="property">class </em><tt class="descclassname">mailbox.</tt><tt class="descname">Maildir</tt><big>(</big><em>dirname</em>, <em>factory=rfc822.Message</em>, <em>create=True</em><big>)</big><a class="headerlink" href="#mailbox.Maildir" title="Permalink to this definition">¶</a></dt>
<dd><p>A subclass of <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> for mailboxes in Maildir format. Parameter
<em>factory</em> is a callable object that accepts a file-like message representation
(which behaves as if opened in binary mode) and returns a custom representation.
If <em>factory</em> is <tt class="docutils literal"><span class="pre">None</span></tt>, <a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><tt class="xref py py-class docutils literal"><span class="pre">MaildirMessage</span></tt></a> is used as the default message
representation. If <em>create</em> is <tt class="docutils literal"><span class="pre">True</span></tt>, the mailbox is created if it does not
exist.</p>
<p>It is for historical reasons that <em>factory</em> defaults to <a class="reference internal" href="rfc822.html#rfc822.Message" title="rfc822.Message"><tt class="xref py py-class docutils literal"><span class="pre">rfc822.Message</span></tt></a>
and that <em>dirname</em> is named as such rather than <em>path</em>. For a <a class="reference internal" href="#mailbox.Maildir" title="mailbox.Maildir"><tt class="xref py py-class docutils literal"><span class="pre">Maildir</span></tt></a>
instance that behaves like instances of other <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> subclasses, set
<em>factory</em> to <tt class="docutils literal"><span class="pre">None</span></tt>.</p>
<p>Maildir is a directory-based mailbox format invented for the qmail mail
transfer agent and now widely supported by other programs. Messages in a
Maildir mailbox are stored in separate files within a common directory
structure. This design allows Maildir mailboxes to be accessed and modified
by multiple unrelated programs without data corruption, so file locking is
unnecessary.</p>
<p>Maildir mailboxes contain three subdirectories, namely: <tt class="file docutils literal"><span class="pre">tmp</span></tt>,
<tt class="file docutils literal"><span class="pre">new</span></tt>, and <tt class="file docutils literal"><span class="pre">cur</span></tt>. Messages are created momentarily in the
<tt class="file docutils literal"><span class="pre">tmp</span></tt> subdirectory and then moved to the <tt class="file docutils literal"><span class="pre">new</span></tt> subdirectory to
finalize delivery. A mail user agent may subsequently move the message to the
<tt class="file docutils literal"><span class="pre">cur</span></tt> subdirectory and store information about the state of the message
in a special &#8220;info&#8221; section appended to its file name.</p>
<p>Folders of the style introduced by the Courier mail transfer agent are also
supported. Any subdirectory of the main mailbox is considered a folder if
<tt class="docutils literal"><span class="pre">'.'</span></tt> is the first character in its name. Folder names are represented by
<a class="reference internal" href="#mailbox.Maildir" title="mailbox.Maildir"><tt class="xref py py-class docutils literal"><span class="pre">Maildir</span></tt></a> without the leading <tt class="docutils literal"><span class="pre">'.'</span></tt>. Each folder is itself a Maildir
mailbox but should not contain other folders. Instead, a logical nesting is
indicated using <tt class="docutils literal"><span class="pre">'.'</span></tt> to delimit levels, e.g., &#8220;Archived.2005.07&#8221;.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>The Maildir specification requires the use of a colon (<tt class="docutils literal"><span class="pre">':'</span></tt>) in certain
message file names. However, some operating systems do not permit this
character in file names, If you wish to use a Maildir-like format on such
an operating system, you should specify another character to use
instead. The exclamation point (<tt class="docutils literal"><span class="pre">'!'</span></tt>) is a popular choice. For
example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">mailbox</span>
<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="s">&#39;!&#39;</span>
</pre></div>
</div>
<p class="last">The <tt class="xref py py-attr docutils literal"><span class="pre">colon</span></tt> attribute may also be set on a per-instance basis.</p>
</div>
<p><a class="reference internal" href="#mailbox.Maildir" title="mailbox.Maildir"><tt class="xref py py-class docutils literal"><span class="pre">Maildir</span></tt></a> instances have all of the methods of <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> in
addition to the following:</p>
<dl class="method">
<dt id="mailbox.Maildir.list_folders">
<tt class="descname">list_folders</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.Maildir.list_folders" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a list of the names of all folders.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.Maildir.get_folder">
<tt class="descname">get_folder</tt><big>(</big><em>folder</em><big>)</big><a class="headerlink" href="#mailbox.Maildir.get_folder" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a <a class="reference internal" href="#mailbox.Maildir" title="mailbox.Maildir"><tt class="xref py py-class docutils literal"><span class="pre">Maildir</span></tt></a> instance representing the folder whose name is
<em>folder</em>. A <a class="reference internal" href="#mailbox.NoSuchMailboxError" title="mailbox.NoSuchMailboxError"><tt class="xref py py-exc docutils literal"><span class="pre">NoSuchMailboxError</span></tt></a> exception is raised if the folder
does not exist.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.Maildir.add_folder">
<tt class="descname">add_folder</tt><big>(</big><em>folder</em><big>)</big><a class="headerlink" href="#mailbox.Maildir.add_folder" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a folder whose name is <em>folder</em> and return a <a class="reference internal" href="#mailbox.Maildir" title="mailbox.Maildir"><tt class="xref py py-class docutils literal"><span class="pre">Maildir</span></tt></a>
instance representing it.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.Maildir.remove_folder">
<tt class="descname">remove_folder</tt><big>(</big><em>folder</em><big>)</big><a class="headerlink" href="#mailbox.Maildir.remove_folder" title="Permalink to this definition">¶</a></dt>
<dd><p>Delete the folder whose name is <em>folder</em>. If the folder contains any
messages, a <a class="reference internal" href="#mailbox.NotEmptyError" title="mailbox.NotEmptyError"><tt class="xref py py-exc docutils literal"><span class="pre">NotEmptyError</span></tt></a> exception will be raised and the folder
will not be deleted.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.Maildir.clean">
<tt class="descname">clean</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.Maildir.clean" title="Permalink to this definition">¶</a></dt>
<dd><p>Delete temporary files from the mailbox that have not been accessed in the
last 36 hours. The Maildir specification says that mail-reading programs
should do this occasionally.</p>
</dd></dl>

<p>Some <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> methods implemented by <a class="reference internal" href="#mailbox.Maildir" title="mailbox.Maildir"><tt class="xref py py-class docutils literal"><span class="pre">Maildir</span></tt></a> deserve special
remarks:</p>
<dl class="method">
<dt id="mailbox.Maildir.add">
<tt class="descname">add</tt><big>(</big><em>message</em><big>)</big><a class="headerlink" href="#mailbox.Maildir.add" title="Permalink to this definition">¶</a></dt>
<dt id="mailbox.Maildir.__setitem__">
<tt class="descname">__setitem__</tt><big>(</big><em>key</em>, <em>message</em><big>)</big><a class="headerlink" href="#mailbox.Maildir.__setitem__" title="Permalink to this definition">¶</a></dt>
<dt id="mailbox.Maildir.update">
<tt class="descname">update</tt><big>(</big><em>arg</em><big>)</big><a class="headerlink" href="#mailbox.Maildir.update" title="Permalink to this definition">¶</a></dt>
<dd><div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">These methods generate unique file names based upon the current process
ID. When using multiple threads, undetected name clashes may occur and
cause corruption of the mailbox unless threads are coordinated to avoid
using these methods to manipulate the same mailbox simultaneously.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="mailbox.Maildir.flush">
<tt class="descname">flush</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.Maildir.flush" title="Permalink to this definition">¶</a></dt>
<dd><p>All changes to Maildir mailboxes are immediately applied, so this method
does nothing.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.Maildir.lock">
<tt class="descname">lock</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.Maildir.lock" title="Permalink to this definition">¶</a></dt>
<dt id="mailbox.Maildir.unlock">
<tt class="descname">unlock</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.Maildir.unlock" title="Permalink to this definition">¶</a></dt>
<dd><p>Maildir mailboxes do not support (or require) locking, so these methods do
nothing.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.Maildir.close">
<tt class="descname">close</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.Maildir.close" title="Permalink to this definition">¶</a></dt>
<dd><p><a class="reference internal" href="#mailbox.Maildir" title="mailbox.Maildir"><tt class="xref py py-class docutils literal"><span class="pre">Maildir</span></tt></a> instances do not keep any open files and the underlying
mailboxes do not support locking, so this method does nothing.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.Maildir.get_file">
<tt class="descname">get_file</tt><big>(</big><em>key</em><big>)</big><a class="headerlink" href="#mailbox.Maildir.get_file" title="Permalink to this definition">¶</a></dt>
<dd><p>Depending upon the host platform, it may not be possible to modify or
remove the underlying message while the returned file remains open.</p>
</dd></dl>

</dd></dl>

<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><a class="reference external" href="http://www.qmail.org/man/man5/maildir.html">maildir man page from qmail</a></dt>
<dd>The original specification of the format.</dd>
<dt><a class="reference external" href="http://cr.yp.to/proto/maildir.html">Using maildir format</a></dt>
<dd>Notes on Maildir by its inventor. Includes an updated name-creation scheme and
details on &#8220;info&#8221; semantics.</dd>
<dt><a class="reference external" href="http://www.courier-mta.org/maildir.html">maildir man page from Courier</a></dt>
<dd>Another specification of the format. Describes a common extension for supporting
folders.</dd>
</dl>
</div>
</div>
<div class="section" id="mbox">
<span id="mailbox-mbox"></span><h3>18.4.1.2. <a class="reference internal" href="#mailbox.mbox" title="mailbox.mbox"><tt class="xref py py-class docutils literal"><span class="pre">mbox</span></tt></a><a class="headerlink" href="#mbox" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="mailbox.mbox">
<em class="property">class </em><tt class="descclassname">mailbox.</tt><tt class="descname">mbox</tt><big>(</big><em>path</em>, <em>factory=None</em>, <em>create=True</em><big>)</big><a class="headerlink" href="#mailbox.mbox" title="Permalink to this definition">¶</a></dt>
<dd><p>A subclass of <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> for mailboxes in mbox format. Parameter <em>factory</em>
is a callable object that accepts a file-like message representation (which
behaves as if opened in binary mode) and returns a custom representation. If
<em>factory</em> is <tt class="docutils literal"><span class="pre">None</span></tt>, <a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><tt class="xref py py-class docutils literal"><span class="pre">mboxMessage</span></tt></a> is used as the default message
representation. If <em>create</em> is <tt class="docutils literal"><span class="pre">True</span></tt>, the mailbox is created if it does not
exist.</p>
<p>The mbox format is the classic format for storing mail on Unix systems. All
messages in an mbox mailbox are stored in a single file with the beginning of
each message indicated by a line whose first five characters are &#8220;From &#8221;.</p>
<p>Several variations of the mbox format exist to address perceived shortcomings in
the original. In the interest of compatibility, <a class="reference internal" href="#mailbox.mbox" title="mailbox.mbox"><tt class="xref py py-class docutils literal"><span class="pre">mbox</span></tt></a> implements the
original format, which is sometimes referred to as <em class="dfn">mboxo</em>. This means that
the <em class="mailheader">Content-Length</em> header, if present, is ignored and that any
occurrences of &#8220;From &#8221; at the beginning of a line in a message body are
transformed to &#8220;&gt;From &#8221; when storing the message, although occurrences of &#8220;&gt;From
&#8221; are not transformed to &#8220;From &#8221; when reading the message.</p>
<p>Some <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> methods implemented by <a class="reference internal" href="#mailbox.mbox" title="mailbox.mbox"><tt class="xref py py-class docutils literal"><span class="pre">mbox</span></tt></a> deserve special
remarks:</p>
<dl class="method">
<dt id="mailbox.mbox.get_file">
<tt class="descname">get_file</tt><big>(</big><em>key</em><big>)</big><a class="headerlink" href="#mailbox.mbox.get_file" title="Permalink to this definition">¶</a></dt>
<dd><p>Using the file after calling <tt class="xref py py-meth docutils literal"><span class="pre">flush()</span></tt> or <tt class="xref py py-meth docutils literal"><span class="pre">close()</span></tt> on the
<a class="reference internal" href="#mailbox.mbox" title="mailbox.mbox"><tt class="xref py py-class docutils literal"><span class="pre">mbox</span></tt></a> instance may yield unpredictable results or raise an
exception.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.mbox.lock">
<tt class="descname">lock</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.mbox.lock" title="Permalink to this definition">¶</a></dt>
<dt id="mailbox.mbox.unlock">
<tt class="descname">unlock</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.mbox.unlock" title="Permalink to this definition">¶</a></dt>
<dd><p>Three locking mechanisms are used&#8212;dot locking and, if available, the
<tt class="xref c c-func docutils literal"><span class="pre">flock()</span></tt> and <tt class="xref c c-func docutils literal"><span class="pre">lockf()</span></tt> system calls.</p>
</dd></dl>

</dd></dl>

<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><a class="reference external" href="http://www.qmail.org/man/man5/mbox.html">mbox man page from qmail</a></dt>
<dd>A specification of the format and its variations.</dd>
<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>Another specification of the format, with details on locking.</dd>
<dt><a class="reference external" href="http://www.jwz.org/doc/content-length.html">Configuring Netscape Mail on Unix: Why The Content-Length Format is Bad</a></dt>
<dd>An argument for using the original mbox format rather than a variation.</dd>
<dt><a class="reference external" href="http://homepages.tesco.net./~J.deBoynePollard/FGA/mail-mbox-formats.html">&#8220;mbox&#8221; is a family of several mutually incompatible mailbox formats</a></dt>
<dd>A history of mbox variations.</dd>
</dl>
</div>
</div>
<div class="section" id="mh">
<span id="mailbox-mh"></span><h3>18.4.1.3. <a class="reference internal" href="#mailbox.MH" title="mailbox.MH"><tt class="xref py py-class docutils literal"><span class="pre">MH</span></tt></a><a class="headerlink" href="#mh" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="mailbox.MH">
<em class="property">class </em><tt class="descclassname">mailbox.</tt><tt class="descname">MH</tt><big>(</big><em>path</em>, <em>factory=None</em>, <em>create=True</em><big>)</big><a class="headerlink" href="#mailbox.MH" title="Permalink to this definition">¶</a></dt>
<dd><p>A subclass of <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> for mailboxes in MH format. Parameter <em>factory</em>
is a callable object that accepts a file-like message representation (which
behaves as if opened in binary mode) and returns a custom representation. If
<em>factory</em> is <tt class="docutils literal"><span class="pre">None</span></tt>, <a class="reference internal" href="#mailbox.MHMessage" title="mailbox.MHMessage"><tt class="xref py py-class docutils literal"><span class="pre">MHMessage</span></tt></a> is used as the default message
representation. If <em>create</em> is <tt class="docutils literal"><span class="pre">True</span></tt>, the mailbox is created if it does not
exist.</p>
<p>MH is a directory-based mailbox format invented for the MH Message Handling
System, a mail user agent. Each message in an MH mailbox resides in its own
file. An MH mailbox may contain other MH mailboxes (called <em class="dfn">folders</em>) in
addition to messages. Folders may be nested indefinitely. MH mailboxes also
support <em class="dfn">sequences</em>, which are named lists used to logically group
messages without moving them to sub-folders. Sequences are defined in a file
called <tt class="file docutils literal"><span class="pre">.mh_sequences</span></tt> in each folder.</p>
<p>The <a class="reference internal" href="#mailbox.MH" title="mailbox.MH"><tt class="xref py py-class docutils literal"><span class="pre">MH</span></tt></a> class manipulates MH mailboxes, but it does not attempt to
emulate all of <strong class="program">mh</strong>&#8216;s behaviors. In particular, it does not modify
and is not affected by the <tt class="file docutils literal"><span class="pre">context</span></tt> or <tt class="file docutils literal"><span class="pre">.mh_profile</span></tt> files that
are used by <strong class="program">mh</strong> to store its state and configuration.</p>
<p><a class="reference internal" href="#mailbox.MH" title="mailbox.MH"><tt class="xref py py-class docutils literal"><span class="pre">MH</span></tt></a> instances have all of the methods of <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> in addition
to the following:</p>
<dl class="method">
<dt id="mailbox.MH.list_folders">
<tt class="descname">list_folders</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.MH.list_folders" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a list of the names of all folders.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MH.get_folder">
<tt class="descname">get_folder</tt><big>(</big><em>folder</em><big>)</big><a class="headerlink" href="#mailbox.MH.get_folder" title="Permalink to this definition">¶</a></dt>
<dd><p>Return an <a class="reference internal" href="#mailbox.MH" title="mailbox.MH"><tt class="xref py py-class docutils literal"><span class="pre">MH</span></tt></a> instance representing the folder whose name is
<em>folder</em>. A <a class="reference internal" href="#mailbox.NoSuchMailboxError" title="mailbox.NoSuchMailboxError"><tt class="xref py py-exc docutils literal"><span class="pre">NoSuchMailboxError</span></tt></a> exception is raised if the folder
does not exist.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MH.add_folder">
<tt class="descname">add_folder</tt><big>(</big><em>folder</em><big>)</big><a class="headerlink" href="#mailbox.MH.add_folder" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a folder whose name is <em>folder</em> and return an <a class="reference internal" href="#mailbox.MH" title="mailbox.MH"><tt class="xref py py-class docutils literal"><span class="pre">MH</span></tt></a> instance
representing it.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MH.remove_folder">
<tt class="descname">remove_folder</tt><big>(</big><em>folder</em><big>)</big><a class="headerlink" href="#mailbox.MH.remove_folder" title="Permalink to this definition">¶</a></dt>
<dd><p>Delete the folder whose name is <em>folder</em>. If the folder contains any
messages, a <a class="reference internal" href="#mailbox.NotEmptyError" title="mailbox.NotEmptyError"><tt class="xref py py-exc docutils literal"><span class="pre">NotEmptyError</span></tt></a> exception will be raised and the folder
will not be deleted.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MH.get_sequences">
<tt class="descname">get_sequences</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.MH.get_sequences" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a dictionary of sequence names mapped to key lists. If there are no
sequences, the empty dictionary is returned.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MH.set_sequences">
<tt class="descname">set_sequences</tt><big>(</big><em>sequences</em><big>)</big><a class="headerlink" href="#mailbox.MH.set_sequences" title="Permalink to this definition">¶</a></dt>
<dd><p>Re-define the sequences that exist in the mailbox based upon <em>sequences</em>,
a dictionary of names mapped to key lists, like returned by
<a class="reference internal" href="#mailbox.MH.get_sequences" title="mailbox.MH.get_sequences"><tt class="xref py py-meth docutils literal"><span class="pre">get_sequences()</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MH.pack">
<tt class="descname">pack</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.MH.pack" title="Permalink to this definition">¶</a></dt>
<dd><p>Rename messages in the mailbox as necessary to eliminate gaps in
numbering.  Entries in the sequences list are updated correspondingly.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Already-issued keys are invalidated by this operation and should not be
subsequently used.</p>
</div>
</dd></dl>

<p>Some <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> methods implemented by <a class="reference internal" href="#mailbox.MH" title="mailbox.MH"><tt class="xref py py-class docutils literal"><span class="pre">MH</span></tt></a> deserve special
remarks:</p>
<dl class="method">
<dt id="mailbox.MH.remove">
<tt class="descname">remove</tt><big>(</big><em>key</em><big>)</big><a class="headerlink" href="#mailbox.MH.remove" title="Permalink to this definition">¶</a></dt>
<dt id="mailbox.MH.__delitem__">
<tt class="descname">__delitem__</tt><big>(</big><em>key</em><big>)</big><a class="headerlink" href="#mailbox.MH.__delitem__" title="Permalink to this definition">¶</a></dt>
<dt id="mailbox.MH.discard">
<tt class="descname">discard</tt><big>(</big><em>key</em><big>)</big><a class="headerlink" href="#mailbox.MH.discard" title="Permalink to this definition">¶</a></dt>
<dd><p>These methods immediately delete the message. The MH convention of marking
a message for deletion by prepending a comma to its name is not used.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MH.lock">
<tt class="descname">lock</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.MH.lock" title="Permalink to this definition">¶</a></dt>
<dt id="mailbox.MH.unlock">
<tt class="descname">unlock</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.MH.unlock" title="Permalink to this definition">¶</a></dt>
<dd><p>Three locking mechanisms are used&#8212;dot locking and, if available, the
<tt class="xref c c-func docutils literal"><span class="pre">flock()</span></tt> and <tt class="xref c c-func docutils literal"><span class="pre">lockf()</span></tt> system calls. For MH mailboxes, locking
the mailbox means locking the <tt class="file docutils literal"><span class="pre">.mh_sequences</span></tt> file and, only for the
duration of any operations that affect them, locking individual message
files.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MH.get_file">
<tt class="descname">get_file</tt><big>(</big><em>key</em><big>)</big><a class="headerlink" href="#mailbox.MH.get_file" title="Permalink to this definition">¶</a></dt>
<dd><p>Depending upon the host platform, it may not be possible to remove the
underlying message while the returned file remains open.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MH.flush">
<tt class="descname">flush</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.MH.flush" title="Permalink to this definition">¶</a></dt>
<dd><p>All changes to MH mailboxes are immediately applied, so this method does
nothing.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MH.close">
<tt class="descname">close</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.MH.close" title="Permalink to this definition">¶</a></dt>
<dd><p><a class="reference internal" href="#mailbox.MH" title="mailbox.MH"><tt class="xref py py-class docutils literal"><span class="pre">MH</span></tt></a> instances do not keep any open files, so this method is
equivalent to <a class="reference internal" href="#mailbox.MH.unlock" title="mailbox.MH.unlock"><tt class="xref py py-meth docutils literal"><span class="pre">unlock()</span></tt></a>.</p>
</dd></dl>

</dd></dl>

<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><a class="reference external" href="http://www.nongnu.org/nmh/">nmh - Message Handling System</a></dt>
<dd>Home page of <strong class="program">nmh</strong>, an updated version of the original <strong class="program">mh</strong>.</dd>
<dt><a class="reference external" href="http://rand-mh.sourceforge.net/book/">MH &amp; nmh: Email for Users &amp; Programmers</a></dt>
<dd>A GPL-licensed book on <strong class="program">mh</strong> and <strong class="program">nmh</strong>, with some information
on the mailbox format.</dd>
</dl>
</div>
</div>
<div class="section" id="babyl">
<span id="mailbox-babyl"></span><h3>18.4.1.4. <a class="reference internal" href="#mailbox.Babyl" title="mailbox.Babyl"><tt class="xref py py-class docutils literal"><span class="pre">Babyl</span></tt></a><a class="headerlink" href="#babyl" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="mailbox.Babyl">
<em class="property">class </em><tt class="descclassname">mailbox.</tt><tt class="descname">Babyl</tt><big>(</big><em>path</em>, <em>factory=None</em>, <em>create=True</em><big>)</big><a class="headerlink" href="#mailbox.Babyl" title="Permalink to this definition">¶</a></dt>
<dd><p>A subclass of <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> for mailboxes in Babyl format. Parameter
<em>factory</em> is a callable object that accepts a file-like message representation
(which behaves as if opened in binary mode) and returns a custom representation.
If <em>factory</em> is <tt class="docutils literal"><span class="pre">None</span></tt>, <a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><tt class="xref py py-class docutils literal"><span class="pre">BabylMessage</span></tt></a> is used as the default message
representation. If <em>create</em> is <tt class="docutils literal"><span class="pre">True</span></tt>, the mailbox is created if it does not
exist.</p>
<p>Babyl is a single-file mailbox format used by the Rmail mail user agent
included with Emacs. The beginning of a message is indicated by a line
containing the two characters Control-Underscore (<tt class="docutils literal"><span class="pre">'\037'</span></tt>) and Control-L
(<tt class="docutils literal"><span class="pre">'\014'</span></tt>). The end of a message is indicated by the start of the next
message or, in the case of the last message, a line containing a
Control-Underscore (<tt class="docutils literal"><span class="pre">'\037'</span></tt>) character.</p>
<p>Messages in a Babyl mailbox have two sets of headers, original headers and
so-called visible headers. Visible headers are typically a subset of the
original headers that have been reformatted or abridged to be more
attractive. Each message in a Babyl mailbox also has an accompanying list of
<em class="dfn">labels</em>, or short strings that record extra information about the
message, and a list of all user-defined labels found in the mailbox is kept
in the Babyl options section.</p>
<p><a class="reference internal" href="#mailbox.Babyl" title="mailbox.Babyl"><tt class="xref py py-class docutils literal"><span class="pre">Babyl</span></tt></a> instances have all of the methods of <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> in
addition to the following:</p>
<dl class="method">
<dt id="mailbox.Babyl.get_labels">
<tt class="descname">get_labels</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.Babyl.get_labels" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a list of the names of all user-defined labels used in the mailbox.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The actual messages are inspected to determine which labels exist in
the mailbox rather than consulting the list of labels in the Babyl
options section, but the Babyl section is updated whenever the mailbox
is modified.</p>
</div>
</dd></dl>

<p>Some <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> methods implemented by <a class="reference internal" href="#mailbox.Babyl" title="mailbox.Babyl"><tt class="xref py py-class docutils literal"><span class="pre">Babyl</span></tt></a> deserve special
remarks:</p>
<dl class="method">
<dt id="mailbox.Babyl.get_file">
<tt class="descname">get_file</tt><big>(</big><em>key</em><big>)</big><a class="headerlink" href="#mailbox.Babyl.get_file" title="Permalink to this definition">¶</a></dt>
<dd><p>In Babyl mailboxes, the headers of a message are not stored contiguously
with the body of the message. To generate a file-like representation, the
headers and body are copied together into a <a class="reference internal" href="stringio.html#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-class docutils literal"><span class="pre">StringIO</span></tt></a> instance
(from the <a class="reference internal" href="stringio.html#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-mod docutils literal"><span class="pre">StringIO</span></tt></a> module), which has an API identical to that of a
file. As a result, the file-like object is truly independent of the
underlying mailbox but does not save memory compared to a string
representation.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.Babyl.lock">
<tt class="descname">lock</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.Babyl.lock" title="Permalink to this definition">¶</a></dt>
<dt id="mailbox.Babyl.unlock">
<tt class="descname">unlock</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.Babyl.unlock" title="Permalink to this definition">¶</a></dt>
<dd><p>Three locking mechanisms are used&#8212;dot locking and, if available, the
<tt class="xref c c-func docutils literal"><span class="pre">flock()</span></tt> and <tt class="xref c c-func docutils literal"><span class="pre">lockf()</span></tt> system calls.</p>
</dd></dl>

</dd></dl>

<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><a class="reference external" href="http://quimby.gnus.org/notes/BABYL">Format of Version 5 Babyl Files</a></dt>
<dd>A specification of the Babyl format.</dd>
<dt><a class="reference external" href="http://www.gnu.org/software/emacs/manual/html_node/emacs/Rmail.html">Reading Mail with Rmail</a></dt>
<dd>The Rmail manual, with some information on Babyl semantics.</dd>
</dl>
</div>
</div>
<div class="section" id="mmdf">
<span id="mailbox-mmdf"></span><h3>18.4.1.5. <a class="reference internal" href="#mailbox.MMDF" title="mailbox.MMDF"><tt class="xref py py-class docutils literal"><span class="pre">MMDF</span></tt></a><a class="headerlink" href="#mmdf" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="mailbox.MMDF">
<em class="property">class </em><tt class="descclassname">mailbox.</tt><tt class="descname">MMDF</tt><big>(</big><em>path</em>, <em>factory=None</em>, <em>create=True</em><big>)</big><a class="headerlink" href="#mailbox.MMDF" title="Permalink to this definition">¶</a></dt>
<dd><p>A subclass of <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> for mailboxes in MMDF format. Parameter <em>factory</em>
is a callable object that accepts a file-like message representation (which
behaves as if opened in binary mode) and returns a custom representation. If
<em>factory</em> is <tt class="docutils literal"><span class="pre">None</span></tt>, <a class="reference internal" href="#mailbox.MMDFMessage" title="mailbox.MMDFMessage"><tt class="xref py py-class docutils literal"><span class="pre">MMDFMessage</span></tt></a> is used as the default message
representation. If <em>create</em> is <tt class="docutils literal"><span class="pre">True</span></tt>, the mailbox is created if it does not
exist.</p>
<p>MMDF is a single-file mailbox format invented for the Multichannel Memorandum
Distribution Facility, a mail transfer agent. Each message is in the same
form as an mbox message but is bracketed before and after by lines containing
four Control-A (<tt class="docutils literal"><span class="pre">'\001'</span></tt>) characters. As with the mbox format, the
beginning of each message is indicated by a line whose first five characters
are &#8220;From &#8221;, but additional occurrences of &#8220;From &#8221; are not transformed to
&#8220;&gt;From &#8221; when storing messages because the extra message separator lines
prevent mistaking such occurrences for the starts of subsequent messages.</p>
<p>Some <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> methods implemented by <a class="reference internal" href="#mailbox.MMDF" title="mailbox.MMDF"><tt class="xref py py-class docutils literal"><span class="pre">MMDF</span></tt></a> deserve special
remarks:</p>
<dl class="method">
<dt id="mailbox.MMDF.get_file">
<tt class="descname">get_file</tt><big>(</big><em>key</em><big>)</big><a class="headerlink" href="#mailbox.MMDF.get_file" title="Permalink to this definition">¶</a></dt>
<dd><p>Using the file after calling <tt class="xref py py-meth docutils literal"><span class="pre">flush()</span></tt> or <tt class="xref py py-meth docutils literal"><span class="pre">close()</span></tt> on the
<a class="reference internal" href="#mailbox.MMDF" title="mailbox.MMDF"><tt class="xref py py-class docutils literal"><span class="pre">MMDF</span></tt></a> instance may yield unpredictable results or raise an
exception.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MMDF.lock">
<tt class="descname">lock</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.MMDF.lock" title="Permalink to this definition">¶</a></dt>
<dt id="mailbox.MMDF.unlock">
<tt class="descname">unlock</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.MMDF.unlock" title="Permalink to this definition">¶</a></dt>
<dd><p>Three locking mechanisms are used&#8212;dot locking and, if available, the
<tt class="xref c c-func docutils literal"><span class="pre">flock()</span></tt> and <tt class="xref c c-func docutils literal"><span class="pre">lockf()</span></tt> system calls.</p>
</dd></dl>

</dd></dl>

<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<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>A specification of MMDF format from the documentation of tin, a newsreader.</dd>
<dt><a class="reference external" href="http://en.wikipedia.org/wiki/MMDF">MMDF</a></dt>
<dd>A Wikipedia article describing the Multichannel Memorandum Distribution
Facility.</dd>
</dl>
</div>
</div>
</div>
<div class="section" id="message-objects">
<span id="mailbox-message-objects"></span><h2>18.4.2. <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a> objects<a class="headerlink" href="#message-objects" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="mailbox.Message">
<em class="property">class </em><tt class="descclassname">mailbox.</tt><tt class="descname">Message</tt><big>(</big><span class="optional">[</span><em>message</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#mailbox.Message" title="Permalink to this definition">¶</a></dt>
<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."><tt class="xref py py-mod docutils literal"><span class="pre">email.message</span></tt></a> module&#8217;s
<a class="reference internal" href="email.message.html#email.message.Message" title="email.message.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a>. Subclasses of <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">mailbox.Message</span></tt></a> add
mailbox-format-specific state and behavior.</p>
<p>If <em>message</em> is omitted, the new instance is created in a default, empty state.
If <em>message</em> is an <a class="reference internal" href="email.message.html#email.message.Message" title="email.message.Message"><tt class="xref py py-class docutils literal"><span class="pre">email.message.Message</span></tt></a> instance, its contents are
copied; furthermore, any format-specific information is converted insofar as
possible if <em>message</em> is a <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a> instance. If <em>message</em> is a string
or a file, it should contain an <span class="target" id="index-0"></span><a class="rfc reference external" href="http://tools.ietf.org/html/rfc2822.html"><strong>RFC 2822</strong></a>-compliant message, which is read
and parsed.</p>
<p>The format-specific state and behaviors offered by subclasses vary, but in
general it is only the properties that are not specific to a particular
mailbox that are supported (although presumably the properties are specific
to a particular mailbox format). For example, file offsets for single-file
mailbox formats and file names for directory-based mailbox formats are not
retained, because they are only applicable to the original mailbox. But state
such as whether a message has been read by the user or marked as important is
retained, because it applies to the message itself.</p>
<p>There is no requirement that <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a> instances be used to represent
messages retrieved using <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> instances. In some situations, the
time and memory required to generate <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a> representations might
not be acceptable. For such situations, <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> instances also
offer string and file-like representations, and a custom message factory may
be specified when a <a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> instance is initialized.</p>
</dd></dl>

<div class="section" id="maildirmessage">
<span id="mailbox-maildirmessage"></span><h3>18.4.2.1. <a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><tt class="xref py py-class docutils literal"><span class="pre">MaildirMessage</span></tt></a><a class="headerlink" href="#maildirmessage" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="mailbox.MaildirMessage">
<em class="property">class </em><tt class="descclassname">mailbox.</tt><tt class="descname">MaildirMessage</tt><big>(</big><span class="optional">[</span><em>message</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#mailbox.MaildirMessage" title="Permalink to this definition">¶</a></dt>
<dd><p>A message with Maildir-specific behaviors. Parameter <em>message</em> has the same
meaning as with the <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a> constructor.</p>
<p>Typically, a mail user agent application moves all of the messages in the
<tt class="file docutils literal"><span class="pre">new</span></tt> subdirectory to the <tt class="file docutils literal"><span class="pre">cur</span></tt> subdirectory after the first time
the user opens and closes the mailbox, recording that the messages are old
whether or not they&#8217;ve actually been read. Each message in <tt class="file docutils literal"><span class="pre">cur</span></tt> has an
&#8220;info&#8221; section added to its file name to store information about its state.
(Some mail readers may also add an &#8220;info&#8221; section to messages in
<tt class="file docutils literal"><span class="pre">new</span></tt>.)  The &#8220;info&#8221; section may take one of two forms: it may contain
&#8220;2,&#8221; followed by a list of standardized flags (e.g., &#8220;2,FR&#8221;) or it may
contain &#8220;1,&#8221; followed by so-called experimental information. Standard flags
for Maildir messages are as follows:</p>
<table border="1" class="docutils">
<colgroup>
<col width="13%" />
<col width="19%" />
<col width="68%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Flag</th>
<th class="head">Meaning</th>
<th class="head">Explanation</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>D</td>
<td>Draft</td>
<td>Under composition</td>
</tr>
<tr class="row-odd"><td>F</td>
<td>Flagged</td>
<td>Marked as important</td>
</tr>
<tr class="row-even"><td>P</td>
<td>Passed</td>
<td>Forwarded, resent, or bounced</td>
</tr>
<tr class="row-odd"><td>R</td>
<td>Replied</td>
<td>Replied to</td>
</tr>
<tr class="row-even"><td>S</td>
<td>Seen</td>
<td>Read</td>
</tr>
<tr class="row-odd"><td>T</td>
<td>Trashed</td>
<td>Marked for subsequent deletion</td>
</tr>
</tbody>
</table>
<p><a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><tt class="xref py py-class docutils literal"><span class="pre">MaildirMessage</span></tt></a> instances offer the following methods:</p>
<dl class="method">
<dt id="mailbox.MaildirMessage.get_subdir">
<tt class="descname">get_subdir</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.MaildirMessage.get_subdir" title="Permalink to this definition">¶</a></dt>
<dd><p>Return either &#8220;new&#8221; (if the message should be stored in the <tt class="file docutils literal"><span class="pre">new</span></tt>
subdirectory) or &#8220;cur&#8221; (if the message should be stored in the <tt class="file docutils literal"><span class="pre">cur</span></tt>
subdirectory).</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">A message is typically moved from <tt class="file docutils literal"><span class="pre">new</span></tt> to <tt class="file docutils literal"><span class="pre">cur</span></tt> after its
mailbox has been accessed, whether or not the message is has been
read. A message <tt class="docutils literal"><span class="pre">msg</span></tt> has been read if <tt class="docutils literal"><span class="pre">&quot;S&quot;</span> <span class="pre">in</span> <span class="pre">msg.get_flags()</span></tt> is
<tt class="docutils literal"><span class="pre">True</span></tt>.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="mailbox.MaildirMessage.set_subdir">
<tt class="descname">set_subdir</tt><big>(</big><em>subdir</em><big>)</big><a class="headerlink" href="#mailbox.MaildirMessage.set_subdir" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the subdirectory the message should be stored in. Parameter <em>subdir</em>
must be either &#8220;new&#8221; or &#8220;cur&#8221;.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MaildirMessage.get_flags">
<tt class="descname">get_flags</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.MaildirMessage.get_flags" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a string specifying the flags that are currently set. If the
message complies with the standard Maildir format, the result is the
concatenation in alphabetical order of zero or one occurrence of each of
<tt class="docutils literal"><span class="pre">'D'</span></tt>, <tt class="docutils literal"><span class="pre">'F'</span></tt>, <tt class="docutils literal"><span class="pre">'P'</span></tt>, <tt class="docutils literal"><span class="pre">'R'</span></tt>, <tt class="docutils literal"><span class="pre">'S'</span></tt>, and <tt class="docutils literal"><span class="pre">'T'</span></tt>. The empty string
is returned if no flags are set or if &#8220;info&#8221; contains experimental
semantics.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MaildirMessage.set_flags">
<tt class="descname">set_flags</tt><big>(</big><em>flags</em><big>)</big><a class="headerlink" href="#mailbox.MaildirMessage.set_flags" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the flags specified by <em>flags</em> and unset all others.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MaildirMessage.add_flag">
<tt class="descname">add_flag</tt><big>(</big><em>flag</em><big>)</big><a class="headerlink" href="#mailbox.MaildirMessage.add_flag" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the flag(s) specified by <em>flag</em> without changing other flags. To add
more than one flag at a time, <em>flag</em> may be a string of more than one
character. The current &#8220;info&#8221; is overwritten whether or not it contains
experimental information rather than flags.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MaildirMessage.remove_flag">
<tt class="descname">remove_flag</tt><big>(</big><em>flag</em><big>)</big><a class="headerlink" href="#mailbox.MaildirMessage.remove_flag" title="Permalink to this definition">¶</a></dt>
<dd><p>Unset the flag(s) specified by <em>flag</em> without changing other flags. To
remove more than one flag at a time, <em>flag</em> maybe a string of more than
one character.  If &#8220;info&#8221; contains experimental information rather than
flags, the current &#8220;info&#8221; is not modified.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MaildirMessage.get_date">
<tt class="descname">get_date</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.MaildirMessage.get_date" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the delivery date of the message as a floating-point number
representing seconds since the epoch.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MaildirMessage.set_date">
<tt class="descname">set_date</tt><big>(</big><em>date</em><big>)</big><a class="headerlink" href="#mailbox.MaildirMessage.set_date" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the delivery date of the message to <em>date</em>, a floating-point number
representing seconds since the epoch.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MaildirMessage.get_info">
<tt class="descname">get_info</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.MaildirMessage.get_info" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a string containing the &#8220;info&#8221; for a message. This is useful for
accessing and modifying &#8220;info&#8221; that is experimental (i.e., not a list of
flags).</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MaildirMessage.set_info">
<tt class="descname">set_info</tt><big>(</big><em>info</em><big>)</big><a class="headerlink" href="#mailbox.MaildirMessage.set_info" title="Permalink to this definition">¶</a></dt>
<dd><p>Set &#8220;info&#8221; to <em>info</em>, which should be a string.</p>
</dd></dl>

</dd></dl>

<p>When a <a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><tt class="xref py py-class docutils literal"><span class="pre">MaildirMessage</span></tt></a> instance is created based upon an
<a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><tt class="xref py py-class docutils literal"><span class="pre">mboxMessage</span></tt></a> or <a class="reference internal" href="#mailbox.MMDFMessage" title="mailbox.MMDFMessage"><tt class="xref py py-class docutils literal"><span class="pre">MMDFMessage</span></tt></a> instance, the <em class="mailheader">Status</em>
and <em class="mailheader">X-Status</em> headers are omitted and the following conversions
take place:</p>
<table border="1" class="docutils">
<colgroup>
<col width="30%" />
<col width="70%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Resulting state</th>
<th class="head"><a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><tt class="xref py py-class docutils literal"><span class="pre">mboxMessage</span></tt></a> or <a class="reference internal" href="#mailbox.MMDFMessage" title="mailbox.MMDFMessage"><tt class="xref py py-class docutils literal"><span class="pre">MMDFMessage</span></tt></a>
state</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>&#8220;cur&#8221; subdirectory</td>
<td>O flag</td>
</tr>
<tr class="row-odd"><td>F flag</td>
<td>F flag</td>
</tr>
<tr class="row-even"><td>R flag</td>
<td>A flag</td>
</tr>
<tr class="row-odd"><td>S flag</td>
<td>R flag</td>
</tr>
<tr class="row-even"><td>T flag</td>
<td>D flag</td>
</tr>
</tbody>
</table>
<p>When a <a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><tt class="xref py py-class docutils literal"><span class="pre">MaildirMessage</span></tt></a> instance is created based upon an
<a class="reference internal" href="#mailbox.MHMessage" title="mailbox.MHMessage"><tt class="xref py py-class docutils literal"><span class="pre">MHMessage</span></tt></a> instance, the following conversions take place:</p>
<table border="1" class="docutils">
<colgroup>
<col width="54%" />
<col width="46%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Resulting state</th>
<th class="head"><a class="reference internal" href="#mailbox.MHMessage" title="mailbox.MHMessage"><tt class="xref py py-class docutils literal"><span class="pre">MHMessage</span></tt></a> state</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>&#8220;cur&#8221; subdirectory</td>
<td>&#8220;unseen&#8221; sequence</td>
</tr>
<tr class="row-odd"><td>&#8220;cur&#8221; subdirectory and S flag</td>
<td>no &#8220;unseen&#8221; sequence</td>
</tr>
<tr class="row-even"><td>F flag</td>
<td>&#8220;flagged&#8221; sequence</td>
</tr>
<tr class="row-odd"><td>R flag</td>
<td>&#8220;replied&#8221; sequence</td>
</tr>
</tbody>
</table>
<p>When a <a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><tt class="xref py py-class docutils literal"><span class="pre">MaildirMessage</span></tt></a> instance is created based upon a
<a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><tt class="xref py py-class docutils literal"><span class="pre">BabylMessage</span></tt></a> instance, the following conversions take place:</p>
<table border="1" class="docutils">
<colgroup>
<col width="50%" />
<col width="50%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Resulting state</th>
<th class="head"><a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><tt class="xref py py-class docutils literal"><span class="pre">BabylMessage</span></tt></a> state</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>&#8220;cur&#8221; subdirectory</td>
<td>&#8220;unseen&#8221; label</td>
</tr>
<tr class="row-odd"><td>&#8220;cur&#8221; subdirectory and S flag</td>
<td>no &#8220;unseen&#8221; label</td>
</tr>
<tr class="row-even"><td>P flag</td>
<td>&#8220;forwarded&#8221; or &#8220;resent&#8221; label</td>
</tr>
<tr class="row-odd"><td>R flag</td>
<td>&#8220;answered&#8221; label</td>
</tr>
<tr class="row-even"><td>T flag</td>
<td>&#8220;deleted&#8221; label</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="mboxmessage">
<span id="mailbox-mboxmessage"></span><h3>18.4.2.2. <a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><tt class="xref py py-class docutils literal"><span class="pre">mboxMessage</span></tt></a><a class="headerlink" href="#mboxmessage" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="mailbox.mboxMessage">
<em class="property">class </em><tt class="descclassname">mailbox.</tt><tt class="descname">mboxMessage</tt><big>(</big><span class="optional">[</span><em>message</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#mailbox.mboxMessage" title="Permalink to this definition">¶</a></dt>
<dd><p>A message with mbox-specific behaviors. Parameter <em>message</em> has the same meaning
as with the <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a> constructor.</p>
<p>Messages in an mbox mailbox are stored together in a single file. The
sender&#8217;s envelope address and the time of delivery are typically stored in a
line beginning with &#8220;From &#8221; that is used to indicate the start of a message,
though there is considerable variation in the exact format of this data among
mbox implementations. Flags that indicate the state of the message, such as
whether it has been read or marked as important, are typically stored in
<em class="mailheader">Status</em> and <em class="mailheader">X-Status</em> headers.</p>
<p>Conventional flags for mbox messages are as follows:</p>
<table border="1" class="docutils">
<colgroup>
<col width="13%" />
<col width="21%" />
<col width="67%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Flag</th>
<th class="head">Meaning</th>
<th class="head">Explanation</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>R</td>
<td>Read</td>
<td>Read</td>
</tr>
<tr class="row-odd"><td>O</td>
<td>Old</td>
<td>Previously detected by MUA</td>
</tr>
<tr class="row-even"><td>D</td>
<td>Deleted</td>
<td>Marked for subsequent deletion</td>
</tr>
<tr class="row-odd"><td>F</td>
<td>Flagged</td>
<td>Marked as important</td>
</tr>
<tr class="row-even"><td>A</td>
<td>Answered</td>
<td>Replied to</td>
</tr>
</tbody>
</table>
<p>The &#8220;R&#8221; and &#8220;O&#8221; flags are stored in the <em class="mailheader">Status</em> header, and the
&#8220;D&#8221;, &#8220;F&#8221;, and &#8220;A&#8221; flags are stored in the <em class="mailheader">X-Status</em> header. The
flags and headers typically appear in the order mentioned.</p>
<p><a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><tt class="xref py py-class docutils literal"><span class="pre">mboxMessage</span></tt></a> instances offer the following methods:</p>
<dl class="method">
<dt id="mailbox.mboxMessage.get_from">
<tt class="descname">get_from</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.mboxMessage.get_from" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a string representing the &#8220;From &#8221; line that marks the start of the
message in an mbox mailbox. The leading &#8220;From &#8221; and the trailing newline
are excluded.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.mboxMessage.set_from">
<tt class="descname">set_from</tt><big>(</big><em>from_</em>, <em>time_=None</em><big>)</big><a class="headerlink" href="#mailbox.mboxMessage.set_from" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the &#8220;From &#8221; line to <em>from_</em>, which should be specified without a
leading &#8220;From &#8221; or trailing newline. For convenience, <em>time_</em> may be
specified and will be formatted appropriately and appended to <em>from_</em>. If
<em>time_</em> is specified, it should be a <tt class="xref py py-class docutils literal"><span class="pre">struct_time</span></tt> instance, a
tuple suitable for passing to <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><tt class="xref py py-meth docutils literal"><span class="pre">time.strftime()</span></tt></a>, or <tt class="docutils literal"><span class="pre">True</span></tt> (to use
<a class="reference internal" href="time.html#time.gmtime" title="time.gmtime"><tt class="xref py py-meth docutils literal"><span class="pre">time.gmtime()</span></tt></a>).</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.mboxMessage.get_flags">
<tt class="descname">get_flags</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.mboxMessage.get_flags" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a string specifying the flags that are currently set. If the
message complies with the conventional format, the result is the
concatenation in the following order of zero or one occurrence of each of
<tt class="docutils literal"><span class="pre">'R'</span></tt>, <tt class="docutils literal"><span class="pre">'O'</span></tt>, <tt class="docutils literal"><span class="pre">'D'</span></tt>, <tt class="docutils literal"><span class="pre">'F'</span></tt>, and <tt class="docutils literal"><span class="pre">'A'</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.mboxMessage.set_flags">
<tt class="descname">set_flags</tt><big>(</big><em>flags</em><big>)</big><a class="headerlink" href="#mailbox.mboxMessage.set_flags" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the flags specified by <em>flags</em> and unset all others. Parameter <em>flags</em>
should be the concatenation in any order of zero or more occurrences of
each of <tt class="docutils literal"><span class="pre">'R'</span></tt>, <tt class="docutils literal"><span class="pre">'O'</span></tt>, <tt class="docutils literal"><span class="pre">'D'</span></tt>, <tt class="docutils literal"><span class="pre">'F'</span></tt>, and <tt class="docutils literal"><span class="pre">'A'</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.mboxMessage.add_flag">
<tt class="descname">add_flag</tt><big>(</big><em>flag</em><big>)</big><a class="headerlink" href="#mailbox.mboxMessage.add_flag" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the flag(s) specified by <em>flag</em> without changing other flags. To add
more than one flag at a time, <em>flag</em> may be a string of more than one
character.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.mboxMessage.remove_flag">
<tt class="descname">remove_flag</tt><big>(</big><em>flag</em><big>)</big><a class="headerlink" href="#mailbox.mboxMessage.remove_flag" title="Permalink to this definition">¶</a></dt>
<dd><p>Unset the flag(s) specified by <em>flag</em> without changing other flags. To
remove more than one flag at a time, <em>flag</em> maybe a string of more than
one character.</p>
</dd></dl>

</dd></dl>

<p>When an <a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><tt class="xref py py-class docutils literal"><span class="pre">mboxMessage</span></tt></a> instance is created based upon a
<a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><tt class="xref py py-class docutils literal"><span class="pre">MaildirMessage</span></tt></a> instance, a &#8220;From &#8221; line is generated based upon the
<a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><tt class="xref py py-class docutils literal"><span class="pre">MaildirMessage</span></tt></a> instance&#8217;s delivery date, and the following conversions
take place:</p>
<table border="1" class="docutils">
<colgroup>
<col width="35%" />
<col width="65%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Resulting state</th>
<th class="head"><a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><tt class="xref py py-class docutils literal"><span class="pre">MaildirMessage</span></tt></a> state</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>R flag</td>
<td>S flag</td>
</tr>
<tr class="row-odd"><td>O flag</td>
<td>&#8220;cur&#8221; subdirectory</td>
</tr>
<tr class="row-even"><td>D flag</td>
<td>T flag</td>
</tr>
<tr class="row-odd"><td>F flag</td>
<td>F flag</td>
</tr>
<tr class="row-even"><td>A flag</td>
<td>R flag</td>
</tr>
</tbody>
</table>
<p>When an <a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><tt class="xref py py-class docutils literal"><span class="pre">mboxMessage</span></tt></a> instance is created based upon an
<a class="reference internal" href="#mailbox.MHMessage" title="mailbox.MHMessage"><tt class="xref py py-class docutils literal"><span class="pre">MHMessage</span></tt></a> instance, the following conversions take place:</p>
<table border="1" class="docutils">
<colgroup>
<col width="42%" />
<col width="58%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Resulting state</th>
<th class="head"><a class="reference internal" href="#mailbox.MHMessage" title="mailbox.MHMessage"><tt class="xref py py-class docutils literal"><span class="pre">MHMessage</span></tt></a> state</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>R flag and O flag</td>
<td>no &#8220;unseen&#8221; sequence</td>
</tr>
<tr class="row-odd"><td>O flag</td>
<td>&#8220;unseen&#8221; sequence</td>
</tr>
<tr class="row-even"><td>F flag</td>
<td>&#8220;flagged&#8221; sequence</td>
</tr>
<tr class="row-odd"><td>A flag</td>
<td>&#8220;replied&#8221; sequence</td>
</tr>
</tbody>
</table>
<p>When an <a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><tt class="xref py py-class docutils literal"><span class="pre">mboxMessage</span></tt></a> instance is created based upon a
<a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><tt class="xref py py-class docutils literal"><span class="pre">BabylMessage</span></tt></a> instance, the following conversions take place:</p>
<table border="1" class="docutils">
<colgroup>
<col width="40%" />
<col width="60%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Resulting state</th>
<th class="head"><a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><tt class="xref py py-class docutils literal"><span class="pre">BabylMessage</span></tt></a> state</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>R flag and O flag</td>
<td>no &#8220;unseen&#8221; label</td>
</tr>
<tr class="row-odd"><td>O flag</td>
<td>&#8220;unseen&#8221; label</td>
</tr>
<tr class="row-even"><td>D flag</td>
<td>&#8220;deleted&#8221; label</td>
</tr>
<tr class="row-odd"><td>A flag</td>
<td>&#8220;answered&#8221; label</td>
</tr>
</tbody>
</table>
<p>When a <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a> instance is created based upon an <a class="reference internal" href="#mailbox.MMDFMessage" title="mailbox.MMDFMessage"><tt class="xref py py-class docutils literal"><span class="pre">MMDFMessage</span></tt></a>
instance, the &#8220;From &#8221; line is copied and all flags directly correspond:</p>
<table border="1" class="docutils">
<colgroup>
<col width="38%" />
<col width="62%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Resulting state</th>
<th class="head"><a class="reference internal" href="#mailbox.MMDFMessage" title="mailbox.MMDFMessage"><tt class="xref py py-class docutils literal"><span class="pre">MMDFMessage</span></tt></a> state</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>R flag</td>
<td>R flag</td>
</tr>
<tr class="row-odd"><td>O flag</td>
<td>O flag</td>
</tr>
<tr class="row-even"><td>D flag</td>
<td>D flag</td>
</tr>
<tr class="row-odd"><td>F flag</td>
<td>F flag</td>
</tr>
<tr class="row-even"><td>A flag</td>
<td>A flag</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="mhmessage">
<span id="mailbox-mhmessage"></span><h3>18.4.2.3. <a class="reference internal" href="#mailbox.MHMessage" title="mailbox.MHMessage"><tt class="xref py py-class docutils literal"><span class="pre">MHMessage</span></tt></a><a class="headerlink" href="#mhmessage" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="mailbox.MHMessage">
<em class="property">class </em><tt class="descclassname">mailbox.</tt><tt class="descname">MHMessage</tt><big>(</big><span class="optional">[</span><em>message</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#mailbox.MHMessage" title="Permalink to this definition">¶</a></dt>
<dd><p>A message with MH-specific behaviors. Parameter <em>message</em> has the same meaning
as with the <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a> constructor.</p>
<p>MH messages do not support marks or flags in the traditional sense, but they
do support sequences, which are logical groupings of arbitrary messages. Some
mail reading programs (although not the standard <strong class="program">mh</strong> and
<strong class="program">nmh</strong>) use sequences in much the same way flags are used with other
formats, as follows:</p>
<table border="1" class="docutils">
<colgroup>
<col width="19%" />
<col width="81%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Sequence</th>
<th class="head">Explanation</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>unseen</td>
<td>Not read, but previously detected by MUA</td>
</tr>
<tr class="row-odd"><td>replied</td>
<td>Replied to</td>
</tr>
<tr class="row-even"><td>flagged</td>
<td>Marked as important</td>
</tr>
</tbody>
</table>
<p><a class="reference internal" href="#mailbox.MHMessage" title="mailbox.MHMessage"><tt class="xref py py-class docutils literal"><span class="pre">MHMessage</span></tt></a> instances offer the following methods:</p>
<dl class="method">
<dt id="mailbox.MHMessage.get_sequences">
<tt class="descname">get_sequences</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.MHMessage.get_sequences" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a list of the names of sequences that include this message.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MHMessage.set_sequences">
<tt class="descname">set_sequences</tt><big>(</big><em>sequences</em><big>)</big><a class="headerlink" href="#mailbox.MHMessage.set_sequences" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the list of sequences that include this message.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MHMessage.add_sequence">
<tt class="descname">add_sequence</tt><big>(</big><em>sequence</em><big>)</big><a class="headerlink" href="#mailbox.MHMessage.add_sequence" title="Permalink to this definition">¶</a></dt>
<dd><p>Add <em>sequence</em> to the list of sequences that include this message.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MHMessage.remove_sequence">
<tt class="descname">remove_sequence</tt><big>(</big><em>sequence</em><big>)</big><a class="headerlink" href="#mailbox.MHMessage.remove_sequence" title="Permalink to this definition">¶</a></dt>
<dd><p>Remove <em>sequence</em> from the list of sequences that include this message.</p>
</dd></dl>

</dd></dl>

<p>When an <a class="reference internal" href="#mailbox.MHMessage" title="mailbox.MHMessage"><tt class="xref py py-class docutils literal"><span class="pre">MHMessage</span></tt></a> instance is created based upon a
<a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><tt class="xref py py-class docutils literal"><span class="pre">MaildirMessage</span></tt></a> instance, the following conversions take place:</p>
<table border="1" class="docutils">
<colgroup>
<col width="39%" />
<col width="61%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Resulting state</th>
<th class="head"><a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><tt class="xref py py-class docutils literal"><span class="pre">MaildirMessage</span></tt></a> state</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>&#8220;unseen&#8221; sequence</td>
<td>no S flag</td>
</tr>
<tr class="row-odd"><td>&#8220;replied&#8221; sequence</td>
<td>R flag</td>
</tr>
<tr class="row-even"><td>&#8220;flagged&#8221; sequence</td>
<td>F flag</td>
</tr>
</tbody>
</table>
<p>When an <a class="reference internal" href="#mailbox.MHMessage" title="mailbox.MHMessage"><tt class="xref py py-class docutils literal"><span class="pre">MHMessage</span></tt></a> instance is created based upon an
<a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><tt class="xref py py-class docutils literal"><span class="pre">mboxMessage</span></tt></a> or <a class="reference internal" href="#mailbox.MMDFMessage" title="mailbox.MMDFMessage"><tt class="xref py py-class docutils literal"><span class="pre">MMDFMessage</span></tt></a> instance, the <em class="mailheader">Status</em>
and <em class="mailheader">X-Status</em> headers are omitted and the following conversions
take place:</p>
<table border="1" class="docutils">
<colgroup>
<col width="30%" />
<col width="70%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Resulting state</th>
<th class="head"><a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><tt class="xref py py-class docutils literal"><span class="pre">mboxMessage</span></tt></a> or <a class="reference internal" href="#mailbox.MMDFMessage" title="mailbox.MMDFMessage"><tt class="xref py py-class docutils literal"><span class="pre">MMDFMessage</span></tt></a>
state</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>&#8220;unseen&#8221; sequence</td>
<td>no R flag</td>
</tr>
<tr class="row-odd"><td>&#8220;replied&#8221; sequence</td>
<td>A flag</td>
</tr>
<tr class="row-even"><td>&#8220;flagged&#8221; sequence</td>
<td>F flag</td>
</tr>
</tbody>
</table>
<p>When an <a class="reference internal" href="#mailbox.MHMessage" title="mailbox.MHMessage"><tt class="xref py py-class docutils literal"><span class="pre">MHMessage</span></tt></a> instance is created based upon a
<a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><tt class="xref py py-class docutils literal"><span class="pre">BabylMessage</span></tt></a> instance, the following conversions take place:</p>
<table border="1" class="docutils">
<colgroup>
<col width="41%" />
<col width="59%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Resulting state</th>
<th class="head"><a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><tt class="xref py py-class docutils literal"><span class="pre">BabylMessage</span></tt></a> state</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>&#8220;unseen&#8221; sequence</td>
<td>&#8220;unseen&#8221; label</td>
</tr>
<tr class="row-odd"><td>&#8220;replied&#8221; sequence</td>
<td>&#8220;answered&#8221; label</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="babylmessage">
<span id="mailbox-babylmessage"></span><h3>18.4.2.4. <a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><tt class="xref py py-class docutils literal"><span class="pre">BabylMessage</span></tt></a><a class="headerlink" href="#babylmessage" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="mailbox.BabylMessage">
<em class="property">class </em><tt class="descclassname">mailbox.</tt><tt class="descname">BabylMessage</tt><big>(</big><span class="optional">[</span><em>message</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#mailbox.BabylMessage" title="Permalink to this definition">¶</a></dt>
<dd><p>A message with Babyl-specific behaviors. Parameter <em>message</em> has the same
meaning as with the <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a> constructor.</p>
<p>Certain message labels, called <em class="dfn">attributes</em>, are defined by convention
to have special meanings. The attributes are as follows:</p>
<table border="1" class="docutils">
<colgroup>
<col width="21%" />
<col width="79%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Label</th>
<th class="head">Explanation</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>unseen</td>
<td>Not read, but previously detected by MUA</td>
</tr>
<tr class="row-odd"><td>deleted</td>
<td>Marked for subsequent deletion</td>
</tr>
<tr class="row-even"><td>filed</td>
<td>Copied to another file or mailbox</td>
</tr>
<tr class="row-odd"><td>answered</td>
<td>Replied to</td>
</tr>
<tr class="row-even"><td>forwarded</td>
<td>Forwarded</td>
</tr>
<tr class="row-odd"><td>edited</td>
<td>Modified by the user</td>
</tr>
<tr class="row-even"><td>resent</td>
<td>Resent</td>
</tr>
</tbody>
</table>
<p>By default, Rmail displays only visible headers. The <a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><tt class="xref py py-class docutils literal"><span class="pre">BabylMessage</span></tt></a>
class, though, uses the original headers because they are more
complete. Visible headers may be accessed explicitly if desired.</p>
<p><a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><tt class="xref py py-class docutils literal"><span class="pre">BabylMessage</span></tt></a> instances offer the following methods:</p>
<dl class="method">
<dt id="mailbox.BabylMessage.get_labels">
<tt class="descname">get_labels</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.BabylMessage.get_labels" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a list of labels on the message.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.BabylMessage.set_labels">
<tt class="descname">set_labels</tt><big>(</big><em>labels</em><big>)</big><a class="headerlink" href="#mailbox.BabylMessage.set_labels" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the list of labels on the message to <em>labels</em>.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.BabylMessage.add_label">
<tt class="descname">add_label</tt><big>(</big><em>label</em><big>)</big><a class="headerlink" href="#mailbox.BabylMessage.add_label" title="Permalink to this definition">¶</a></dt>
<dd><p>Add <em>label</em> to the list of labels on the message.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.BabylMessage.remove_label">
<tt class="descname">remove_label</tt><big>(</big><em>label</em><big>)</big><a class="headerlink" href="#mailbox.BabylMessage.remove_label" title="Permalink to this definition">¶</a></dt>
<dd><p>Remove <em>label</em> from the list of labels on the message.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.BabylMessage.get_visible">
<tt class="descname">get_visible</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.BabylMessage.get_visible" title="Permalink to this definition">¶</a></dt>
<dd><p>Return an <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a> instance whose headers are the message&#8217;s
visible headers and whose body is empty.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.BabylMessage.set_visible">
<tt class="descname">set_visible</tt><big>(</big><em>visible</em><big>)</big><a class="headerlink" href="#mailbox.BabylMessage.set_visible" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the message&#8217;s visible headers to be the same as the headers in
<em>message</em>.  Parameter <em>visible</em> should be a <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a> instance, an
<a class="reference internal" href="email.message.html#email.message.Message" title="email.message.Message"><tt class="xref py py-class docutils literal"><span class="pre">email.message.Message</span></tt></a> instance, a string, or a file-like object
(which should be open in text mode).</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.BabylMessage.update_visible">
<tt class="descname">update_visible</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.BabylMessage.update_visible" title="Permalink to this definition">¶</a></dt>
<dd><p>When a <a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><tt class="xref py py-class docutils literal"><span class="pre">BabylMessage</span></tt></a> instance&#8217;s original headers are modified, the
visible headers are not automatically modified to correspond. This method
updates the visible headers as follows: each visible header with a
corresponding original header is set to the value of the original header,
each visible header without a corresponding original header is removed,
and any of <em class="mailheader">Date</em>, <em class="mailheader">From</em>, <em class="mailheader">Reply-To</em>,
<em class="mailheader">To</em>, <em class="mailheader">CC</em>, and <em class="mailheader">Subject</em> that are
present in the original headers but not the visible headers are added to
the visible headers.</p>
</dd></dl>

</dd></dl>

<p>When a <a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><tt class="xref py py-class docutils literal"><span class="pre">BabylMessage</span></tt></a> instance is created based upon a
<a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><tt class="xref py py-class docutils literal"><span class="pre">MaildirMessage</span></tt></a> instance, the following conversions take place:</p>
<table border="1" class="docutils">
<colgroup>
<col width="38%" />
<col width="62%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Resulting state</th>
<th class="head"><a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><tt class="xref py py-class docutils literal"><span class="pre">MaildirMessage</span></tt></a> state</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>&#8220;unseen&#8221; label</td>
<td>no S flag</td>
</tr>
<tr class="row-odd"><td>&#8220;deleted&#8221; label</td>
<td>T flag</td>
</tr>
<tr class="row-even"><td>&#8220;answered&#8221; label</td>
<td>R flag</td>
</tr>
<tr class="row-odd"><td>&#8220;forwarded&#8221; label</td>
<td>P flag</td>
</tr>
</tbody>
</table>
<p>When a <a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><tt class="xref py py-class docutils literal"><span class="pre">BabylMessage</span></tt></a> instance is created based upon an
<a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><tt class="xref py py-class docutils literal"><span class="pre">mboxMessage</span></tt></a> or <a class="reference internal" href="#mailbox.MMDFMessage" title="mailbox.MMDFMessage"><tt class="xref py py-class docutils literal"><span class="pre">MMDFMessage</span></tt></a> instance, the <em class="mailheader">Status</em>
and <em class="mailheader">X-Status</em> headers are omitted and the following conversions
take place:</p>
<table border="1" class="docutils">
<colgroup>
<col width="28%" />
<col width="72%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Resulting state</th>
<th class="head"><a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><tt class="xref py py-class docutils literal"><span class="pre">mboxMessage</span></tt></a> or <a class="reference internal" href="#mailbox.MMDFMessage" title="mailbox.MMDFMessage"><tt class="xref py py-class docutils literal"><span class="pre">MMDFMessage</span></tt></a>
state</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>&#8220;unseen&#8221; label</td>
<td>no R flag</td>
</tr>
<tr class="row-odd"><td>&#8220;deleted&#8221; label</td>
<td>D flag</td>
</tr>
<tr class="row-even"><td>&#8220;answered&#8221; label</td>
<td>A flag</td>
</tr>
</tbody>
</table>
<p>When a <a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><tt class="xref py py-class docutils literal"><span class="pre">BabylMessage</span></tt></a> instance is created based upon an
<a class="reference internal" href="#mailbox.MHMessage" title="mailbox.MHMessage"><tt class="xref py py-class docutils literal"><span class="pre">MHMessage</span></tt></a> instance, the following conversions take place:</p>
<table border="1" class="docutils">
<colgroup>
<col width="41%" />
<col width="59%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Resulting state</th>
<th class="head"><a class="reference internal" href="#mailbox.MHMessage" title="mailbox.MHMessage"><tt class="xref py py-class docutils literal"><span class="pre">MHMessage</span></tt></a> state</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>&#8220;unseen&#8221; label</td>
<td>&#8220;unseen&#8221; sequence</td>
</tr>
<tr class="row-odd"><td>&#8220;answered&#8221; label</td>
<td>&#8220;replied&#8221; sequence</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="mmdfmessage">
<span id="mailbox-mmdfmessage"></span><h3>18.4.2.5. <a class="reference internal" href="#mailbox.MMDFMessage" title="mailbox.MMDFMessage"><tt class="xref py py-class docutils literal"><span class="pre">MMDFMessage</span></tt></a><a class="headerlink" href="#mmdfmessage" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="mailbox.MMDFMessage">
<em class="property">class </em><tt class="descclassname">mailbox.</tt><tt class="descname">MMDFMessage</tt><big>(</big><span class="optional">[</span><em>message</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#mailbox.MMDFMessage" title="Permalink to this definition">¶</a></dt>
<dd><p>A message with MMDF-specific behaviors. Parameter <em>message</em> has the same meaning
as with the <a class="reference internal" href="#mailbox.Message" title="mailbox.Message"><tt class="xref py py-class docutils literal"><span class="pre">Message</span></tt></a> constructor.</p>
<p>As with message in an mbox mailbox, MMDF messages are stored with the
sender&#8217;s address and the delivery date in an initial line beginning with
&#8220;From &#8221;.  Likewise, flags that indicate the state of the message are
typically stored in <em class="mailheader">Status</em> and <em class="mailheader">X-Status</em> headers.</p>
<p>Conventional flags for MMDF messages are identical to those of mbox message
and are as follows:</p>
<table border="1" class="docutils">
<colgroup>
<col width="13%" />
<col width="21%" />
<col width="67%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Flag</th>
<th class="head">Meaning</th>
<th class="head">Explanation</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>R</td>
<td>Read</td>
<td>Read</td>
</tr>
<tr class="row-odd"><td>O</td>
<td>Old</td>
<td>Previously detected by MUA</td>
</tr>
<tr class="row-even"><td>D</td>
<td>Deleted</td>
<td>Marked for subsequent deletion</td>
</tr>
<tr class="row-odd"><td>F</td>
<td>Flagged</td>
<td>Marked as important</td>
</tr>
<tr class="row-even"><td>A</td>
<td>Answered</td>
<td>Replied to</td>
</tr>
</tbody>
</table>
<p>The &#8220;R&#8221; and &#8220;O&#8221; flags are stored in the <em class="mailheader">Status</em> header, and the
&#8220;D&#8221;, &#8220;F&#8221;, and &#8220;A&#8221; flags are stored in the <em class="mailheader">X-Status</em> header. The
flags and headers typically appear in the order mentioned.</p>
<p><a class="reference internal" href="#mailbox.MMDFMessage" title="mailbox.MMDFMessage"><tt class="xref py py-class docutils literal"><span class="pre">MMDFMessage</span></tt></a> instances offer the following methods, which are
identical to those offered by <a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><tt class="xref py py-class docutils literal"><span class="pre">mboxMessage</span></tt></a>:</p>
<dl class="method">
<dt id="mailbox.MMDFMessage.get_from">
<tt class="descname">get_from</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.MMDFMessage.get_from" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a string representing the &#8220;From &#8221; line that marks the start of the
message in an mbox mailbox. The leading &#8220;From &#8221; and the trailing newline
are excluded.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MMDFMessage.set_from">
<tt class="descname">set_from</tt><big>(</big><em>from_</em>, <em>time_=None</em><big>)</big><a class="headerlink" href="#mailbox.MMDFMessage.set_from" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the &#8220;From &#8221; line to <em>from_</em>, which should be specified without a
leading &#8220;From &#8221; or trailing newline. For convenience, <em>time_</em> may be
specified and will be formatted appropriately and appended to <em>from_</em>. If
<em>time_</em> is specified, it should be a <tt class="xref py py-class docutils literal"><span class="pre">struct_time</span></tt> instance, a
tuple suitable for passing to <a class="reference internal" href="time.html#time.strftime" title="time.strftime"><tt class="xref py py-meth docutils literal"><span class="pre">time.strftime()</span></tt></a>, or <tt class="docutils literal"><span class="pre">True</span></tt> (to use
<a class="reference internal" href="time.html#time.gmtime" title="time.gmtime"><tt class="xref py py-meth docutils literal"><span class="pre">time.gmtime()</span></tt></a>).</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MMDFMessage.get_flags">
<tt class="descname">get_flags</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.MMDFMessage.get_flags" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a string specifying the flags that are currently set. If the
message complies with the conventional format, the result is the
concatenation in the following order of zero or one occurrence of each of
<tt class="docutils literal"><span class="pre">'R'</span></tt>, <tt class="docutils literal"><span class="pre">'O'</span></tt>, <tt class="docutils literal"><span class="pre">'D'</span></tt>, <tt class="docutils literal"><span class="pre">'F'</span></tt>, and <tt class="docutils literal"><span class="pre">'A'</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MMDFMessage.set_flags">
<tt class="descname">set_flags</tt><big>(</big><em>flags</em><big>)</big><a class="headerlink" href="#mailbox.MMDFMessage.set_flags" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the flags specified by <em>flags</em> and unset all others. Parameter <em>flags</em>
should be the concatenation in any order of zero or more occurrences of
each of <tt class="docutils literal"><span class="pre">'R'</span></tt>, <tt class="docutils literal"><span class="pre">'O'</span></tt>, <tt class="docutils literal"><span class="pre">'D'</span></tt>, <tt class="docutils literal"><span class="pre">'F'</span></tt>, and <tt class="docutils literal"><span class="pre">'A'</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MMDFMessage.add_flag">
<tt class="descname">add_flag</tt><big>(</big><em>flag</em><big>)</big><a class="headerlink" href="#mailbox.MMDFMessage.add_flag" title="Permalink to this definition">¶</a></dt>
<dd><p>Set the flag(s) specified by <em>flag</em> without changing other flags. To add
more than one flag at a time, <em>flag</em> may be a string of more than one
character.</p>
</dd></dl>

<dl class="method">
<dt id="mailbox.MMDFMessage.remove_flag">
<tt class="descname">remove_flag</tt><big>(</big><em>flag</em><big>)</big><a class="headerlink" href="#mailbox.MMDFMessage.remove_flag" title="Permalink to this definition">¶</a></dt>
<dd><p>Unset the flag(s) specified by <em>flag</em> without changing other flags. To
remove more than one flag at a time, <em>flag</em> maybe a string of more than
one character.</p>
</dd></dl>

</dd></dl>

<p>When an <a class="reference internal" href="#mailbox.MMDFMessage" title="mailbox.MMDFMessage"><tt class="xref py py-class docutils literal"><span class="pre">MMDFMessage</span></tt></a> instance is created based upon a
<a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><tt class="xref py py-class docutils literal"><span class="pre">MaildirMessage</span></tt></a> instance, a &#8220;From &#8221; line is generated based upon the
<a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><tt class="xref py py-class docutils literal"><span class="pre">MaildirMessage</span></tt></a> instance&#8217;s delivery date, and the following conversions
take place:</p>
<table border="1" class="docutils">
<colgroup>
<col width="35%" />
<col width="65%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Resulting state</th>
<th class="head"><a class="reference internal" href="#mailbox.MaildirMessage" title="mailbox.MaildirMessage"><tt class="xref py py-class docutils literal"><span class="pre">MaildirMessage</span></tt></a> state</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>R flag</td>
<td>S flag</td>
</tr>
<tr class="row-odd"><td>O flag</td>
<td>&#8220;cur&#8221; subdirectory</td>
</tr>
<tr class="row-even"><td>D flag</td>
<td>T flag</td>
</tr>
<tr class="row-odd"><td>F flag</td>
<td>F flag</td>
</tr>
<tr class="row-even"><td>A flag</td>
<td>R flag</td>
</tr>
</tbody>
</table>
<p>When an <a class="reference internal" href="#mailbox.MMDFMessage" title="mailbox.MMDFMessage"><tt class="xref py py-class docutils literal"><span class="pre">MMDFMessage</span></tt></a> instance is created based upon an
<a class="reference internal" href="#mailbox.MHMessage" title="mailbox.MHMessage"><tt class="xref py py-class docutils literal"><span class="pre">MHMessage</span></tt></a> instance, the following conversions take place:</p>
<table border="1" class="docutils">
<colgroup>
<col width="42%" />
<col width="58%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Resulting state</th>
<th class="head"><a class="reference internal" href="#mailbox.MHMessage" title="mailbox.MHMessage"><tt class="xref py py-class docutils literal"><span class="pre">MHMessage</span></tt></a> state</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>R flag and O flag</td>
<td>no &#8220;unseen&#8221; sequence</td>
</tr>
<tr class="row-odd"><td>O flag</td>
<td>&#8220;unseen&#8221; sequence</td>
</tr>
<tr class="row-even"><td>F flag</td>
<td>&#8220;flagged&#8221; sequence</td>
</tr>
<tr class="row-odd"><td>A flag</td>
<td>&#8220;replied&#8221; sequence</td>
</tr>
</tbody>
</table>
<p>When an <a class="reference internal" href="#mailbox.MMDFMessage" title="mailbox.MMDFMessage"><tt class="xref py py-class docutils literal"><span class="pre">MMDFMessage</span></tt></a> instance is created based upon a
<a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><tt class="xref py py-class docutils literal"><span class="pre">BabylMessage</span></tt></a> instance, the following conversions take place:</p>
<table border="1" class="docutils">
<colgroup>
<col width="40%" />
<col width="60%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Resulting state</th>
<th class="head"><a class="reference internal" href="#mailbox.BabylMessage" title="mailbox.BabylMessage"><tt class="xref py py-class docutils literal"><span class="pre">BabylMessage</span></tt></a> state</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>R flag and O flag</td>
<td>no &#8220;unseen&#8221; label</td>
</tr>
<tr class="row-odd"><td>O flag</td>
<td>&#8220;unseen&#8221; label</td>
</tr>
<tr class="row-even"><td>D flag</td>
<td>&#8220;deleted&#8221; label</td>
</tr>
<tr class="row-odd"><td>A flag</td>
<td>&#8220;answered&#8221; label</td>
</tr>
</tbody>
</table>
<p>When an <a class="reference internal" href="#mailbox.MMDFMessage" title="mailbox.MMDFMessage"><tt class="xref py py-class docutils literal"><span class="pre">MMDFMessage</span></tt></a> instance is created based upon an
<a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><tt class="xref py py-class docutils literal"><span class="pre">mboxMessage</span></tt></a> instance, the &#8220;From &#8221; line is copied and all flags directly
correspond:</p>
<table border="1" class="docutils">
<colgroup>
<col width="38%" />
<col width="62%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Resulting state</th>
<th class="head"><a class="reference internal" href="#mailbox.mboxMessage" title="mailbox.mboxMessage"><tt class="xref py py-class docutils literal"><span class="pre">mboxMessage</span></tt></a> state</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>R flag</td>
<td>R flag</td>
</tr>
<tr class="row-odd"><td>O flag</td>
<td>O flag</td>
</tr>
<tr class="row-even"><td>D flag</td>
<td>D flag</td>
</tr>
<tr class="row-odd"><td>F flag</td>
<td>F flag</td>
</tr>
<tr class="row-even"><td>A flag</td>
<td>A flag</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section" id="exceptions">
<h2>18.4.3. Exceptions<a class="headerlink" href="#exceptions" title="Permalink to this headline">¶</a></h2>
<p>The following exception classes are defined in the <a class="reference internal" href="#module-mailbox" title="mailbox: Manipulate mailboxes in various formats"><tt class="xref py py-mod docutils literal"><span class="pre">mailbox</span></tt></a> module:</p>
<dl class="exception">
<dt id="mailbox.Error">
<em class="property">exception </em><tt class="descclassname">mailbox.</tt><tt class="descname">Error</tt><a class="headerlink" href="#mailbox.Error" title="Permalink to this definition">¶</a></dt>
<dd><p>The based class for all other module-specific exceptions.</p>
</dd></dl>

<dl class="exception">
<dt id="mailbox.NoSuchMailboxError">
<em class="property">exception </em><tt class="descclassname">mailbox.</tt><tt class="descname">NoSuchMailboxError</tt><a class="headerlink" href="#mailbox.NoSuchMailboxError" title="Permalink to this definition">¶</a></dt>
<dd><p>Raised when a mailbox is expected but is not found, such as when instantiating a
<a class="reference internal" href="#mailbox.Mailbox" title="mailbox.Mailbox"><tt class="xref py py-class docutils literal"><span class="pre">Mailbox</span></tt></a> subclass with a path that does not exist (and with the <em>create</em>
parameter set to <tt class="docutils literal"><span class="pre">False</span></tt>), or when opening a folder that does not exist.</p>
</dd></dl>

<dl class="exception">
<dt id="mailbox.NotEmptyError">
<em class="property">exception </em><tt class="descclassname">mailbox.</tt><tt class="descname">NotEmptyError</tt><a class="headerlink" href="#mailbox.NotEmptyError" title="Permalink to this definition">¶</a></dt>
<dd><p>Raised when a mailbox is not empty but is expected to be, such as when deleting
a folder that contains messages.</p>
</dd></dl>

<dl class="exception">
<dt id="mailbox.ExternalClashError">
<em class="property">exception </em><tt class="descclassname">mailbox.</tt><tt class="descname">ExternalClashError</tt><a class="headerlink" href="#mailbox.ExternalClashError" title="Permalink to this definition">¶</a></dt>
<dd><p>Raised when some mailbox-related condition beyond the control of the program
causes it to be unable to proceed, such as when failing to acquire a lock that
another program already holds a lock, or when a uniquely-generated file name
already exists.</p>
</dd></dl>

<dl class="exception">
<dt id="mailbox.FormatError">
<em class="property">exception </em><tt class="descclassname">mailbox.</tt><tt class="descname">FormatError</tt><a class="headerlink" href="#mailbox.FormatError" title="Permalink to this definition">¶</a></dt>
<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"><tt class="xref py py-class docutils literal"><span class="pre">MH</span></tt></a>
instance attempts to read a corrupted <tt class="file docutils literal"><span class="pre">.mh_sequences</span></tt> file.</p>
</dd></dl>

</div>
<div class="section" id="deprecated-classes-and-methods">
<span id="mailbox-deprecated"></span><h2>18.4.4. Deprecated classes and methods<a class="headerlink" href="#deprecated-classes-and-methods" title="Permalink to this headline">¶</a></h2>
<p class="deprecated">
<span class="versionmodified">Deprecated since version 2.6.</span></p>
<p>Older versions of the <a class="reference internal" href="#module-mailbox" title="mailbox: Manipulate mailboxes in various formats"><tt class="xref py py-mod docutils literal"><span class="pre">mailbox</span></tt></a> module do not support modification of
mailboxes, such as adding or removing message, and do not provide classes to
represent format-specific message properties. For backward compatibility, the
older mailbox classes are still available, but the newer classes should be used
in preference to them.  The old classes have been removed in Python 3.</p>
<p>Older mailbox objects support only iteration and provide a single public method:</p>
<dl class="method">
<dt id="mailbox.oldmailbox.next">
<tt class="descclassname">oldmailbox.</tt><tt class="descname">next</tt><big>(</big><big>)</big><a class="headerlink" href="#mailbox.oldmailbox.next" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the next message in the mailbox, created with the optional <em>factory</em>
argument passed into the mailbox object&#8217;s constructor. By default this is an
<a class="reference internal" href="rfc822.html#rfc822.Message" title="rfc822.Message"><tt class="xref py py-class docutils literal"><span class="pre">rfc822.Message</span></tt></a> object (see the <a class="reference internal" href="rfc822.html#module-rfc822" title="rfc822: Parse 2822 style mail messages. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">rfc822</span></tt></a> module).  Depending on the
mailbox implementation the <em>fp</em> attribute of this object may be a true file
object or a class instance simulating a file object, taking care of things like
message boundaries if multiple mail messages are contained in a single file,
etc.  If no more messages are available, this method returns <tt class="docutils literal"><span class="pre">None</span></tt>.</p>
</dd></dl>

<p>Most of the older mailbox classes have names that differ from the current
mailbox class names, except for <a class="reference internal" href="#mailbox.Maildir" title="mailbox.Maildir"><tt class="xref py py-class docutils literal"><span class="pre">Maildir</span></tt></a>. For this reason, the new
<a class="reference internal" href="#mailbox.Maildir" title="mailbox.Maildir"><tt class="xref py py-class docutils literal"><span class="pre">Maildir</span></tt></a> class defines a <tt class="xref py py-meth docutils literal"><span class="pre">next()</span></tt> method and its constructor differs
slightly from those of the other new mailbox classes.</p>
<p>The older mailbox classes whose names are not the same as their newer
counterparts are as follows:</p>
<dl class="class">
<dt id="mailbox.UnixMailbox">
<em class="property">class </em><tt class="descclassname">mailbox.</tt><tt class="descname">UnixMailbox</tt><big>(</big><em>fp</em><span class="optional">[</span>, <em>factory</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#mailbox.UnixMailbox" title="Permalink to this definition">¶</a></dt>
<dd><p>Access to a classic Unix-style mailbox, where all messages are contained in a
single file and separated by <tt class="docutils literal"><span class="pre">From</span></tt> (a.k.a. <tt class="docutils literal"><span class="pre">From_</span></tt>) lines.  The file object
<em>fp</em> points to the mailbox file.  The optional <em>factory</em> parameter is a callable
that should create new message objects.  <em>factory</em> is called with one argument,
<em>fp</em> by the <tt class="xref py py-meth docutils literal"><span class="pre">next()</span></tt> method of the mailbox object.  The default is the
<a class="reference internal" href="rfc822.html#rfc822.Message" title="rfc822.Message"><tt class="xref py py-class docutils literal"><span class="pre">rfc822.Message</span></tt></a> class (see the <a class="reference internal" href="rfc822.html#module-rfc822" title="rfc822: Parse 2822 style mail messages. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">rfc822</span></tt></a> module &#8211; and the note
below).</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">For reasons of this module&#8217;s internal implementation, you will probably want to
open the <em>fp</em> object in binary mode.  This is especially important on Windows.</p>
</div>
<p>For maximum portability, messages in a Unix-style mailbox are separated by any
line that begins exactly with the string <tt class="docutils literal"><span class="pre">'From</span> <span class="pre">'</span></tt> (note the trailing space)
if preceded by exactly two newlines. Because of the wide-range of variations in
practice, nothing else on the <tt class="docutils literal"><span class="pre">From_</span></tt> line should be considered.  However, the
current implementation doesn&#8217;t check for the leading two newlines.  This is
usually fine for most applications.</p>
<p>The <a class="reference internal" href="#mailbox.UnixMailbox" title="mailbox.UnixMailbox"><tt class="xref py py-class docutils literal"><span class="pre">UnixMailbox</span></tt></a> class implements a more strict version of <tt class="docutils literal"><span class="pre">From_</span></tt>
line checking, using a regular expression that usually correctly matched
<tt class="docutils literal"><span class="pre">From_</span></tt> delimiters.  It considers delimiter line to be separated by <tt class="docutils literal"><span class="pre">From</span>
<span class="pre">name</span> <span class="pre">time</span></tt> lines.  For maximum portability, use the
<a class="reference internal" href="#mailbox.PortableUnixMailbox" title="mailbox.PortableUnixMailbox"><tt class="xref py py-class docutils literal"><span class="pre">PortableUnixMailbox</span></tt></a> class instead.  This class is identical to
<a class="reference internal" href="#mailbox.UnixMailbox" title="mailbox.UnixMailbox"><tt class="xref py py-class docutils literal"><span class="pre">UnixMailbox</span></tt></a> except that individual messages are separated by only
<tt class="docutils literal"><span class="pre">From</span></tt> lines.</p>
</dd></dl>

<dl class="class">
<dt id="mailbox.PortableUnixMailbox">
<em class="property">class </em><tt class="descclassname">mailbox.</tt><tt class="descname">PortableUnixMailbox</tt><big>(</big><em>fp</em><span class="optional">[</span>, <em>factory</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#mailbox.PortableUnixMailbox" title="Permalink to this definition">¶</a></dt>
<dd><p>A less-strict version of <a class="reference internal" href="#mailbox.UnixMailbox" title="mailbox.UnixMailbox"><tt class="xref py py-class docutils literal"><span class="pre">UnixMailbox</span></tt></a>, which considers only the <tt class="docutils literal"><span class="pre">From</span></tt>
at the beginning of the line separating messages.  The &#8220;<em>name</em> <em>time</em>&#8221; portion
of the From line is ignored, to protect against some variations that are
observed in practice.  This works since lines in the message which begin with
<tt class="docutils literal"><span class="pre">'From</span> <span class="pre">'</span></tt> are quoted by mail handling software at delivery-time.</p>
</dd></dl>

<dl class="class">
<dt id="mailbox.MmdfMailbox">
<em class="property">class </em><tt class="descclassname">mailbox.</tt><tt class="descname">MmdfMailbox</tt><big>(</big><em>fp</em><span class="optional">[</span>, <em>factory</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#mailbox.MmdfMailbox" title="Permalink to this definition">¶</a></dt>
<dd><p>Access an MMDF-style mailbox, where all messages are contained in a single file
and separated by lines consisting of 4 control-A characters.  The file object
<em>fp</em> points to the mailbox file. Optional <em>factory</em> is as with the
<a class="reference internal" href="#mailbox.UnixMailbox" title="mailbox.UnixMailbox"><tt class="xref py py-class docutils literal"><span class="pre">UnixMailbox</span></tt></a> class.</p>
</dd></dl>

<dl class="class">
<dt id="mailbox.MHMailbox">
<em class="property">class </em><tt class="descclassname">mailbox.</tt><tt class="descname">MHMailbox</tt><big>(</big><em>dirname</em><span class="optional">[</span>, <em>factory</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#mailbox.MHMailbox" title="Permalink to this definition">¶</a></dt>
<dd><p>Access an MH mailbox, a directory with each message in a separate file with a
numeric name. The name of the mailbox directory is passed in <em>dirname</em>.
<em>factory</em> is as with the <a class="reference internal" href="#mailbox.UnixMailbox" title="mailbox.UnixMailbox"><tt class="xref py py-class docutils literal"><span class="pre">UnixMailbox</span></tt></a> class.</p>
</dd></dl>

<dl class="class">
<dt id="mailbox.BabylMailbox">
<em class="property">class </em><tt class="descclassname">mailbox.</tt><tt class="descname">BabylMailbox</tt><big>(</big><em>fp</em><span class="optional">[</span>, <em>factory</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#mailbox.BabylMailbox" title="Permalink to this definition">¶</a></dt>
<dd><p>Access a Babyl mailbox, which is similar to an MMDF mailbox.  In Babyl format,
each message has two sets of headers, the <em>original</em> headers and the <em>visible</em>
headers.  The original headers appear before a line containing only <tt class="docutils literal"><span class="pre">'***</span> <span class="pre">EOOH</span>
<span class="pre">***'</span></tt> (End-Of-Original-Headers) and the visible headers appear after the
<tt class="docutils literal"><span class="pre">EOOH</span></tt> line.  Babyl-compliant mail readers will show you only the visible
headers, and <a class="reference internal" href="#mailbox.BabylMailbox" title="mailbox.BabylMailbox"><tt class="xref py py-class docutils literal"><span class="pre">BabylMailbox</span></tt></a> objects will return messages containing only
the visible headers.  You&#8217;ll have to do your own parsing of the mailbox file to
get at the original headers.  Mail messages start with the EOOH line and end
with a line containing only <tt class="docutils literal"><span class="pre">'\037\014'</span></tt>.  <em>factory</em> is as with the
<a class="reference internal" href="#mailbox.UnixMailbox" title="mailbox.UnixMailbox"><tt class="xref py py-class docutils literal"><span class="pre">UnixMailbox</span></tt></a> class.</p>
</dd></dl>

<p>If you wish to use the older mailbox classes with the <a class="reference internal" href="email.html#module-email" title="email: Package supporting the parsing, manipulating, and generating email messages, including MIME documents."><tt class="xref py py-mod docutils literal"><span class="pre">email</span></tt></a> module rather
than the deprecated <a class="reference internal" href="rfc822.html#module-rfc822" title="rfc822: Parse 2822 style mail messages. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">rfc822</span></tt></a> module, you can do so as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">email</span>
<span class="kn">import</span> <span class="nn">email.Errors</span>
<span class="kn">import</span> <span class="nn">mailbox</span>

<span class="k">def</span> <span class="nf">msgfactory</span><span class="p">(</span><span class="n">fp</span><span class="p">):</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">email</span><span class="o">.</span><span class="n">message_from_file</span><span class="p">(</span><span class="n">fp</span><span class="p">)</span>
    <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>
        <span class="c"># Don&#39;t return None since that will</span>
        <span class="c"># stop the mailbox iterator</span>
        <span class="k">return</span> <span class="s">&#39;&#39;</span>

<span class="n">mbox</span> <span class="o">=</span> <span class="n">mailbox</span><span class="o">.</span><span class="n">UnixMailbox</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="n">msgfactory</span><span class="p">)</span>
</pre></div>
</div>
<p>Alternatively, if you know your mailbox contains only well-formed MIME messages,
you can simplify this to:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">email</span>
<span class="kn">import</span> <span class="nn">mailbox</span>

<span class="n">mbox</span> <span class="o">=</span> <span class="n">mailbox</span><span class="o">.</span><span class="n">UnixMailbox</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span> <span class="n">email</span><span class="o">.</span><span class="n">message_from_file</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="examples">
<span id="mailbox-examples"></span><h2>18.4.5. Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h2>
<p>A simple example of printing the subjects of all messages in a mailbox that seem
interesting:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">mailbox</span>
<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="s">&#39;~/mbox&#39;</span><span class="p">):</span>
    <span class="n">subject</span> <span class="o">=</span> <span class="n">message</span><span class="p">[</span><span class="s">&#39;subject&#39;</span><span class="p">]</span>       <span class="c"># Could possibly be None.</span>
    <span class="k">if</span> <span class="n">subject</span> <span class="ow">and</span> <span class="s">&#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>
        <span class="k">print</span> <span class="n">subject</span>
</pre></div>
</div>
<p>To copy all mail from a Babyl mailbox to an MH mailbox, converting all of the
format-specific information that can be converted:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">mailbox</span>
<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="s">&#39;~/Mail&#39;</span><span class="p">)</span>
<span class="n">destination</span><span class="o">.</span><span class="n">lock</span><span class="p">()</span>
<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="s">&#39;~/RMAIL&#39;</span><span class="p">):</span>
    <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>
<span class="n">destination</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
<span class="n">destination</span><span class="o">.</span><span class="n">unlock</span><span class="p">()</span>
</pre></div>
</div>
<p>This example sorts mail from several mailing lists into different mailboxes,
being careful to avoid mail corruption due to concurrent modification by other
programs, mail loss due to interruption of the program, or premature termination
due to malformed messages in the mailbox:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">mailbox</span>
<span class="kn">import</span> <span class="nn">email.Errors</span>

<span class="n">list_names</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;python-list&#39;</span><span class="p">,</span> <span class="s">&#39;python-dev&#39;</span><span class="p">,</span> <span class="s">&#39;python-bugs&#39;</span><span class="p">)</span>

<span class="n">boxes</span> <span class="o">=</span> <span class="nb">dict</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="s">&#39;~/email/</span><span class="si">%s</span><span class="s">&#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>
<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="s">&#39;~/Maildir&#39;</span><span class="p">,</span> <span class="n">factory</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>

<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>
    <span class="k">try</span><span class="p">:</span>
        <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>
    <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>
        <span class="k">continue</span>                <span class="c"># The message is malformed. Just leave it.</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>
        <span class="n">list_id</span> <span class="o">=</span> <span class="n">message</span><span class="p">[</span><span class="s">&#39;list-id&#39;</span><span class="p">]</span>
        <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>
            <span class="c"># Get mailbox to use</span>
            <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>

            <span class="c"># Write copy to disk before removing original.</span>
            <span class="c"># If there&#39;s a crash, you might duplicate a message, but</span>
            <span class="c"># that&#39;s better than losing a message completely.</span>
            <span class="n">box</span><span class="o">.</span><span class="n">lock</span><span class="p">()</span>
            <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>
            <span class="n">box</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
            <span class="n">box</span><span class="o">.</span><span class="n">unlock</span><span class="p">()</span>

            <span class="c"># Remove original message</span>
            <span class="n">inbox</span><span class="o">.</span><span class="n">lock</span><span class="p">()</span>
            <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>
            <span class="n">inbox</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
            <span class="n">inbox</span><span class="o">.</span><span class="n">unlock</span><span class="p">()</span>
            <span class="k">break</span>               <span class="c"># Found destination, so stop looking.</span>

<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>
    <span class="n">box</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">18.4. <tt class="docutils literal"><span class="pre">mailbox</span></tt> &#8212; Manipulate mailboxes in various formats</a><ul>
<li><a class="reference internal" href="#mailbox-objects">18.4.1. <tt class="docutils literal"><span class="pre">Mailbox</span></tt> objects</a><ul>
<li><a class="reference internal" href="#maildir">18.4.1.1. <tt class="docutils literal"><span class="pre">Maildir</span></tt></a></li>
<li><a class="reference internal" href="#mbox">18.4.1.2. <tt class="docutils literal"><span class="pre">mbox</span></tt></a></li>
<li><a class="reference internal" href="#mh">18.4.1.3. <tt class="docutils literal"><span class="pre">MH</span></tt></a></li>
<li><a class="reference internal" href="#babyl">18.4.1.4. <tt class="docutils literal"><span class="pre">Babyl</span></tt></a></li>
<li><a class="reference internal" href="#mmdf">18.4.1.5. <tt class="docutils literal"><span class="pre">MMDF</span></tt></a></li>
</ul>
</li>
<li><a class="reference internal" href="#message-objects">18.4.2. <tt class="docutils literal"><span class="pre">Message</span></tt> objects</a><ul>
<li><a class="reference internal" href="#maildirmessage">18.4.2.1. <tt class="docutils literal"><span class="pre">MaildirMessage</span></tt></a></li>
<li><a class="reference internal" href="#mboxmessage">18.4.2.2. <tt class="docutils literal"><span class="pre">mboxMessage</span></tt></a></li>
<li><a class="reference internal" href="#mhmessage">18.4.2.3. <tt class="docutils literal"><span class="pre">MHMessage</span></tt></a></li>
<li><a class="reference internal" href="#babylmessage">18.4.2.4. <tt class="docutils literal"><span class="pre">BabylMessage</span></tt></a></li>
<li><a class="reference internal" href="#mmdfmessage">18.4.2.5. <tt class="docutils literal"><span class="pre">MMDFMessage</span></tt></a></li>
</ul>
</li>
<li><a class="reference internal" href="#exceptions">18.4.3. Exceptions</a></li>
<li><a class="reference internal" href="#deprecated-classes-and-methods">18.4.4. Deprecated classes and methods</a></li>
<li><a class="reference internal" href="#examples">18.4.5. Examples</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="mailcap.html"
                        title="previous chapter">18.3. <tt class="docutils literal"><span class="pre">mailcap</span></tt> &#8212; Mailcap file handling</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="mhlib.html"
                        title="next chapter">18.5. <tt class="docutils literal"><span class="pre">mhlib</span></tt> &#8212; Access to MH mailboxes</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/library/mailbox.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="mhlib.html" title="18.5. mhlib — Access to MH mailboxes"
             >next</a> |</li>
        <li class="right" >
          <a href="mailcap.html" title="18.3. mailcap — Mailcap file handling"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="http://www.python.org/">Python</a> &raquo;</li>
        <li>
          <a href="../index.html">Python 2.7.5 documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="netdata.html" >18. Internet Data Handling</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2020, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Oct 13, 2020.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>

  </body>
</html>

ZeroDay Forums Mini