在python中解码utf-8电子邮件附件文件名?

时间:2018-06-18 08:55:11

标签: python email utf-8

我收到了一封附件名称如下的电子邮件:

=?UTF-8?b?cmVhbF9hdHRhY2htZW50X25hbWUueGxz?=

但是,如果我查看Gmail中的原始文件,则会显示如下:

------=_Part_1264195_253682143.1529181492460
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="real_attachment_name.xls"


------=_Part_1264195_253682143.1529181492460--

如何获得真实的附件名称?

1 个答案:

答案 0 :(得分:0)

这种情况发生是因为电子邮件没有设想支持utf-8,所以它已经被提升了。这里发生的是Content-Transfer-Encoding标题告诉你,你有一些base64正在进行的事情,您需要按如下方式解码文件名:

for part in mail.walk():
    if part.get_content_maintype() == 'multipart' or part.get('Content-Disposition') is None:
        continue

    filename = part.get_filename()

    # Find non-ascii filenames and decode

    transfer_encoding = part.get_all('Content-Transfer-Encoding')
    if transfer_encoding and transfer_encoding[0] == 'base64':
        filename_parts = filename.split('?')
        filename = base64.b64decode(filename_parts[3]).decode(filename_parts[1])