我有这个功能,可以使用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\u filename()没有正确读取名称:
OSError: [Errno 22] Invalid argument: './temp//=?UTF-8?B?QkQgUmVsYXTDs3JpbyAywqogRmFzZS5kb2M=?=\r\n\t=?UTF-8?B?eA==?='
我能做些什么来解决这个问题
我找到了解决办法
这是一个老问题,但我面对这个问题,很难找到解决办法。。。也许这可以帮助其他人
编辑:这仅包括将文件名“解码”为正确文件名的部分
结果:
相关问题 更多 >
编程相关推荐