blob: 961b05019ecd4916c7b9c0f6c3fc8d574625a1d8 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{mailbox} ---
Thomas Wouters477c8d52006-05-27 19:21:47 +00002 Manipulate mailboxes in various formats}
Fred Drake199b79c1999-02-20 05:04:59 +00003
Thomas Wouters477c8d52006-05-27 19:21:47 +00004\declaremodule{}{mailbox}
5\moduleauthor{Gregory K.~Johnson}{gkj@gregorykjohnson.com}
6\sectionauthor{Gregory K.~Johnson}{gkj@gregorykjohnson.com}
7\modulesynopsis{Manipulate mailboxes in various formats}
Fred Drakeb91e9341998-07-23 17:59:49 +00008
Guido van Rossum39a23cc1997-06-02 21:04:41 +00009
Thomas Wouters477c8d52006-05-27 19:21:47 +000010This module defines two classes, \class{Mailbox} and \class{Message}, for
11accessing and manipulating on-disk mailboxes and the messages they contain.
12\class{Mailbox} offers a dictionary-like mapping from keys to messages.
13\class{Message} extends the \module{email.Message} module's \class{Message}
14class with format-specific state and behavior. Supported mailbox formats are
15Maildir, mbox, MH, Babyl, and MMDF.
16
17\begin{seealso}
18 \seemodule{email}{Represent and manipulate messages.}
19\end{seealso}
20
21\subsection{\class{Mailbox} objects}
22\label{mailbox-objects}
23
24\begin{classdesc*}{Mailbox}
25A mailbox, which may be inspected and modified.
26\end{classdesc*}
27
Thomas Woutersb2137042007-02-01 18:02:27 +000028The \class{Mailbox} class defines an interface and
29is not intended to be instantiated. Instead, format-specific
30subclasses should inherit from \class{Mailbox} and your code
31should instantiate a particular subclass.
32
Thomas Wouters477c8d52006-05-27 19:21:47 +000033The \class{Mailbox} interface is dictionary-like, with small keys
Thomas Woutersb2137042007-02-01 18:02:27 +000034corresponding to messages. Keys are issued by the \class{Mailbox}
35instance with which they will be used and are only meaningful to that
36\class{Mailbox} instance. A key continues to identify a message even
37if the corresponding message is modified, such as by replacing it with
38another message.
39
40Messages may be added to a \class{Mailbox} instance using the set-like
41method \method{add()} and removed using a \code{del} statement or the
42set-like methods \method{remove()} and \method{discard()}.
Thomas Wouters477c8d52006-05-27 19:21:47 +000043
44\class{Mailbox} interface semantics differ from dictionary semantics in some
Thomas Woutersb2137042007-02-01 18:02:27 +000045noteworthy ways. Each time a message is requested, a new
46representation (typically a \class{Message} instance) is generated
47based upon the current state of the mailbox. Similarly, when a message
48is added to a \class{Mailbox} instance, the provided message
49representation's contents are copied. In neither case is a reference
50to the message representation kept by the \class{Mailbox} instance.
Thomas Wouters477c8d52006-05-27 19:21:47 +000051
52The default \class{Mailbox} iterator iterates over message representations, not
53keys as the default dictionary iterator does. Moreover, modification of a
54mailbox during iteration is safe and well-defined. Messages added to the
55mailbox after an iterator is created will not be seen by the iterator. Messages
56removed from the mailbox before the iterator yields them will be silently
57skipped, though using a key from an iterator may result in a
58\exception{KeyError} exception if the corresponding message is subsequently
59removed.
60
Thomas Woutersb2137042007-02-01 18:02:27 +000061Be very cautious when modifying mailboxes that might also be changed
62by some other process. The safest mailbox format to use for such
63tasks is Maildir; try to avoid using single-file formats such as mbox
64for concurrent writing. If you're modifying a mailbox, no matter what
65the format, you must lock it by calling the \method{lock()} and
66\method{unlock()} methods before making any changes. Failing to lock
67the mailbox runs the risk of losing data if some other process makes
68changes to the mailbox while your Python code is running.
Thomas Wouters477c8d52006-05-27 19:21:47 +000069
70\class{Mailbox} instances have the following methods:
71
72\begin{methoddesc}{add}{message}
73Add \var{message} to the mailbox and return the key that has been assigned to
74it.
75
76Parameter \var{message} may be a \class{Message} instance, an
77\class{email.Message.Message} instance, a string, or a file-like object (which
78should be open in text mode). If \var{message} is an instance of the
79appropriate format-specific \class{Message} subclass (e.g., if it's an
80\class{mboxMessage} instance and this is an \class{mbox} instance), its
81format-specific information is used. Otherwise, reasonable defaults for
82format-specific information are used.
83\end{methoddesc}
84
85\begin{methoddesc}{remove}{key}
86\methodline{__delitem__}{key}
87\methodline{discard}{key}
88Delete the message corresponding to \var{key} from the mailbox.
89
90If no such message exists, a \exception{KeyError} exception is raised if the
91method was called as \method{remove()} or \method{__delitem__()} but no
92exception is raised if the method was called as \method{discard()}. The
93behavior of \method{discard()} may be preferred if the underlying mailbox
94format supports concurrent modification by other processes.
95\end{methoddesc}
96
97\begin{methoddesc}{__setitem__}{key, message}
98Replace the message corresponding to \var{key} with \var{message}. Raise a
99\exception{KeyError} exception if no message already corresponds to \var{key}.
100
101As with \method{add()}, parameter \var{message} may be a \class{Message}
102instance, an \class{email.Message.Message} instance, a string, or a file-like
103object (which should be open in text mode). If \var{message} is an instance of
104the appropriate format-specific \class{Message} subclass (e.g., if it's an
105\class{mboxMessage} instance and this is an \class{mbox} instance), its
106format-specific information is used. Otherwise, the format-specific information
107of the message that currently corresponds to \var{key} is left unchanged.
108\end{methoddesc}
109
110\begin{methoddesc}{iterkeys}{}
111\methodline{keys}{}
112Return an iterator over all keys if called as \method{iterkeys()} or return a
113list of keys if called as \method{keys()}.
114\end{methoddesc}
115
116\begin{methoddesc}{itervalues}{}
117\methodline{__iter__}{}
118\methodline{values}{}
119Return an iterator over representations of all messages if called as
120\method{itervalues()} or \method{__iter__()} or return a list of such
121representations if called as \method{values()}. The messages are represented as
122instances of the appropriate format-specific \class{Message} subclass unless a
123custom message factory was specified when the \class{Mailbox} instance was
124initialized. \note{The behavior of \method{__iter__()} is unlike that of
125dictionaries, which iterate over keys.}
126\end{methoddesc}
127
128\begin{methoddesc}{iteritems}{}
129\methodline{items}{}
130Return an iterator over (\var{key}, \var{message}) pairs, where \var{key} is a
131key and \var{message} is a message representation, if called as
132\method{iteritems()} or return a list of such pairs if called as
133\method{items()}. The messages are represented as instances of the appropriate
134format-specific \class{Message} subclass unless a custom message factory was
135specified when the \class{Mailbox} instance was initialized.
136\end{methoddesc}
137
138\begin{methoddesc}{get}{key\optional{, default=None}}
139\methodline{__getitem__}{key}
140Return a representation of the message corresponding to \var{key}. If no such
141message exists, \var{default} is returned if the method was called as
142\method{get()} and a \exception{KeyError} exception is raised if the method was
143called as \method{__getitem__()}. The message is represented as an instance of
144the appropriate format-specific \class{Message} subclass unless a custom
145message factory was specified when the \class{Mailbox} instance was
146initialized.
147\end{methoddesc}
148
149\begin{methoddesc}{get_message}{key}
150Return a representation of the message corresponding to \var{key} as an
151instance of the appropriate format-specific \class{Message} subclass, or raise
152a \exception{KeyError} exception if no such message exists.
153\end{methoddesc}
154
155\begin{methoddesc}{get_string}{key}
156Return a string representation of the message corresponding to \var{key}, or
157raise a \exception{KeyError} exception if no such message exists.
158\end{methoddesc}
159
160\begin{methoddesc}{get_file}{key}
161Return a file-like representation of the message corresponding to \var{key},
162or raise a \exception{KeyError} exception if no such message exists. The
163file-like object behaves as if open in binary mode. This file should be closed
164once it is no longer needed.
165
166\note{Unlike other representations of messages, file-like representations are
167not necessarily independent of the \class{Mailbox} instance that created them
168or of the underlying mailbox. More specific documentation is provided by each
169subclass.}
170\end{methoddesc}
171
172\begin{methoddesc}{has_key}{key}
173\methodline{__contains__}{key}
174Return \code{True} if \var{key} corresponds to a message, \code{False}
175otherwise.
176\end{methoddesc}
177
178\begin{methoddesc}{__len__}{}
179Return a count of messages in the mailbox.
180\end{methoddesc}
181
182\begin{methoddesc}{clear}{}
183Delete all messages from the mailbox.
184\end{methoddesc}
185
186\begin{methoddesc}{pop}{key\optional{, default}}
187Return a representation of the message corresponding to \var{key} and delete
188the message. If no such message exists, return \var{default} if it was supplied
189or else raise a \exception{KeyError} exception. The message is represented as
190an instance of the appropriate format-specific \class{Message} subclass unless
191a custom message factory was specified when the \class{Mailbox} instance was
192initialized.
193\end{methoddesc}
194
195\begin{methoddesc}{popitem}{}
196Return an arbitrary (\var{key}, \var{message}) pair, where \var{key} is a key
197and \var{message} is a message representation, and delete the corresponding
198message. If the mailbox is empty, raise a \exception{KeyError} exception. The
199message is represented as an instance of the appropriate format-specific
200\class{Message} subclass unless a custom message factory was specified when the
201\class{Mailbox} instance was initialized.
202\end{methoddesc}
203
204\begin{methoddesc}{update}{arg}
205Parameter \var{arg} should be a \var{key}-to-\var{message} mapping or an
206iterable of (\var{key}, \var{message}) pairs. Updates the mailbox so that, for
207each given \var{key} and \var{message}, the message corresponding to \var{key}
208is set to \var{message} as if by using \method{__setitem__()}. As with
209\method{__setitem__()}, each \var{key} must already correspond to a message in
210the mailbox or else a \exception{KeyError} exception will be raised, so in
211general it is incorrect for \var{arg} to be a \class{Mailbox} instance.
212\note{Unlike with dictionaries, keyword arguments are not supported.}
213\end{methoddesc}
214
215\begin{methoddesc}{flush}{}
216Write any pending changes to the filesystem. For some \class{Mailbox}
Thomas Woutersb2137042007-02-01 18:02:27 +0000217subclasses, changes are always written immediately and \method{flush()} does
218nothing, but you should still make a habit of calling this method.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000219\end{methoddesc}
220
221\begin{methoddesc}{lock}{}
222Acquire an exclusive advisory lock on the mailbox so that other processes know
223not to modify it. An \exception{ExternalClashError} is raised if the lock is
224not available. The particular locking mechanisms used depend upon the mailbox
Thomas Woutersb2137042007-02-01 18:02:27 +0000225format. You should \emph{always} lock the mailbox before making any
226modifications to its contents.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000227\end{methoddesc}
228
229\begin{methoddesc}{unlock}{}
230Release the lock on the mailbox, if any.
231\end{methoddesc}
232
233\begin{methoddesc}{close}{}
234Flush the mailbox, unlock it if necessary, and close any open files. For some
235\class{Mailbox} subclasses, this method does nothing.
236\end{methoddesc}
237
238
239\subsubsection{\class{Maildir}}
240\label{mailbox-maildir}
241
242\begin{classdesc}{Maildir}{dirname\optional{, factory=rfc822.Message\optional{,
243create=True}}}
244A subclass of \class{Mailbox} for mailboxes in Maildir format. Parameter
245\var{factory} is a callable object that accepts a file-like message
246representation (which behaves as if opened in binary mode) and returns a custom
247representation. If \var{factory} is \code{None}, \class{MaildirMessage} is used
248as the default message representation. If \var{create} is \code{True}, the
249mailbox is created if it does not exist.
250
251It is for historical reasons that \var{factory} defaults to
252\class{rfc822.Message} and that \var{dirname} is named as such rather than
253\var{path}. For a \class{Maildir} instance that behaves like instances of other
254\class{Mailbox} subclasses, set \var{factory} to \code{None}.
255\end{classdesc}
256
257Maildir is a directory-based mailbox format invented for the qmail mail
258transfer agent and now widely supported by other programs. Messages in a
259Maildir mailbox are stored in separate files within a common directory
260structure. This design allows Maildir mailboxes to be accessed and modified by
261multiple unrelated programs without data corruption, so file locking is
262unnecessary.
263
264Maildir mailboxes contain three subdirectories, namely: \file{tmp}, \file{new},
265and \file{cur}. Messages are created momentarily in the \file{tmp} subdirectory
266and then moved to the \file{new} subdirectory to finalize delivery. A mail user
267agent may subsequently move the message to the \file{cur} subdirectory and
268store information about the state of the message in a special "info" section
269appended to its file name.
270
271Folders of the style introduced by the Courier mail transfer agent are also
272supported. Any subdirectory of the main mailbox is considered a folder if
273\character{.} is the first character in its name. Folder names are represented
274by \class{Maildir} without the leading \character{.}. Each folder is itself a
275Maildir mailbox but should not contain other folders. Instead, a logical
276nesting is indicated using \character{.} to delimit levels, e.g.,
277"Archived.2005.07".
278
279\begin{notice}
280The Maildir specification requires the use of a colon (\character{:}) in
281certain message file names. However, some operating systems do not permit this
282character in file names, If you wish to use a Maildir-like format on such an
283operating system, you should specify another character to use instead. The
284exclamation point (\character{!}) is a popular choice. For example:
285\begin{verbatim}
286import mailbox
287mailbox.Maildir.colon = '!'
288\end{verbatim}
289The \member{colon} attribute may also be set on a per-instance basis.
290\end{notice}
291
292\class{Maildir} instances have all of the methods of \class{Mailbox} in
293addition to the following:
294
295\begin{methoddesc}{list_folders}{}
296Return a list of the names of all folders.
297\end{methoddesc}
298
299\begin{methoddesc}{get_folder}{folder}
300Return a \class{Maildir} instance representing the folder whose name is
301\var{folder}. A \exception{NoSuchMailboxError} exception is raised if the
302folder does not exist.
303\end{methoddesc}
304
305\begin{methoddesc}{add_folder}{folder}
306Create a folder whose name is \var{folder} and return a \class{Maildir}
307instance representing it.
308\end{methoddesc}
309
310\begin{methoddesc}{remove_folder}{folder}
311Delete the folder whose name is \var{folder}. If the folder contains any
312messages, a \exception{NotEmptyError} exception will be raised and the folder
313will not be deleted.
314\end{methoddesc}
315
316\begin{methoddesc}{clean}{}
317Delete temporary files from the mailbox that have not been accessed in the
318last 36 hours. The Maildir specification says that mail-reading programs
319should do this occasionally.
320\end{methoddesc}
321
322Some \class{Mailbox} methods implemented by \class{Maildir} deserve special
323remarks:
324
325\begin{methoddesc}{add}{message}
326\methodline[Maildir]{__setitem__}{key, message}
327\methodline[Maildir]{update}{arg}
328\warning{These methods generate unique file names based upon the current
329process ID. When using multiple threads, undetected name clashes may occur and
330cause corruption of the mailbox unless threads are coordinated to avoid using
331these methods to manipulate the same mailbox simultaneously.}
332\end{methoddesc}
333
334\begin{methoddesc}{flush}{}
335All changes to Maildir mailboxes are immediately applied, so this method does
336nothing.
337\end{methoddesc}
338
339\begin{methoddesc}{lock}{}
340\methodline{unlock}{}
341Maildir mailboxes do not support (or require) locking, so these methods do
342nothing.
343\end{methoddesc}
344
345\begin{methoddesc}{close}{}
346\class{Maildir} instances do not keep any open files and the underlying
347mailboxes do not support locking, so this method does nothing.
348\end{methoddesc}
349
350\begin{methoddesc}{get_file}{key}
351Depending upon the host platform, it may not be possible to modify or remove
352the underlying message while the returned file remains open.
353\end{methoddesc}
354
355\begin{seealso}
356 \seelink{http://www.qmail.org/man/man5/maildir.html}{maildir man page from
357 qmail}{The original specification of the format.}
358 \seelink{http://cr.yp.to/proto/maildir.html}{Using maildir format}{Notes
359 on Maildir by its inventor. Includes an updated name-creation scheme and
360 details on "info" semantics.}
361 \seelink{http://www.courier-mta.org/?maildir.html}{maildir man page from
362 Courier}{Another specification of the format. Describes a common extension
363 for supporting folders.}
364\end{seealso}
365
366\subsubsection{\class{mbox}}
367\label{mailbox-mbox}
368
369\begin{classdesc}{mbox}{path\optional{, factory=None\optional{, create=True}}}
370A subclass of \class{Mailbox} for mailboxes in mbox format. Parameter
371\var{factory} is a callable object that accepts a file-like message
372representation (which behaves as if opened in binary mode) and returns a custom
373representation. If \var{factory} is \code{None}, \class{mboxMessage} is used as
374the default message representation. If \var{create} is \code{True}, the mailbox
375is created if it does not exist.
376\end{classdesc}
377
378The mbox format is the classic format for storing mail on \UNIX{} systems. All
379messages in an mbox mailbox are stored in a single file with the beginning of
380each message indicated by a line whose first five characters are "From~".
381
382Several variations of the mbox format exist to address perceived shortcomings
383in the original. In the interest of compatibility, \class{mbox} implements the
384original format, which is sometimes referred to as \dfn{mboxo}. This means that
385the \mailheader{Content-Length} header, if present, is ignored and that any
386occurrences of "From~" at the beginning of a line in a message body are
387transformed to ">From~" when storing the message, although occurences of
388">From~" are not transformed to "From~" when reading the message.
389
390Some \class{Mailbox} methods implemented by \class{mbox} deserve special
391remarks:
392
393\begin{methoddesc}{get_file}{key}
394Using the file after calling \method{flush()} or \method{close()} on the
395\class{mbox} instance may yield unpredictable results or raise an exception.
396\end{methoddesc}
397
398\begin{methoddesc}{lock}{}
399\methodline{unlock}{}
400Three locking mechanisms are used---dot locking and, if available, the
401\cfunction{flock()} and \cfunction{lockf()} system calls.
402\end{methoddesc}
403
404\begin{seealso}
405 \seelink{http://www.qmail.org/man/man5/mbox.html}{mbox man page from
406 qmail}{A specification of the format and its variations.}
407 \seelink{http://www.tin.org/bin/man.cgi?section=5\&topic=mbox}{mbox man
408 page from tin}{Another specification of the format, with details on
409 locking.}
410 \seelink{http://home.netscape.com/eng/mozilla/2.0/relnotes/demo/content-length.html}
411 {Configuring Netscape Mail on \UNIX{}: Why The Content-Length Format is
412 Bad}{An argument for using the original mbox format rather than a
413 variation.}
414 \seelink{http://homepages.tesco.net./\tilde{}J.deBoynePollard/FGA/mail-mbox-formats.html}
415 {"mbox" is a family of several mutually incompatible mailbox formats}{A
416 history of mbox variations.}
417\end{seealso}
418
419\subsubsection{\class{MH}}
420\label{mailbox-mh}
421
422\begin{classdesc}{MH}{path\optional{, factory=None\optional{, create=True}}}
423A subclass of \class{Mailbox} for mailboxes in MH format. Parameter
424\var{factory} is a callable object that accepts a file-like message
425representation (which behaves as if opened in binary mode) and returns a custom
426representation. If \var{factory} is \code{None}, \class{MHMessage} is used as
427the default message representation. If \var{create} is \code{True}, the mailbox
428is created if it does not exist.
429\end{classdesc}
430
431MH is a directory-based mailbox format invented for the MH Message Handling
432System, a mail user agent. Each message in an MH mailbox resides in its own
433file. An MH mailbox may contain other MH mailboxes (called \dfn{folders}) in
434addition to messages. Folders may be nested indefinitely. MH mailboxes also
435support \dfn{sequences}, which are named lists used to logically group messages
436without moving them to sub-folders. Sequences are defined in a file called
437\file{.mh_sequences} in each folder.
438
439The \class{MH} class manipulates MH mailboxes, but it does not attempt to
440emulate all of \program{mh}'s behaviors. In particular, it does not modify and
441is not affected by the \file{context} or \file{.mh_profile} files that are used
442by \program{mh} to store its state and configuration.
443
444\class{MH} instances have all of the methods of \class{Mailbox} in addition to
445the following:
446
447\begin{methoddesc}{list_folders}{}
448Return a list of the names of all folders.
449\end{methoddesc}
450
451\begin{methoddesc}{get_folder}{folder}
452Return an \class{MH} instance representing the folder whose name is
453\var{folder}. A \exception{NoSuchMailboxError} exception is raised if the
454folder does not exist.
455\end{methoddesc}
456
457\begin{methoddesc}{add_folder}{folder}
458Create a folder whose name is \var{folder} and return an \class{MH} instance
459representing it.
460\end{methoddesc}
461
462\begin{methoddesc}{remove_folder}{folder}
463Delete the folder whose name is \var{folder}. If the folder contains any
464messages, a \exception{NotEmptyError} exception will be raised and the folder
465will not be deleted.
466\end{methoddesc}
467
468\begin{methoddesc}{get_sequences}{}
469Return a dictionary of sequence names mapped to key lists. If there are no
470sequences, the empty dictionary is returned.
471\end{methoddesc}
472
473\begin{methoddesc}{set_sequences}{sequences}
474Re-define the sequences that exist in the mailbox based upon \var{sequences}, a
475dictionary of names mapped to key lists, like returned by
476\method{get_sequences()}.
477\end{methoddesc}
478
479\begin{methoddesc}{pack}{}
480Rename messages in the mailbox as necessary to eliminate gaps in numbering.
481Entries in the sequences list are updated correspondingly. \note{Already-issued
482keys are invalidated by this operation and should not be subsequently used.}
483\end{methoddesc}
484
485Some \class{Mailbox} methods implemented by \class{MH} deserve special remarks:
486
487\begin{methoddesc}{remove}{key}
488\methodline{__delitem__}{key}
489\methodline{discard}{key}
490These methods immediately delete the message. The MH convention of marking a
491message for deletion by prepending a comma to its name is not used.
492\end{methoddesc}
493
494\begin{methoddesc}{lock}{}
495\methodline{unlock}{}
496Three locking mechanisms are used---dot locking and, if available, the
497\cfunction{flock()} and \cfunction{lockf()} system calls. For MH mailboxes,
498locking the mailbox means locking the \file{.mh_sequences} file and, only for
499the duration of any operations that affect them, locking individual message
500files.
501\end{methoddesc}
502
503\begin{methoddesc}{get_file}{key}
504Depending upon the host platform, it may not be possible to remove the
505underlying message while the returned file remains open.
506\end{methoddesc}
507
508\begin{methoddesc}{flush}{}
509All changes to MH mailboxes are immediately applied, so this method does
510nothing.
511\end{methoddesc}
512
513\begin{methoddesc}{close}{}
514\class{MH} instances do not keep any open files, so this method is equivelant
515to \method{unlock()}.
516\end{methoddesc}
517
518\begin{seealso}
519\seelink{http://www.nongnu.org/nmh/}{nmh - Message Handling System}{Home page
520of \program{nmh}, an updated version of the original \program{mh}.}
521\seelink{http://www.ics.uci.edu/\tilde{}mh/book/}{MH \& nmh: Email for Users \&
522Programmers}{A GPL-licensed book on \program{mh} and \program{nmh}, with some
523information on the mailbox format.}
524\end{seealso}
525
526\subsubsection{\class{Babyl}}
527\label{mailbox-babyl}
528
529\begin{classdesc}{Babyl}{path\optional{, factory=None\optional{, create=True}}}
530A subclass of \class{Mailbox} for mailboxes in Babyl format. Parameter
531\var{factory} is a callable object that accepts a file-like message
532representation (which behaves as if opened in binary mode) and returns a custom
533representation. If \var{factory} is \code{None}, \class{BabylMessage} is used
534as the default message representation. If \var{create} is \code{True}, the
535mailbox is created if it does not exist.
536\end{classdesc}
537
538Babyl is a single-file mailbox format used by the Rmail mail user agent
539included with Emacs. The beginning of a message is indicated by a line
540containing the two characters Control-Underscore
541(\character{\textbackslash037}) and Control-L (\character{\textbackslash014}).
542The end of a message is indicated by the start of the next message or, in the
543case of the last message, a line containing a Control-Underscore
544(\character{\textbackslash037}) character.
545
546Messages in a Babyl mailbox have two sets of headers, original headers and
547so-called visible headers. Visible headers are typically a subset of the
548original headers that have been reformatted or abridged to be more attractive.
549Each message in a Babyl mailbox also has an accompanying list of \dfn{labels},
550or short strings that record extra information about the message, and a list of
551all user-defined labels found in the mailbox is kept in the Babyl options
552section.
553
554\class{Babyl} instances have all of the methods of \class{Mailbox} in addition
555to the following:
556
557\begin{methoddesc}{get_labels}{}
558Return a list of the names of all user-defined labels used in the mailbox.
559\note{The actual messages are inspected to determine which labels exist in the
560mailbox rather than consulting the list of labels in the Babyl options section,
561but the Babyl section is updated whenever the mailbox is modified.}
562\end{methoddesc}
563
564Some \class{Mailbox} methods implemented by \class{Babyl} deserve special
565remarks:
566
567\begin{methoddesc}{get_file}{key}
568In Babyl mailboxes, the headers of a message are not stored contiguously with
569the body of the message. To generate a file-like representation, the headers
570and body are copied together into a \class{StringIO} instance (from the
571\module{StringIO} module), which has an API identical to that of a file. As a
572result, the file-like object is truly independent of the underlying mailbox but
573does not save memory compared to a string representation.
574\end{methoddesc}
575
576\begin{methoddesc}{lock}{}
577\methodline{unlock}{}
578Three locking mechanisms are used---dot locking and, if available, the
579\cfunction{flock()} and \cfunction{lockf()} system calls.
580\end{methoddesc}
581
582\begin{seealso}
583\seelink{http://quimby.gnus.org/notes/BABYL}{Format of Version 5 Babyl Files}{A
584specification of the Babyl format.}
585\seelink{http://www.gnu.org/software/emacs/manual/html_node/Rmail.html}{Reading
586Mail with Rmail}{The Rmail manual, with some information on Babyl semantics.}
587\end{seealso}
588
589\subsubsection{\class{MMDF}}
590\label{mailbox-mmdf}
591
592\begin{classdesc}{MMDF}{path\optional{, factory=None\optional{, create=True}}}
593A subclass of \class{Mailbox} for mailboxes in MMDF format. Parameter
594\var{factory} is a callable object that accepts a file-like message
595representation (which behaves as if opened in binary mode) and returns a custom
596representation. If \var{factory} is \code{None}, \class{MMDFMessage} is used as
597the default message representation. If \var{create} is \code{True}, the mailbox
598is created if it does not exist.
599\end{classdesc}
600
601MMDF is a single-file mailbox format invented for the Multichannel Memorandum
602Distribution Facility, a mail transfer agent. Each message is in the same form
603as an mbox message but is bracketed before and after by lines containing four
604Control-A (\character{\textbackslash001}) characters. As with the mbox format,
605the beginning of each message is indicated by a line whose first five
606characters are "From~", but additional occurrences of "From~" are not
607transformed to ">From~" when storing messages because the extra message
608separator lines prevent mistaking such occurrences for the starts of subsequent
609messages.
610
611Some \class{Mailbox} methods implemented by \class{MMDF} deserve special
612remarks:
613
614\begin{methoddesc}{get_file}{key}
615Using the file after calling \method{flush()} or \method{close()} on the
616\class{MMDF} instance may yield unpredictable results or raise an exception.
617\end{methoddesc}
618
619\begin{methoddesc}{lock}{}
620\methodline{unlock}{}
621Three locking mechanisms are used---dot locking and, if available, the
622\cfunction{flock()} and \cfunction{lockf()} system calls.
623\end{methoddesc}
624
625\begin{seealso}
626\seelink{http://www.tin.org/bin/man.cgi?section=5\&topic=mmdf}{mmdf man page
627from tin}{A specification of MMDF format from the documentation of tin, a
628newsreader.}
629\seelink{http://en.wikipedia.org/wiki/MMDF}{MMDF}{A Wikipedia article
630describing the Multichannel Memorandum Distribution Facility.}
631\end{seealso}
632
633\subsection{\class{Message} objects}
634\label{mailbox-message-objects}
635
636\begin{classdesc}{Message}{\optional{message}}
637A subclass of the \module{email.Message} module's \class{Message}. Subclasses
638of \class{mailbox.Message} add mailbox-format-specific state and behavior.
639
640If \var{message} is omitted, the new instance is created in a default, empty
641state. If \var{message} is an \class{email.Message.Message} instance, its
642contents are copied; furthermore, any format-specific information is converted
643insofar as possible if \var{message} is a \class{Message} instance. If
644\var{message} is a string or a file, it should contain an \rfc{2822}-compliant
645message, which is read and parsed.
646\end{classdesc}
647
648The format-specific state and behaviors offered by subclasses vary, but in
649general it is only the properties that are not specific to a particular mailbox
650that are supported (although presumably the properties are specific to a
651particular mailbox format). For example, file offsets for single-file mailbox
652formats and file names for directory-based mailbox formats are not retained,
653because they are only applicable to the original mailbox. But state such as
654whether a message has been read by the user or marked as important is retained,
655because it applies to the message itself.
656
657There is no requirement that \class{Message} instances be used to represent
658messages retrieved using \class{Mailbox} instances. In some situations, the
659time and memory required to generate \class{Message} representations might not
660not acceptable. For such situations, \class{Mailbox} instances also offer
661string and file-like representations, and a custom message factory may be
662specified when a \class{Mailbox} instance is initialized.
663
664\subsubsection{\class{MaildirMessage}}
665\label{mailbox-maildirmessage}
666
667\begin{classdesc}{MaildirMessage}{\optional{message}}
668A message with Maildir-specific behaviors. Parameter \var{message}
669has the same meaning as with the \class{Message} constructor.
670\end{classdesc}
671
672Typically, a mail user agent application moves all of the messages in the
673\file{new} subdirectory to the \file{cur} subdirectory after the first time the
674user opens and closes the mailbox, recording that the messages are old whether
675or not they've actually been read. Each message in \file{cur} has an "info"
676section added to its file name to store information about its state. (Some mail
677readers may also add an "info" section to messages in \file{new}.) The "info"
678section may take one of two forms: it may contain "2," followed by a list of
679standardized flags (e.g., "2,FR") or it may contain "1," followed by so-called
680experimental information. Standard flags for Maildir messages are as follows:
681
682\begin{tableiii}{l|l|l}{textrm}{Flag}{Meaning}{Explanation}
683\lineiii{D}{Draft}{Under composition}
684\lineiii{F}{Flagged}{Marked as important}
685\lineiii{P}{Passed}{Forwarded, resent, or bounced}
686\lineiii{R}{Replied}{Replied to}
687\lineiii{S}{Seen}{Read}
688\lineiii{T}{Trashed}{Marked for subsequent deletion}
689\end{tableiii}
690
691\class{MaildirMessage} instances offer the following methods:
692
693\begin{methoddesc}{get_subdir}{}
694Return either "new" (if the message should be stored in the \file{new}
695subdirectory) or "cur" (if the message should be stored in the \file{cur}
696subdirectory). \note{A message is typically moved from \file{new} to \file{cur}
697after its mailbox has been accessed, whether or not the message is has been
698read. A message \code{msg} has been read if \code{"S" not in msg.get_flags()}
699is \code{True}.}
700\end{methoddesc}
701
702\begin{methoddesc}{set_subdir}{subdir}
703Set the subdirectory the message should be stored in. Parameter \var{subdir}
704must be either "new" or "cur".
705\end{methoddesc}
706
707\begin{methoddesc}{get_flags}{}
708Return a string specifying the flags that are currently set. If the message
709complies with the standard Maildir format, the result is the concatenation in
710alphabetical order of zero or one occurrence of each of \character{D},
711\character{F}, \character{P}, \character{R}, \character{S}, and \character{T}.
712The empty string is returned if no flags are set or if "info" contains
713experimental semantics.
714\end{methoddesc}
715
716\begin{methoddesc}{set_flags}{flags}
717Set the flags specified by \var{flags} and unset all others.
718\end{methoddesc}
719
720\begin{methoddesc}{add_flag}{flag}
721Set the flag(s) specified by \var{flag} without changing other flags. To add
722more than one flag at a time, \var{flag} may be a string of more than one
723character. The current "info" is overwritten whether or not it contains
724experimental information rather than
725flags.
726\end{methoddesc}
727
728\begin{methoddesc}{remove_flag}{flag}
729Unset the flag(s) specified by \var{flag} without changing other flags. To
730remove more than one flag at a time, \var{flag} maybe a string of more than one
731character. If "info" contains experimental information rather than flags, the
732current "info" is not modified.
733\end{methoddesc}
734
735\begin{methoddesc}{get_date}{}
736Return the delivery date of the message as a floating-point number representing
737seconds since the epoch.
738\end{methoddesc}
739
740\begin{methoddesc}{set_date}{date}
741Set the delivery date of the message to \var{date}, a floating-point number
742representing seconds since the epoch.
743\end{methoddesc}
744
745\begin{methoddesc}{get_info}{}
746Return a string containing the "info" for a message. This is useful for
747accessing and modifying "info" that is experimental (i.e., not a list of
748flags).
749\end{methoddesc}
750
751\begin{methoddesc}{set_info}{info}
752Set "info" to \var{info}, which should be a string.
753\end{methoddesc}
754
755When a \class{MaildirMessage} instance is created based upon an
756\class{mboxMessage} or \class{MMDFMessage} instance, the \mailheader{Status}
757and \mailheader{X-Status} headers are omitted and the following conversions
758take place:
759
760\begin{tableii}{l|l}{textrm}
761 {Resulting state}{\class{mboxMessage} or \class{MMDFMessage} state}
762\lineii{"cur" subdirectory}{O flag}
763\lineii{F flag}{F flag}
764\lineii{R flag}{A flag}
765\lineii{S flag}{R flag}
766\lineii{T flag}{D flag}
767\end{tableii}
768
769When a \class{MaildirMessage} instance is created based upon an
770\class{MHMessage} instance, the following conversions take place:
771
772\begin{tableii}{l|l}{textrm}
773 {Resulting state}{\class{MHMessage} state}
774\lineii{"cur" subdirectory}{"unseen" sequence}
775\lineii{"cur" subdirectory and S flag}{no "unseen" sequence}
776\lineii{F flag}{"flagged" sequence}
777\lineii{R flag}{"replied" sequence}
778\end{tableii}
779
780When a \class{MaildirMessage} instance is created based upon a
781\class{BabylMessage} instance, the following conversions take place:
782
783\begin{tableii}{l|l}{textrm}
784 {Resulting state}{\class{BabylMessage} state}
785\lineii{"cur" subdirectory}{"unseen" label}
786\lineii{"cur" subdirectory and S flag}{no "unseen" label}
787\lineii{P flag}{"forwarded" or "resent" label}
788\lineii{R flag}{"answered" label}
789\lineii{T flag}{"deleted" label}
790\end{tableii}
791
792\subsubsection{\class{mboxMessage}}
793\label{mailbox-mboxmessage}
794
795\begin{classdesc}{mboxMessage}{\optional{message}}
796A message with mbox-specific behaviors. Parameter \var{message} has the same
797meaning as with the \class{Message} constructor.
798\end{classdesc}
799
800Messages in an mbox mailbox are stored together in a single file. The sender's
801envelope address and the time of delivery are typically stored in a line
802beginning with "From~" that is used to indicate the start of a message, though
803there is considerable variation in the exact format of this data among mbox
804implementations. Flags that indicate the state of the message, such as whether
805it has been read or marked as important, are typically stored in
806\mailheader{Status} and \mailheader{X-Status} headers.
807
808Conventional flags for mbox messages are as follows:
809
810\begin{tableiii}{l|l|l}{textrm}{Flag}{Meaning}{Explanation}
811\lineiii{R}{Read}{Read}
812\lineiii{O}{Old}{Previously detected by MUA}
813\lineiii{D}{Deleted}{Marked for subsequent deletion}
814\lineiii{F}{Flagged}{Marked as important}
815\lineiii{A}{Answered}{Replied to}
816\end{tableiii}
817
818The "R" and "O" flags are stored in the \mailheader{Status} header, and the
819"D", "F", and "A" flags are stored in the \mailheader{X-Status} header. The
820flags and headers typically appear in the order mentioned.
821
822\class{mboxMessage} instances offer the following methods:
823
824\begin{methoddesc}{get_from}{}
825Return a string representing the "From~" line that marks the start of the
826message in an mbox mailbox. The leading "From~" and the trailing newline are
827excluded.
828\end{methoddesc}
829
830\begin{methoddesc}{set_from}{from_\optional{, time_=None}}
831Set the "From~" line to \var{from_}, which should be specified without a
832leading "From~" or trailing newline. For convenience, \var{time_} may be
833specified and will be formatted appropriately and appended to \var{from_}. If
834\var{time_} is specified, it should be a \class{struct_time} instance, a tuple
835suitable for passing to \method{time.strftime()}, or \code{True} (to use
836\method{time.gmtime()}).
837\end{methoddesc}
838
839\begin{methoddesc}{get_flags}{}
840Return a string specifying the flags that are currently set. If the message
841complies with the conventional format, the result is the concatenation in the
842following order of zero or one occurrence of each of \character{R},
843\character{O}, \character{D}, \character{F}, and \character{A}.
844\end{methoddesc}
845
846\begin{methoddesc}{set_flags}{flags}
847Set the flags specified by \var{flags} and unset all others. Parameter
848\var{flags} should be the concatenation in any order of zero or more
849occurrences of each of \character{R}, \character{O}, \character{D},
850\character{F}, and \character{A}.
851\end{methoddesc}
852
853\begin{methoddesc}{add_flag}{flag}
854Set the flag(s) specified by \var{flag} without changing other flags. To add
855more than one flag at a time, \var{flag} may be a string of more than one
856character.
857\end{methoddesc}
858
859\begin{methoddesc}{remove_flag}{flag}
860Unset the flag(s) specified by \var{flag} without changing other flags. To
861remove more than one flag at a time, \var{flag} maybe a string of more than one
862character.
863\end{methoddesc}
864
865When an \class{mboxMessage} instance is created based upon a
866\class{MaildirMessage} instance, a "From~" line is generated based upon the
867\class{MaildirMessage} instance's delivery date, and the following conversions
868take place:
869
870\begin{tableii}{l|l}{textrm}
871 {Resulting state}{\class{MaildirMessage} state}
872\lineii{R flag}{S flag}
873\lineii{O flag}{"cur" subdirectory}
874\lineii{D flag}{T flag}
875\lineii{F flag}{F flag}
876\lineii{A flag}{R flag}
877\end{tableii}
878
879When an \class{mboxMessage} instance is created based upon an \class{MHMessage}
880instance, the following conversions take place:
881
882\begin{tableii}{l|l}{textrm}
883 {Resulting state}{\class{MHMessage} state}
884\lineii{R flag and O flag}{no "unseen" sequence}
885\lineii{O flag}{"unseen" sequence}
886\lineii{F flag}{"flagged" sequence}
887\lineii{A flag}{"replied" sequence}
888\end{tableii}
889
890When an \class{mboxMessage} instance is created based upon a
891\class{BabylMessage} instance, the following conversions take place:
892
893\begin{tableii}{l|l}{textrm}
894 {Resulting state}{\class{BabylMessage} state}
895\lineii{R flag and O flag}{no "unseen" label}
896\lineii{O flag}{"unseen" label}
897\lineii{D flag}{"deleted" label}
898\lineii{A flag}{"answered" label}
899\end{tableii}
900
901When a \class{Message} instance is created based upon an \class{MMDFMessage}
902instance, the "From~" line is copied and all flags directly correspond:
903
904\begin{tableii}{l|l}{textrm}
905 {Resulting state}{\class{MMDFMessage} state}
906\lineii{R flag}{R flag}
907\lineii{O flag}{O flag}
908\lineii{D flag}{D flag}
909\lineii{F flag}{F flag}
910\lineii{A flag}{A flag}
911\end{tableii}
912
913\subsubsection{\class{MHMessage}}
914\label{mailbox-mhmessage}
915
916\begin{classdesc}{MHMessage}{\optional{message}}
917A message with MH-specific behaviors. Parameter \var{message} has the same
918meaning as with the \class{Message} constructor.
919\end{classdesc}
920
921MH messages do not support marks or flags in the traditional sense, but they do
922support sequences, which are logical groupings of arbitrary messages. Some mail
923reading programs (although not the standard \program{mh} and \program{nmh}) use
924sequences in much the same way flags are used with other formats, as follows:
925
926\begin{tableii}{l|l}{textrm}{Sequence}{Explanation}
927\lineii{unseen}{Not read, but previously detected by MUA}
928\lineii{replied}{Replied to}
929\lineii{flagged}{Marked as important}
930\end{tableii}
931
932\class{MHMessage} instances offer the following methods:
933
934\begin{methoddesc}{get_sequences}{}
935Return a list of the names of sequences that include this message.
936\end{methoddesc}
937
938\begin{methoddesc}{set_sequences}{sequences}
939Set the list of sequences that include this message.
940\end{methoddesc}
941
942\begin{methoddesc}{add_sequence}{sequence}
943Add \var{sequence} to the list of sequences that include this message.
944\end{methoddesc}
945
946\begin{methoddesc}{remove_sequence}{sequence}
947Remove \var{sequence} from the list of sequences that include this message.
948\end{methoddesc}
949
950When an \class{MHMessage} instance is created based upon a
951\class{MaildirMessage} instance, the following conversions take place:
952
953\begin{tableii}{l|l}{textrm}
954 {Resulting state}{\class{MaildirMessage} state}
955\lineii{"unseen" sequence}{no S flag}
956\lineii{"replied" sequence}{R flag}
957\lineii{"flagged" sequence}{F flag}
958\end{tableii}
959
960When an \class{MHMessage} instance is created based upon an \class{mboxMessage}
961or \class{MMDFMessage} instance, the \mailheader{Status} and
962\mailheader{X-Status} headers are omitted and the following conversions take
963place:
964
965\begin{tableii}{l|l}{textrm}
966 {Resulting state}{\class{mboxMessage} or \class{MMDFMessage} state}
967\lineii{"unseen" sequence}{no R flag}
968\lineii{"replied" sequence}{A flag}
969\lineii{"flagged" sequence}{F flag}
970\end{tableii}
971
972When an \class{MHMessage} instance is created based upon a \class{BabylMessage}
973instance, the following conversions take place:
974
975\begin{tableii}{l|l}{textrm}
976 {Resulting state}{\class{BabylMessage} state}
977\lineii{"unseen" sequence}{"unseen" label}
978\lineii{"replied" sequence}{"answered" label}
979\end{tableii}
980
981\subsubsection{\class{BabylMessage}}
982\label{mailbox-babylmessage}
983
984\begin{classdesc}{BabylMessage}{\optional{message}}
985A message with Babyl-specific behaviors. Parameter \var{message} has the same
986meaning as with the \class{Message} constructor.
987\end{classdesc}
988
989Certain message labels, called \dfn{attributes}, are defined by convention to
990have special meanings. The attributes are as follows:
991
992\begin{tableii}{l|l}{textrm}{Label}{Explanation}
993\lineii{unseen}{Not read, but previously detected by MUA}
994\lineii{deleted}{Marked for subsequent deletion}
995\lineii{filed}{Copied to another file or mailbox}
996\lineii{answered}{Replied to}
997\lineii{forwarded}{Forwarded}
998\lineii{edited}{Modified by the user}
999\lineii{resent}{Resent}
1000\end{tableii}
1001
1002By default, Rmail displays only
1003visible headers. The \class{BabylMessage} class, though, uses the original
1004headers because they are more complete. Visible headers may be accessed
1005explicitly if desired.
1006
1007\class{BabylMessage} instances offer the following methods:
1008
1009\begin{methoddesc}{get_labels}{}
1010Return a list of labels on the message.
1011\end{methoddesc}
1012
1013\begin{methoddesc}{set_labels}{labels}
1014Set the list of labels on the message to \var{labels}.
1015\end{methoddesc}
1016
1017\begin{methoddesc}{add_label}{label}
1018Add \var{label} to the list of labels on the message.
1019\end{methoddesc}
1020
1021\begin{methoddesc}{remove_label}{label}
1022Remove \var{label} from the list of labels on the message.
1023\end{methoddesc}
1024
1025\begin{methoddesc}{get_visible}{}
1026Return an \class{Message} instance whose headers are the message's visible
1027headers and whose body is empty.
1028\end{methoddesc}
1029
1030\begin{methoddesc}{set_visible}{visible}
1031Set the message's visible headers to be the same as the headers in
1032\var{message}. Parameter \var{visible} should be a \class{Message} instance, an
1033\class{email.Message.Message} instance, a string, or a file-like object (which
1034should be open in text mode).
1035\end{methoddesc}
1036
1037\begin{methoddesc}{update_visible}{}
1038When a \class{BabylMessage} instance's original headers are modified, the
1039visible headers are not automatically modified to correspond. This method
1040updates the visible headers as follows: each visible header with a
1041corresponding original header is set to the value of the original header, each
1042visible header without a corresponding original header is removed, and any of
1043\mailheader{Date}, \mailheader{From}, \mailheader{Reply-To}, \mailheader{To},
1044\mailheader{CC}, and \mailheader{Subject} that are present in the original
1045headers but not the visible headers are added to the visible headers.
1046\end{methoddesc}
1047
1048When a \class{BabylMessage} instance is created based upon a
1049\class{MaildirMessage} instance, the following conversions take place:
1050
1051\begin{tableii}{l|l}{textrm}
1052 {Resulting state}{\class{MaildirMessage} state}
1053\lineii{"unseen" label}{no S flag}
1054\lineii{"deleted" label}{T flag}
1055\lineii{"answered" label}{R flag}
1056\lineii{"forwarded" label}{P flag}
1057\end{tableii}
1058
1059When a \class{BabylMessage} instance is created based upon an
1060\class{mboxMessage} or \class{MMDFMessage} instance, the \mailheader{Status}
1061and \mailheader{X-Status} headers are omitted and the following conversions
1062take place:
1063
1064\begin{tableii}{l|l}{textrm}
1065 {Resulting state}{\class{mboxMessage} or \class{MMDFMessage} state}
1066\lineii{"unseen" label}{no R flag}
1067\lineii{"deleted" label}{D flag}
1068\lineii{"answered" label}{A flag}
1069\end{tableii}
1070
1071When a \class{BabylMessage} instance is created based upon an \class{MHMessage}
1072instance, the following conversions take place:
1073
1074\begin{tableii}{l|l}{textrm}
1075 {Resulting state}{\class{MHMessage} state}
1076\lineii{"unseen" label}{"unseen" sequence}
1077\lineii{"answered" label}{"replied" sequence}
1078\end{tableii}
1079
1080\subsubsection{\class{MMDFMessage}}
1081\label{mailbox-mmdfmessage}
1082
1083\begin{classdesc}{MMDFMessage}{\optional{message}}
1084A message with MMDF-specific behaviors. Parameter \var{message} has the same
1085meaning as with the \class{Message} constructor.
1086\end{classdesc}
1087
1088As with message in an mbox mailbox, MMDF messages are stored with the sender's
1089address and the delivery date in an initial line beginning with "From ".
1090Likewise, flags that indicate the state of the message are typically stored in
1091\mailheader{Status} and \mailheader{X-Status} headers.
1092
1093Conventional flags for MMDF messages are identical to those of mbox message and
1094are as follows:
1095
1096\begin{tableiii}{l|l|l}{textrm}{Flag}{Meaning}{Explanation}
1097\lineiii{R}{Read}{Read}
1098\lineiii{O}{Old}{Previously detected by MUA}
1099\lineiii{D}{Deleted}{Marked for subsequent deletion}
1100\lineiii{F}{Flagged}{Marked as important}
1101\lineiii{A}{Answered}{Replied to}
1102\end{tableiii}
1103
1104The "R" and "O" flags are stored in the \mailheader{Status} header, and the
1105"D", "F", and "A" flags are stored in the \mailheader{X-Status} header. The
1106flags and headers typically appear in the order mentioned.
1107
1108\class{MMDFMessage} instances offer the following methods, which are identical
1109to those offered by \class{mboxMessage}:
1110
1111\begin{methoddesc}{get_from}{}
1112Return a string representing the "From~" line that marks the start of the
1113message in an mbox mailbox. The leading "From~" and the trailing newline are
1114excluded.
1115\end{methoddesc}
1116
1117\begin{methoddesc}{set_from}{from_\optional{, time_=None}}
1118Set the "From~" line to \var{from_}, which should be specified without a
1119leading "From~" or trailing newline. For convenience, \var{time_} may be
1120specified and will be formatted appropriately and appended to \var{from_}. If
1121\var{time_} is specified, it should be a \class{struct_time} instance, a tuple
1122suitable for passing to \method{time.strftime()}, or \code{True} (to use
1123\method{time.gmtime()}).
1124\end{methoddesc}
1125
1126\begin{methoddesc}{get_flags}{}
1127Return a string specifying the flags that are currently set. If the message
1128complies with the conventional format, the result is the concatenation in the
1129following order of zero or one occurrence of each of \character{R},
1130\character{O}, \character{D}, \character{F}, and \character{A}.
1131\end{methoddesc}
1132
1133\begin{methoddesc}{set_flags}{flags}
1134Set the flags specified by \var{flags} and unset all others. Parameter
1135\var{flags} should be the concatenation in any order of zero or more
1136occurrences of each of \character{R}, \character{O}, \character{D},
1137\character{F}, and \character{A}.
1138\end{methoddesc}
1139
1140\begin{methoddesc}{add_flag}{flag}
1141Set the flag(s) specified by \var{flag} without changing other flags. To add
1142more than one flag at a time, \var{flag} may be a string of more than one
1143character.
1144\end{methoddesc}
1145
1146\begin{methoddesc}{remove_flag}{flag}
1147Unset the flag(s) specified by \var{flag} without changing other flags. To
1148remove more than one flag at a time, \var{flag} maybe a string of more than one
1149character.
1150\end{methoddesc}
1151
1152When an \class{MMDFMessage} instance is created based upon a
1153\class{MaildirMessage} instance, a "From~" line is generated based upon the
1154\class{MaildirMessage} instance's delivery date, and the following conversions
1155take place:
1156
1157\begin{tableii}{l|l}{textrm}
1158 {Resulting state}{\class{MaildirMessage} state}
1159\lineii{R flag}{S flag}
1160\lineii{O flag}{"cur" subdirectory}
1161\lineii{D flag}{T flag}
1162\lineii{F flag}{F flag}
1163\lineii{A flag}{R flag}
1164\end{tableii}
1165
1166When an \class{MMDFMessage} instance is created based upon an \class{MHMessage}
1167instance, the following conversions take place:
1168
1169\begin{tableii}{l|l}{textrm}
1170 {Resulting state}{\class{MHMessage} state}
1171\lineii{R flag and O flag}{no "unseen" sequence}
1172\lineii{O flag}{"unseen" sequence}
1173\lineii{F flag}{"flagged" sequence}
1174\lineii{A flag}{"replied" sequence}
1175\end{tableii}
1176
1177When an \class{MMDFMessage} instance is created based upon a
1178\class{BabylMessage} instance, the following conversions take place:
1179
1180\begin{tableii}{l|l}{textrm}
1181 {Resulting state}{\class{BabylMessage} state}
1182\lineii{R flag and O flag}{no "unseen" label}
1183\lineii{O flag}{"unseen" label}
1184\lineii{D flag}{"deleted" label}
1185\lineii{A flag}{"answered" label}
1186\end{tableii}
1187
1188When an \class{MMDFMessage} instance is created based upon an
1189\class{mboxMessage} instance, the "From~" line is copied and all flags directly
1190correspond:
1191
1192\begin{tableii}{l|l}{textrm}
1193 {Resulting state}{\class{mboxMessage} state}
1194\lineii{R flag}{R flag}
1195\lineii{O flag}{O flag}
1196\lineii{D flag}{D flag}
1197\lineii{F flag}{F flag}
1198\lineii{A flag}{A flag}
1199\end{tableii}
1200
1201\subsection{Exceptions}
1202\label{mailbox-deprecated}
1203
1204The following exception classes are defined in the \module{mailbox} module:
1205
1206\begin{classdesc}{Error}{}
1207The based class for all other module-specific exceptions.
1208\end{classdesc}
1209
1210\begin{classdesc}{NoSuchMailboxError}{}
1211Raised when a mailbox is expected but is not found, such as when instantiating
1212a \class{Mailbox} subclass with a path that does not exist (and with the
1213\var{create} parameter set to \code{False}), or when opening a folder that does
1214not exist.
1215\end{classdesc}
1216
1217\begin{classdesc}{NotEmptyErrorError}{}
1218Raised when a mailbox is not empty but is expected to be, such as when deleting
1219a folder that contains messages.
1220\end{classdesc}
1221
1222\begin{classdesc}{ExternalClashError}{}
1223Raised when some mailbox-related condition beyond the control of the program
1224causes it to be unable to proceed, such as when failing to acquire a lock that
1225another program already holds a lock, or when a uniquely-generated file name
1226already exists.
1227\end{classdesc}
1228
1229\begin{classdesc}{FormatError}{}
1230Raised when the data in a file cannot be parsed, such as when an \class{MH}
1231instance attempts to read a corrupted \file{.mh_sequences} file.
1232\end{classdesc}
1233
1234\subsection{Deprecated classes and methods}
1235\label{mailbox-deprecated}
1236
1237Older versions of the \module{mailbox} module do not support modification of
1238mailboxes, such as adding or removing message, and do not provide classes to
1239represent format-specific message properties. For backward compatibility, the
1240older mailbox classes are still available, but the newer classes should be used
1241in preference to them.
1242
1243Older mailbox objects support only iteration and provide a single public
1244method:
1245
1246\begin{methoddesc}{next}{}
1247Return the next message in the mailbox, created with the optional \var{factory}
1248argument passed into the mailbox object's constructor. By default this is an
1249\class{rfc822.Message} object (see the \refmodule{rfc822} module). Depending
1250on the mailbox implementation the \var{fp} attribute of this object may be a
1251true file object or a class instance simulating a file object, taking care of
1252things like message boundaries if multiple mail messages are contained in a
1253single file, etc. If no more messages are available, this method returns
1254\code{None}.
1255\end{methoddesc}
1256
1257Most of the older mailbox classes have names that differ from the current
1258mailbox class names, except for \class{Maildir}. For this reason, the new
1259\class{Maildir} class defines a \method{next()} method and its constructor
1260differs slightly from those of the other new mailbox classes.
1261
1262The older mailbox classes whose names are not the same as their newer
1263counterparts are as follows:
Guido van Rossum39a23cc1997-06-02 21:04:41 +00001264
Barry Warsaw30dbd142001-01-31 22:14:01 +00001265\begin{classdesc}{UnixMailbox}{fp\optional{, factory}}
Fred Drake62700312001-02-02 03:51:05 +00001266Access to a classic \UNIX-style mailbox, where all messages are
1267contained in a single file and separated by \samp{From }
1268(a.k.a.\ \samp{From_}) lines. The file object \var{fp} points to the
1269mailbox file. The optional \var{factory} parameter is a callable that
1270should create new message objects. \var{factory} is called with one
1271argument, \var{fp} by the \method{next()} method of the mailbox
1272object. The default is the \class{rfc822.Message} class (see the
Barry Warsaw47db2522003-06-20 22:04:03 +00001273\refmodule{rfc822} module -- and the note below).
Barry Warsaw30dbd142001-01-31 22:14:01 +00001274
Fred Drake0d736212004-05-11 05:29:34 +00001275\begin{notice}
1276 For reasons of this module's internal implementation, you will
1277 probably want to open the \var{fp} object in binary mode. This is
1278 especially important on Windows.
1279\end{notice}
Barry Warsawdd69b0a2004-05-10 23:12:52 +00001280
Fred Drake62700312001-02-02 03:51:05 +00001281For maximum portability, messages in a \UNIX-style mailbox are
1282separated by any line that begins exactly with the string \code{'From
1283'} (note the trailing space) if preceded by exactly two newlines.
1284Because of the wide-range of variations in practice, nothing else on
1285the From_ line should be considered. However, the current
1286implementation doesn't check for the leading two newlines. This is
1287usually fine for most applications.
Barry Warsaw30dbd142001-01-31 22:14:01 +00001288
1289The \class{UnixMailbox} class implements a more strict version of
1290From_ line checking, using a regular expression that usually correctly
1291matched From_ delimiters. It considers delimiter line to be separated
Fred Drake62700312001-02-02 03:51:05 +00001292by \samp{From \var{name} \var{time}} lines. For maximum portability,
1293use the \class{PortableUnixMailbox} class instead. This class is
1294identical to \class{UnixMailbox} except that individual messages are
1295separated by only \samp{From } lines.
Barry Warsaw30dbd142001-01-31 22:14:01 +00001296
Fred Drake62700312001-02-02 03:51:05 +00001297For more information, see
1298\citetitle[http://home.netscape.com/eng/mozilla/2.0/relnotes/demo/content-length.html]{Configuring
1299Netscape Mail on \UNIX: Why the Content-Length Format is Bad}.
1300\end{classdesc}
1301
1302\begin{classdesc}{PortableUnixMailbox}{fp\optional{, factory}}
1303A less-strict version of \class{UnixMailbox}, which considers only the
1304\samp{From } at the beginning of the line separating messages. The
1305``\var{name} \var{time}'' portion of the From line is ignored, to
1306protect against some variations that are observed in practice. This
1307works since lines in the message which begin with \code{'From '} are
Greg Ward02669a32002-09-23 19:32:42 +00001308quoted by mail handling software at delivery-time.
Fred Drake2e495c91998-03-14 06:48:33 +00001309\end{classdesc}
Guido van Rossum39a23cc1997-06-02 21:04:41 +00001310
Barry Warsaw30dbd142001-01-31 22:14:01 +00001311\begin{classdesc}{MmdfMailbox}{fp\optional{, factory}}
Guido van Rossum39a23cc1997-06-02 21:04:41 +00001312Access an MMDF-style mailbox, where all messages are contained
1313in a single file and separated by lines consisting of 4 control-A
Fred Drake6e99adb1998-02-13 22:17:21 +00001314characters. The file object \var{fp} points to the mailbox file.
Barry Warsaw30dbd142001-01-31 22:14:01 +00001315Optional \var{factory} is as with the \class{UnixMailbox} class.
Fred Drake2e495c91998-03-14 06:48:33 +00001316\end{classdesc}
Guido van Rossum39a23cc1997-06-02 21:04:41 +00001317
Barry Warsaw30dbd142001-01-31 22:14:01 +00001318\begin{classdesc}{MHMailbox}{dirname\optional{, factory}}
Guido van Rossum39a23cc1997-06-02 21:04:41 +00001319Access an MH mailbox, a directory with each message in a separate
Fred Drake6e99adb1998-02-13 22:17:21 +00001320file with a numeric name.
1321The name of the mailbox directory is passed in \var{dirname}.
Barry Warsaw30dbd142001-01-31 22:14:01 +00001322\var{factory} is as with the \class{UnixMailbox} class.
Fred Drake2e495c91998-03-14 06:48:33 +00001323\end{classdesc}
Guido van Rossum39a23cc1997-06-02 21:04:41 +00001324
Barry Warsaw30dbd142001-01-31 22:14:01 +00001325\begin{classdesc}{BabylMailbox}{fp\optional{, factory}}
Barry Warsawc3cbbaf2001-04-11 20:12:33 +00001326Access a Babyl mailbox, which is similar to an MMDF mailbox. In
1327Babyl format, each message has two sets of headers, the
1328\emph{original} headers and the \emph{visible} headers. The original
Raymond Hettinger999b57c2003-08-25 04:28:05 +00001329headers appear before a line containing only \code{'*** EOOH ***'}
Barry Warsawc3cbbaf2001-04-11 20:12:33 +00001330(End-Of-Original-Headers) and the visible headers appear after the
1331\code{EOOH} line. Babyl-compliant mail readers will show you only the
1332visible headers, and \class{BabylMailbox} objects will return messages
1333containing only the visible headers. You'll have to do your own
1334parsing of the mailbox file to get at the original headers. Mail
1335messages start with the EOOH line and end with a line containing only
1336\code{'\e{}037\e{}014'}. \var{factory} is as with the
1337\class{UnixMailbox} class.
Fred Drake199b79c1999-02-20 05:04:59 +00001338\end{classdesc}
1339
Thomas Wouters477c8d52006-05-27 19:21:47 +00001340If you wish to use the older mailbox classes with the \module{email} module
1341rather than the deprecated \module{rfc822} module, you can do so as follows:
Barry Warsaw47db2522003-06-20 22:04:03 +00001342
1343\begin{verbatim}
1344import email
1345import email.Errors
1346import mailbox
1347
1348def msgfactory(fp):
1349 try:
1350 return email.message_from_file(fp)
1351 except email.Errors.MessageParseError:
1352 # Don't return None since that will
Thomas Wouters477c8d52006-05-27 19:21:47 +00001353 # stop the mailbox iterator
1354 return ''
Barry Warsaw47db2522003-06-20 22:04:03 +00001355
1356mbox = mailbox.UnixMailbox(fp, msgfactory)
1357\end{verbatim}
1358
Thomas Wouters477c8d52006-05-27 19:21:47 +00001359Alternatively, if you know your mailbox contains only well-formed MIME
1360messages, you can simplify this to:
Barry Warsaw47db2522003-06-20 22:04:03 +00001361
1362\begin{verbatim}
1363import email
1364import mailbox
1365
1366mbox = mailbox.UnixMailbox(fp, email.message_from_file)
1367\end{verbatim}
Fred Drake199b79c1999-02-20 05:04:59 +00001368
Thomas Wouters477c8d52006-05-27 19:21:47 +00001369\subsection{Examples}
1370\label{mailbox-examples}
Fred Drake1400baa2001-05-21 21:23:01 +00001371
Thomas Wouters477c8d52006-05-27 19:21:47 +00001372A simple example of printing the subjects of all messages in a mailbox that
1373seem interesting:
Fred Drake1400baa2001-05-21 21:23:01 +00001374
Thomas Wouters477c8d52006-05-27 19:21:47 +00001375\begin{verbatim}
1376import mailbox
1377for message in mailbox.mbox('~/mbox'):
1378 subject = message['subject'] # Could possibly be None.
1379 if subject and 'python' in subject.lower():
1380 print subject
1381\end{verbatim}
Guido van Rossum39a23cc1997-06-02 21:04:41 +00001382
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001383To copy all mail from a Babyl mailbox to an MH mailbox, converting all
1384of the format-specific information that can be converted:
Guido van Rossum39a23cc1997-06-02 21:04:41 +00001385
Thomas Wouters477c8d52006-05-27 19:21:47 +00001386\begin{verbatim}
1387import mailbox
1388destination = mailbox.MH('~/Mail')
Thomas Woutersb2137042007-02-01 18:02:27 +00001389destination.lock()
Thomas Wouters477c8d52006-05-27 19:21:47 +00001390for message in mailbox.Babyl('~/RMAIL'):
1391 destination.add(MHMessage(message))
Thomas Woutersb2137042007-02-01 18:02:27 +00001392destination.flush()
1393destination.unlock()
Thomas Wouters477c8d52006-05-27 19:21:47 +00001394\end{verbatim}
1395
Thomas Woutersb2137042007-02-01 18:02:27 +00001396This example sorts mail from several mailing lists into different
1397mailboxes, being careful to avoid mail corruption due to concurrent
1398modification by other programs, mail loss due to interruption of the
1399program, or premature termination due to malformed messages in the
1400mailbox:
Thomas Wouters477c8d52006-05-27 19:21:47 +00001401
1402\begin{verbatim}
1403import mailbox
1404import email.Errors
Thomas Woutersb2137042007-02-01 18:02:27 +00001405
Thomas Wouters477c8d52006-05-27 19:21:47 +00001406list_names = ('python-list', 'python-dev', 'python-bugs')
Thomas Woutersb2137042007-02-01 18:02:27 +00001407
Thomas Wouters477c8d52006-05-27 19:21:47 +00001408boxes = dict((name, mailbox.mbox('~/email/%s' % name)) for name in list_names)
Thomas Woutersb2137042007-02-01 18:02:27 +00001409inbox = mailbox.Maildir('~/Maildir', factory=None)
1410
Thomas Wouters477c8d52006-05-27 19:21:47 +00001411for key in inbox.iterkeys():
1412 try:
1413 message = inbox[key]
1414 except email.Errors.MessageParseError:
1415 continue # The message is malformed. Just leave it.
Thomas Woutersb2137042007-02-01 18:02:27 +00001416
Thomas Wouters477c8d52006-05-27 19:21:47 +00001417 for name in list_names:
1418 list_id = message['list-id']
1419 if list_id and name in list_id:
Thomas Woutersb2137042007-02-01 18:02:27 +00001420 # Get mailbox to use
Thomas Wouters477c8d52006-05-27 19:21:47 +00001421 box = boxes[name]
Thomas Woutersb2137042007-02-01 18:02:27 +00001422
1423 # Write copy to disk before removing original.
1424 # If there's a crash, you might duplicate a message, but
1425 # that's better than losing a message completely.
Thomas Wouters477c8d52006-05-27 19:21:47 +00001426 box.lock()
1427 box.add(message)
Thomas Woutersb2137042007-02-01 18:02:27 +00001428 box.flush()
Thomas Wouters477c8d52006-05-27 19:21:47 +00001429 box.unlock()
Thomas Woutersb2137042007-02-01 18:02:27 +00001430
1431 # Remove original message
1432 inbox.lock()
Thomas Wouters477c8d52006-05-27 19:21:47 +00001433 inbox.discard(key)
Thomas Woutersb2137042007-02-01 18:02:27 +00001434 inbox.flush()
1435 inbox.unlock()
Thomas Wouters477c8d52006-05-27 19:21:47 +00001436 break # Found destination, so stop looking.
Thomas Woutersb2137042007-02-01 18:02:27 +00001437
Thomas Wouters477c8d52006-05-27 19:21:47 +00001438for box in boxes.itervalues():
1439 box.close()
1440\end{verbatim}