[Tutor] rfc822.Message [reading emails from mailboxes]

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sun Mar 16 03:11:01 2003


On Sat, 15 Mar 2003, Kristian Rink wrote:

> ...for what I see now, I've ran into some trouble using the rfc822
> module while trying to dissect e-mail messages and to store the
> attachments they're carrying to be able to import them into a
> document management system. Basically, here's what I tried:
>
>
> >>> import rfc822
> >>> mailmessage=rfc822.Message("2")
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "/usr/lib/python2.2/rfc822.py", line 106, in __init__
>     self.readheaders()
>   File "/usr/lib/python2.2/rfc822.py", line 153, in readheaders
>     line = self.fp.readline()
> AttributeError: 'str' object has no attribute 'readline'
>
> Btw Message "2" is a mail file inside a MH mail spool directory.

Hi Kristian,

Wait.  There is definitely something missing in the program...


The main problem is that Python has no way of guessing where your MH mail
spool directory is located!  Some part of your code will need to point
that location to Python by directory name.

Instead of directly using rfc822, you probably want to use the 'mailbox'
module:

    http://www.python.org/doc/lib/module-mailbox.html

The mailbox.MHMailbox class knows how to pull individual rfc822 messages
out of MH spools --- it should have a next() method that will let you pull
out individual Message objects.


If you'd like, I can post up a somewhat extended example of 'mailbox' use
tomorrow.  I've been working on a Jython program to run the Jakarta Lucene
text indicer on the Tutor mailing list.  So your question is nicely
timed... *grin*

The code is still very very rough, so I need another afternoon to polish
it up and comment it nicely, but if it looks ok, I'd be happy to share it
on Tutor for feedback.