The following simplified code (text/plain only, no attachments) works well for me:
class InboundMailHandler(InboundMailHandler):
def post(self):
msg = email.message_from_string(self.request.body)
if msg:
charset = 'utf-8'
from_realname, from_emailaddr = email.utils.parseaddr(msg['from'])
to_realname, to_emailaddr = email.utils.parseaddr(msg['to'])
# parse e-mail body, look for text/plain part only
body = ''
if msg.is_multipart():
for part in msg.walk():
if part.get_content_type() and part.get_content_type()=='text/plain': # ignore text/html
charset = part.get_content_charset()
body = part.get_payload(decode=True).decode(part.get_content_charset())
else:
body = msg.get_payload(decode=True)
body = body.decode('utf-8')
subject, encoding = email.header.decode_header(msg['subject'])[0]
if encoding != None:
subject = subject.decode(encoding)
else:
subject = subject.decode('utf-8')