simple script to read and output Mailbox body to file.

fishboy fishboy at
Mon Jun 7 21:48:49 CEST 2004

On Mon, 07 Jun 2004 17:20:42 +0100, Chuck Amadi <chuck at>

>fp = open("/var/spool/mail/testwwws")
>#fp = open("/home/testwwws/Mail/work")
># message_from_file returns a message object struct tree from an
># open file object.
>mbox = mailbox.UnixMailbox(fp, email.message_from_file)
># list of body messages.
>bodies = []
>msg = email.message_from_file(fp)
>#       # do something(print)
>for msg in mbox:
>        body = msg.get_payload()
>        bodies.append(body)

Trimmed out a few comments

Ok, you dont need to pass UnixMailbox email.message_from_file if your
going to explictly parse later with msg = email.message_from_file(fp)

Your parsing the email message twice.

If there are still problems, try breaking it down a bit

mbox = mailbox.UnixMailbox(fp)
for mail in mbox:
	break #just look at one message

'mail' is a file obj pointing at a single email, so print
should display one email, with headers.  If that doesn't work we have
problems before we get anywhere

If that gives us something sensible for an output, try parsing the

mbox = mailbox.UnixMailbox(fp)
for mail in mbox:
	msg = email.message_from_file(mail)
	print `msg` #backticks
	print msg['Subject']
	print msg.get_content_type()
	break #just look at one message

Should give us something like:

<email.Message.Message instance at 0xa0e442c>
Re: Best IDE?

And if that works, then

msg.get_payload() should return the body.  


More information about the Python-list mailing list