使用Python程序解码邮件主题

要解码邮件主题的编码可以参考之前的文章”Python解码base64编码的邮件主题“.也可以参考以下的python程序,效果是一样的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import email.header
 
def decode_mime_words(s):
    return u''.join(
        word.decode(encoding or 'utf8') if isinstance(word, bytes) else word
        for word, encoding in email.header.decode_header(s))
 
#print(decode_mime_words(u'=?ISO-2022-JP?B?GyRCRnxLXDhsGyhC?='))
#print(decode_mime_words(u'=?GB2312?B?us266tbQuNvUy8rk?='))
#print(decode_mime_words(u'=?UTF-8?B?5pyq55m86YCB6YO1566x6YCa?='))
#print(decode_mime_words(u'=?UTF-8?Q?=E6=9D=8E=E4=BC=9A=E8=AE=A1?='))
#print(decode_mime_words(u'=?utf-8?Q?=E5=87=AF=E7=A5=A8?='))
#print(decode_mime_words(u'=?gb18030?B?hVLS5t9c3ZQgtPPqkdbBz+O42yCMo76Azu/B9yA=?='))
#print(decode_mime_words(u'=?gb18030?B?hVLS5t9c3ZQ?='))
print(decode_mime_words(u'=?UTF-8?B?SVTpupLpup8gaXRreWxpbi5jb20=?='))
#print(decode_mime_words(u'=??='))

如上最后一行代码,只需取消注解释,在两个问号间插入编码数据即可,运行结果如下:
root@itkylin:/python# python3 Subject_Decode.py
IT麒麟 itkylin.com