针对类似如下Base64编码的邮件主题进行解码:
Subject: =?UTF-8?B?SVTpupLpup8gaXRreWxpbi5jb20=?=
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | from email.mime.text import MIMEText from email.header import decode_header msg = MIMEText("body ") #msg["Subject"] = "=?ISO-2022-JP?B?GyRCRnxLXDhsGyhC?=" #msg["Subject"] = "=?GB2312?B?us266tbQuNvUy8rk?=" #msg["Subject"] = "=?UTF-8?Q?=E6=9D=8E=E4=BC=9A=E8=AE=A1?=" msg["Subject"] = "=?UTF-8?B?SVTpupLpup8gaXRreWxpbi5jb20=?=" decoded = decode_header(msg["Subject"]) #decoded[0][0] 字节序列 #decoded[0][1] 编码方式 #pythin2 #print(unicode(decoded[0][0], decoded[0][1]).encode("utf-8")) #python3 print(decoded[0][0].decode(decoded[0][1])) |
如上代码,修改Subject行最后两个问号间的编码数据即可,运行结果如下:
root@itkylin:/python# python3 Base64_Subject_Decode.py
IT麒麟 itkylin.com
End.