parsing emails and saving attached application files

Andrew Gould andrewgould at yahoo.com
Mon Jun 9 10:11:40 EDT 2003


As a learning exercise I'm trying to parse my email
messages. (The next step will be to save the
components in a database.) How do I save attached
application files separately from the email?

In the script below, the email message contains some
text and 2 excel files. Variable payload1a will give
me the contents of of Book1.xls as a string. How do I
save the contents as an Excel file (rather than a text
file with unreadable content)?

# begin script
import email

msgfile = open('a:/testmsg','r')
msg = email.message_from_file(msgfile)

payload1 = msg.get_payload(i=1,decode=0)
payload1a = payload1.get_payload(i=None,decode=0)
# end script

Any help would be greatly appreciated.

Thanks,

Andrew





More information about the Python-list mailing list