0

我用 GAE 设置了一个邮件服务器。以下代码段显示了我如何接受和存储传入的邮件。

class ReceiveMailHandler(InboundMailHandler):
    def receive(self, mail_message):
        from uuid import uuid4
        path_info = self.request.path_info
        Mail(id=uuid4().hex,
             receiver=path_info[len('/_ah/mail/'):].replace(at_domain, '@~'),
             sender=mail_message.sender,
             to=getattr(mail_message, 'to', ''),
             cc=getattr(mail_message, 'cc', ''),
             subject=getattr(mail_message, 'subject', ''),
             body = '\n--\n\n'.join(b.decode()
                 for t, b in mail_message.bodies('text/plain')),
             html = '\n--\n\n'.join(b.decode()
                 for t, b in mail_message.bodies('text/html')),
             ).put()

现在我收到一封邮件,其发件人是"=?utf-8?b?55+l5LmO?=" <notification@mail.zhihu.com> 如何"=?utf-8?b?55+l5LmO?="使用 Python 解码字符串?

4

1 回答 1

1

使用编解码器模块。由于发件人的信息还包含有关其编码方式的任意数据,因此您需要先对其进行解析,然后再对其进行解码

import base64

def sender_decode(sender):
    parsed_string = sender.split("?")

    decoded = base64.b64decode(parsed_string[3]).decode(parsed_string[1], "ignore")
    return decoded

sender_decode("=?utf-8?b?55+l5LmO?=")
于 2013-09-17T07:27:45.117 回答