Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python imaplib-get_filename()在附件包含UTF-8字符时不工作_Python_Python 3.x_Imaplib - Fatal编程技术网

Python imaplib-get_filename()在附件包含UTF-8字符时不工作

Python imaplib-get_filename()在附件包含UTF-8字符时不工作,python,python-3.x,imaplib,Python,Python 3.x,Imaplib,我有这个功能,可以使用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.

我有这个功能,可以使用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文件名没有正确读取名称:

    OSError: [Errno 22] Invalid argument: './temp//=?UTF-8?B?QkQgUmVsYXTDs3JpbyAywqogRmFzZS5kb2M=?=\r\n\t=?UTF-8?B?eA==?='
我能做些什么来解决这个问题?

我找到了一个解决方案

# 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:
            filename, encoding = decode_header(part.get_filename())[0]
            if(encoding is None):
                open(outputdir + '/' + filename, 'wb').write(part.get_payload(decode=True))
            else:
                open(outputdir + '/' + filename.decode(encoding), 'wb').write(part.get_payload(decode=True))
    if(markRead):
        m.uid("STORE", emailid, "+FLAGS", "(\Seen)")**

这是一个老问题,但我面对这个问题,很难找到解决办法。。。也许这可以帮助其他人

编辑:这只包括将文件名解码为正确文件名的部分

import re
import base64
import quopri

def encoded_words_to_text(encoded_words):
    try:
        encoded_word_regex = r'=\?{1}(.+)\?{1}([B|Q])\?{1}(.+)\?{1}='
        charset, encoding, encoded_text = re.match(encoded_word_regex, encoded_words).groups()
        if encoding is 'B':
            byte_string = base64.b64decode(encoded_text)
        elif encoding is 'Q':
            byte_string = quopri.decodestring(encoded_text)
        return byte_string.decode(charset)
    except:
        return encoded_words
结果:

test_string = '=?utf-8?B?SUJUIFB1cmNoYXNlIE9yZGVyLnBkZg==?='
encoded_words_to_text(test_string)
'IBT Purchase Order.pdf'

是否可以添加所有导入的模块?请注意,decode_header函数可在email.header导入中找到。