simple script to read and parse mailbox

chuck amadi chuck.amadi at ntlworld.com
Sat Jun 5 10:27:36 EDT 2004


Hi , Im trying to parse a specific users mailbox (testwwws) and output 
the body of the messages to a file ,that file will then be loaded into a 
PostGresql DB at some point .

I  have read the email posts and been advised to use the email Module 
and mailbox Module.

The blurb from a memeber of this list . Im not at work at the moment So 
I cant test this out , but if someone could take a look and check that 
im on the write track as this Monday I need to show my Boss and get the 
body data out of the user's mailbox.

**Blurb form a member who's directed me**

Thus started with the mailbox and email modules.  Mailbox lets you iterate over a 
mailbox yielding individual messages of type email.  The e-mail object lets 
you parse and operate on the message components.  From there you should be 
able to extract your data.




## The email messages is read as flat text form a file or other source,
##the text is parsed to produce the object structure of the email message.
#!/usr/bon/env python
import mboxutils
import mailbox
import email
import sys
import os
import rfc822
import StringIO
import email.Parser
import types
 
# email package for managing email messages
# Open Users Mailbox
# class Message()
#mbox = mailbox.UnixMailbox(open("/var/spool/mail/chucka"))

def main():
 
# The Directory that will contain the Survey Results
 
    dir = "/tmp/SurveyResults/"

# The Web Survey User Inbox
# Mailbox /home/testwwws/Mail/inbox
 
    maildir = "/home/testwwws/Mail/inbox"
    for file in os.listdir(maildir):
 
        print os.path.join(maildir, file)
 
        fp = open(os.path.join(maildir, file), "rb")
        p = email.Parser.Parser()
        msg = p.parse(fp)
        fp.close()
        #print msg.get("From")
        #print msg.get("Content-Type")
 
        counter = 1
        for part in msg.walk():
          if part.get_main_type() == 'multipart':
            continue
 
          filename = part.get_param("name")
          if filename==None:
                filename = "part-%i" % counter
          counter += 1
 
 
          fp = open(os.path.join(dir, filename), 'wb')
          print os.path.join(dir, filename)
          fp.write(part.get_payload(decode=1))
          fp.close()
 
 
if __name__ == '__main__':
    main()

Cheers all this list has been very helpful.





More information about the Python-list mailing list