simple script to read and parse mailbox

fishboy fishboy at spamspamspam.com
Sun Jun 6 22:53:02 EDT 2004


On Sun, 06 Jun 2004 11:15:22 +0100, chuck amadi
<chuck.amadi at ntlworld.com> wrote:

>>
>Well I did hack  most of the code . I was trying using the mboxutils 
>module but I could only get the headers . I assume form this script I 
>can get the text of the body . The reason I haven't tested is while at 
>work I started the write (Oops Hack ) the script then emailed it home . 
>Because I use pop3 account I onlt have a /var/spool/mail/Chucka not as 
>in work /home/User/Mail/inbox that I usuaslly scan to view data in inbox.
>
>So please re-affirm that my hack script will be able to parse the text 
>of the body ( No attachments of binaries will exist within the email 
>messages.
>
>Cheers for you help.
>
>print msg['Body']
>
>I just need the text of the body. But from your psi I can -
>

Ah, the problem is far too simple for our complicated minds.
just do:
body = msg.get_payload()
That will give you the plain text message body of an email

get_payload(decode=True) is for binary stuff (or maybe unicode, maybe)
all that get_content_type(),get_param() stuff can be ignored if you're
just doing plain text
The script you are adapting is for multiple binary (like pictures)
attachments

So, looking at the doc page for mailbox there's an interesting code
fragment:

import email
import mailbox
mbox = mailbox.UnixMailbox(fp, email.message_from_file)

So if you emails are all plain/text you could just write:

import email
import mailbox
fp = open("/var/spool/mail/chucka")
mbox = mailbox.UnixMailbox(fp, email.message_from_file)
bodies = []
for msg in mbox:
	body = msg.get_payload()
	bodies.append(body)

Which will leave you with a list of strings, each one a message body.

msg = email.message_from_file(fileobj) does the same thing as

p = email.Parser.Parser()
msg = p.parse(fileobj)

it's just a short cut
As is passing Unixmailbox email.message_from_file as a handler

You could also do

fp = open("/var/spool/mail/chucka")
mbox = mailbox.UnixMailbox(fp)  # no handler
for mail in mbox:
	msg = email.message_from_file(mail) # handle here
	body = msg.get_payload()


Hth,
><{{{*>




More information about the Python-list mailing list