How to parse inbound GAE email manually?

2019-09-10 01:27发布

Since I am having problems with standard GAE's e-mail functionality when e-mail subject is UTF-8 encoded, I am trying to handle it manually:

msg_encoding = self.request.headers['Content-Type'].split('charset=')[1] # message/rfc822; charset=UTF-8
msg = email.message_from_string(self.request.body)
if msg:
    logging.debug(msg.get_content_charset()) # None
    logging.debug(msg['to'])
    logging.debug(msg['from'])
    logging.debug(msg['Subject'].decode(msg_encoding))

Do I do it correctly? Should I decode Subject? Or is it done by email parser automatically?

1条回答
干净又极端
2楼-- · 2019-09-10 01:42

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')
查看更多
登录 后发表回答