Python电子邮件正文内容Base64无法解码

时间:2018-12-26 16:50:53

标签: python email-parsing

在尝试解析电子邮件正文时,正文将显示为

VCBJTkZPUk1BVElPTjwvdGQ+Cgk8L3RyPgoJPHRyPjx0ZD4mbmJzcDs8L3RkPjwvdHI+Cgk8dHI+

当我尝试单独解码时,它可以成功工作

import base64
data="VCBJTkZPUk1BVElPTjwvdGQ+Cgk8L3RyPgoJPHRyPjx0ZD4mbmJzcDs8L3RkPjwvdHI+Cgk8dHI+"
print(base64.b64decode((data)))

输出:

b'T INFORMATION</td>\n\t</tr>\n\t<tr><td>&nbsp;</td></tr>\n\t<tr>'

但是当我在邮件解析脚本中尝试相同的操作时,它不起作用

try:
    mail = imaplib.IMAP4_SSL('imap.gmail.com')
    mail.login(FROM_EMAIL,FROM_PWD)
    mail.select('inbox')

    type, data = mail.search(None, 'ALL')
    mail_ids = data[0]

    id_list = mail_ids.split()
    first_email_id = int(id_list[0])
    latest_email_id = int(id_list[-1])
    print(id_list)
    print(first_email_id)
    print(latest_email_id)
    for i in data[0].decode().split(' '):
        print(i)
        typ, data = mail.fetch(i, '(RFC822)' )
        data=(data[0][1])
        print(base64.b64decode(data))

except Exception as e:
    print(str(e))

输出如下:

b"\r\xe9b\xbd\xea\xdeu:-\xa2|\xa9\xae\x8b^rH&j)\\\"

有什么办法可以解码吗?

0 个答案:

没有答案