Working with email and mailbox module

Rob Williscroft rtw at freenet.co.uk
Thu Sep 21 15:48:23 EDT 2006


Nirnimesh wrote in news:1158840271.942540.85640
@d34g2000cwd.googlegroups.com in comp.lang.python:

> 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?
> 

Not really, but this is what I came up with the other day to read
one of my newsreaders mbx files:

MBX = r"<<<-insert-path-to-your-mbx->>>"

import mailbox, email

fmbx = open( MBX, 'rb' )
mbx = mailbox.PortableUnixMailbox( fmbx,  email.message_from_file )

for i, msg in enumerate( mbx ):
  print msg.__class__
  for i in msg.keys(): # gets header names
    print i
  break

fmbx.close()


http://docs.python.org/lib/module-email.Message.html



Rob.
-- 
http://www.victim-prime.dsl.pipex.com/



More information about the Python-list mailing list