views:

39

answers:

1

I'm writing a Google App engine app that processes incoming mail, and here's the code I'm currently using to process mail messages:

for content_type, body in email_bodies:
#8bit bug in mail messages - see bug report here
#http://code.google.com/p/googleappengine/issues/detail?id=2383
if body.encoding == '8bit':
    body.encoding = '7bit'

#test for html content
if content_type == "text/html":
    #parse html result

if content_type == "text/plain":
    decoded_msg_body = body.decode()

However I just got a message that was using the binary encoding scheme, and when my program tried to process the message using body.decode(), I received a UnknownEncodingError. How should this program parse the binary content type? Also, how can I imitate this message type on my local version of GAE so I can debug and test it out?

I appreciate your help, Kevin

+1  A: 

Rather than reinventing the wheel, you should try Python's built in email parser.

http://docs.python.org/library/email.parser.html

It's designed to handle the lifting involved in getting all sorts of different email formats into a good Python object. Use it to do the parsing, and you'll get nicely predictable objects to work with.

The email module doesn't do message sending and receiving, it just helps put them together and parse them out.

Paul McMillan
It'll work on GAE if you use the "original" property of the message, fair enough. Thanks Paul.
Kevin Burke
Awesome. I wasn't certain how good it's odd-case handling was, but given how these things usually work, I figured it probably was a good shot.
Paul McMillan