Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Working with email and mailbox module

Reply
Thread Tools

Working with email and mailbox module

 
 
Nirnimesh
Guest
Posts: n/a
 
      09-21-2006
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?

 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      09-21-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

 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      09-21-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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mailbox module difficulties Eduardo Alvarez Python 3 11-28-2011 06:10 PM
mailbox module ra9ftm Python 1 08-26-2008 07:01 AM
mailbox module, Maildir, and flags jesse.k.rosenthal@gmail.com Python 0 09-11-2007 12:29 AM
only email from my home mailbox is unable to reach my work email system coontrax Computer Support 0 02-09-2004 03:22 AM
Perl module to clear pop mailbox? AcCeSsDeNiEd Perl Misc 4 10-08-2003 05:24 AM



Advertisments