Working with email and mailbox module

Discussion in 'Python' started by Nirnimesh, Sep 21, 2006.

  1. Nirnimesh

    Nirnimesh Guest

    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?
     
    Nirnimesh, Sep 21, 2006
    #1
    1. Advertising

  2. Nirnimesh

    Steve Holden Guest

    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
     
    Steve Holden, Sep 21, 2006
    #2
    1. Advertising

  3. 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/
     
    Rob Williscroft, Sep 21, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    353
  2. ra9ftm

    mailbox module

    ra9ftm, Aug 26, 2008, in forum: Python
    Replies:
    1
    Views:
    315
    ra9ftm
    Aug 26, 2008
  3. Skye
    Replies:
    1
    Views:
    378
    Dennis Lee Bieber
    Sep 24, 2010
  4. Eduardo Alvarez

    mailbox module difficulties

    Eduardo Alvarez, Nov 28, 2011, in forum: Python
    Replies:
    3
    Views:
    153
    Eduardo Alvarez
    Nov 28, 2011
  5. Replies:
    2
    Views:
    134
    David Efflandt
    Aug 22, 2003
Loading...

Share This Page