I got the mail portion working [python]

  • Thread starter David Stockwell
  • Start date
D

David Stockwell

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@[email protected]> ]
reply to[ Jerome plugh <plugh@[email protected]> ]
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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top