当附件包含UTF-8字符时,Python imaplib-get_filename()不起作用

问题描述 投票:0回答:1

我具有使用imaplib从给定电子邮件中下载所有附件的功能

# Download all attachment files for a given email
def downloaAttachmentsInEmail(m, emailid, outputdir, markRead):
    resp, data = m.uid("FETCH", emailid, "(BODY.PEEK[])")
    email_body = data[0][1]
    mail = email.message_from_bytes(email_body)
    if mail.get_content_maintype() != 'multipart':
        return
    for part in mail.walk():
        if part.get_content_maintype() != 'multipart' and part.get('Content-Disposition') is not None:
            open(outputdir + '/' + part.get_filename(), 'wb').write(part.get_payload(decode=True)
    if(markRead):
        m.uid("STORE", emailid, "+FLAGS", "(\Seen)")

问题是,当我尝试下载文件名中包含UTF-8字符的文件时,它不起作用。我收到此错误,我猜发生了,因为part.get_filename()没有正确读取名称:

    OSError: [Errno 22] Invalid argument: './temp//=?UTF-8?B?QkQgUmVsYXTDs3JpbyAywqogRmFzZS5kb2M=?=\r\n\t=?UTF-8?B?eA==?='

我该怎么解决?

python python-3.x imaplib
1个回答
0
投票
我找到了解决方案
© www.soinside.com 2019 - 2024. All rights reserved.