By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

I got the mail portion working [python]

P: n/a
Thanks for the help in breaking the problem down into manageable pieces

This is the code snippet I ended up using: (I'm decoding excel formatted
files)
the file is an email i created with pine that has an excel file in it and
also a csv file in it, both as MIME attachments. I'm only keeping the
excel data, but fwiw I also found 3 other MIME types in the email:

~/mymail/test] python test.py
from[ Jerome plugh <plugh@hg***@sparks.not> ]
reply to[ Jerome plugh <plugh@hg***@sparks.not> ]
Id[ None ]
multipart[<bound method Message.is_multipart of <email.Message.Message
instance at 0x8169554>>]
partition type[multipart/mixed]
Partition #[ 1 ]
partition Name[ None ]
partition type[text/plain]
Partition #[ 2 ]
partition Name[ None ]
partition type[application/vnd.ms-excel]
Partition #[ 3 ]
partition Name[sample.xls ]
partition type[text/tab-separated-values]
Partition #[ 4 ]
partition Name[ sample.tsv ]
total part count 4
-----

payload = {}
text = open('/tmp/test/it').read()
message = email.message_from_string(text)
hdr = dict(message._headers)

addressFrom = hdr["From"]
addressReply = hdr.get("Reply-To", addressFrom)
messageId = hdr.get("Message-Id")

print "\tfrom[", addressFrom, "]\n\treply to[", addressReply,
"]\n\tId[", messageId, "]"

print "multipart[%s]" % message.is_multipart

count = 0

for partition in message.walk():

pType = partition.get_type()
print "partition type[%s]" % pType
count += 1
print "\tPartition #[", count, "]"
fName = partition.get_filename()
print "\t\tpartition Name[", fName , "]"

if pType == "application/vnd.ms-excel":
part = partition.get_payload(decode=1)

print "\t\tpart Info[", part, "]"

payload[fName] = part

print "total part count", count

David
-------
Tracfone: http://cellphone.duneram.com/index.html
Cam: http://www.duneram.com/cam/index.html
Tax: http://www.duneram.com/index.html

__________________________________________________ _______________
Looking to buy a house? Get informed with the Home Buying Guide from MSN
House & Home. http://coldwellbanker.msn.com/
Jul 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.