I got the mail portion working [python]

David Stockwell winexpert at hotmail.com
Fri Jun 4 10:56:49 EDT 2004


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 at hgulp@sparks.not> ]
        reply to[ Jerome plugh <plugh at hgulp@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/





More information about the Python-list mailing list