simple script to read and output Mailbox body to file.
fishboy
fishboy at spamspamspam.com
Mon Jun 7 15:48:49 EDT 2004
On Mon, 07 Jun 2004 17:20:42 +0100, Chuck Amadi <chuck at smtl.co.uk>
wrote:
>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:
print mail.read()
break #just look at one message
'mail' is a file obj pointing at a single email, so print mail.read()
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
email.
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?
text/plain
And if that works, then
msg.get_payload() should return the body.
><{{{*>
More information about the Python-list
mailing list