我使用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>
的邮件
我怎么能用Python解码字符串"=?utf-8?b?55+l5LmO?="
?
答案 0 :(得分: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?=")