Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 47db252

Browse files
committed
Add some documentation which describes how to use the email package
instead of rfc822 as the Message factory.
1 parent dc31dc0 commit 47db252

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

Doc/lib/libmailbox.tex

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ \section{\module{mailbox} ---
1616
should create new message objects. \var{factory} is called with one
1717
argument, \var{fp} by the \method{next()} method of the mailbox
1818
object. The default is the \class{rfc822.Message} class (see the
19-
\refmodule{rfc822} module).
19+
\refmodule{rfc822} module -- and the note below).
2020

2121
For maximum portability, messages in a \UNIX-style mailbox are
2222
separated by any line that begins exactly with the string \code{'From
@@ -83,6 +83,40 @@ \section{\module{mailbox} ---
8383
\class{UnixMailbox} class.
8484
\end{classdesc}
8585

86+
Note that because the \refmodule{rfc822} module is deprecated, it is
87+
recommended that you use the \refmodule{email} package to create
88+
message objects from a mailbox. (The default can't be changed for
89+
backwards compatibility reasons.) The safest way to do this is with
90+
bit of code:
91+
92+
\begin{verbatim}
93+
import email
94+
import email.Errors
95+
import mailbox
96+
97+
def msgfactory(fp):
98+
try:
99+
return email.message_from_file(fp)
100+
except email.Errors.MessageParseError:
101+
# Don't return None since that will
102+
# stop the mailbox iterator
103+
return ''
104+
105+
mbox = mailbox.UnixMailbox(fp, msgfactory)
106+
\end{verbatim}
107+
108+
The above wrapper is defensive against ill-formed MIME messages in the
109+
mailbox, but you have to be prepared to receive the empty string from
110+
the mailbox's \function{next()} method. On the other hand, if you
111+
know your mailbox contains only well-formed MIME messages, you can
112+
simplify this to:
113+
114+
\begin{verbatim}
115+
import email
116+
import mailbox
117+
118+
mbox = mailbox.UnixMailbox(fp, email.message_from_file)
119+
\end{verbatim}
86120

87121
\begin{seealso}
88122
\seetitle[http://www.qmail.org/man/man5/mbox.html]{mbox -

0 commit comments

Comments
 (0)