Working with email and mailbox module

Steve Holden steve at holdenweb.com
Thu Sep 21 12:46:05 EDT 2006


Nirnimesh wrote:
> I want to extract emails from an mbox-type file which contains a number
> of individual emails.
> 
> I tried the python mailbox and email modules individually, but I'm
> unable to combine them to get what I want. Mailbox allows me to iterate
> over all the mails but doesn't give me access the individual messages
> of a multipart mail. The email.Message module provides this, but I'm
> unable to iterate through all the messages with this module.
> 
> Here's what I want:
> 
> Get a list of all messages from mbox-file
> For each message, be able to read the header or body individually (so
> that I can apply some operation)
> 
> Does someone have experience in doing something of this sort?
> 

When you create your mailbox you need to provide a factory function, 
otherwise you get rfc822.Message objects.

It's not obvious to me what that facory should be: I'm guessing you 
could get away with something like

mymailbox = mailbox.UnixMailbox(fp, email.parser.Parser().parse)

but I am far from convinced that will work, and have no time for testing 
right now.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list