10个正确抓取的邮件后,为什么会出现Imaplib.abort EOF错误?

时间:2017-06-22 12:12:15

标签: python outlook imap

我需要将来自我的收件箱&+ 39; +子文件夹的每封邮件复制到2个新文件夹,一个用于短主题,另一个用于长文件夹。 抓取总是在第10封邮件之后中止,我需要重复其中的10 000个进程..如果有人有解决方案我会非常感激,这是我的代码:

import imaplib, getpass, re, email
pattern_uid = re.compile(b'\d+ \(UID (?P<uid>\d+)\)')

def parse_mailbox(data):
    flags, b, c = data.partition(' ')
    separator, b, name = c.partition(' ')
    return (flags, separator.replace('"', ''), name.replace('"', ''))

def connect(email):
    imap = imaplib.IMAP4_SSL("outlook.office365.com",993)
    password = "*******" 
    imap.login(email, password)
    return imap

def disconnect(imap):
    imap.logout()

def parse_uid(data):
    match = pattern_uid.match(data)
    return match.group('uid')

if __name__ == '__main__':
    imap = connect('************')
    rv,data = imap.list()
    # print(data)
    if rv == 'OK':
        for mbox in data:
            flags,separator,name = parse_mailbox(bytes.decode(mbox))
            print(name)
            if 'HasNoChildren' in flags and '2' in name:
                name = name.replace('/ Inbox','Inbox')
                rv2, data2 = imap.select('"'+name+'"')
                print(rv2)
                resp, items = imap.search(None, 'All')
                email_ids  = items[0].split()
                # print(items)
                print(email_ids)
                 # Assuming that you are moving the latest email.
                # for mail in email_ids:
                #     if
                print(len(email_ids))
                for i in range(0,len(email_ids)):
                    print('NOUVEAU MESSAGE \n')
                    print(email_ids[i])
                    resp, data = imap.fetch(email_ids[i], "(UID)")
                    rv,sujet = imap.fetch(email_ids[i],("(RFC822)"))
                    varSubject= ""
                    for response_part in sujet:
                        if isinstance(response_part, tuple):

                            msg = email.message_from_bytes(response_part[1])
                            varSubject = msg['subject']
                            break
                    print(varSubject)
                    print(i)
                    print(type(data[0]))
                    print(data[0])
                    try : 
                        msg_uid = parse_uid(data[0])
                    except AttributeError:
                        continue
                    num = ""
                    dossier = ""
                    if  len(varSubject) <= 20:
                        dossier =  'INBOX/sujets_courts'
                        imap.create(dossier)
                    elif  len(varSubject) > 20:
                        dossier =  'INBOX/sujets_longs'
                        imap.create(dossier)
                    print(varSubject+'******'+dossier)
                    print(msg_uid)

                    result = imap.uid('COPY', msg_uid, dossier)

            # if result[0] == 'OK':
            #     mov, data = imap.uid('STORE', msg_uid , '+FLAGS', '(\Deleted)')
            #     imap.expunge()

    disconnect(imap)

电子邮件和密码被&#39; ****&#39;取代

非常感谢:)

控制台输出:

    Traceback (most recent call last):
  File "C:\Users\lschaub\AppData\Local\Programs\Python\Python36-32\lib\imaplib.py", line 1011, in _command_complete
    typ, data = self._get_tagged_response(tag)
  File "C:\Users\lschaub\AppData\Local\Programs\Python\Python36-32\lib\imaplib.py", line 1131, in _get_tagged_response
    self._get_response()
  File "C:\Users\lschaub\AppData\Local\Programs\Python\Python36-32\lib\imaplib.py", line 1039, in _get_response
    resp = self._get_line()
  File "C:\Users\lschaub\AppData\Local\Programs\Python\Python36-32\lib\imaplib.py", line 1143, in _get_line
    raise self.abort('socket error: EOF')
imaplib.abort: socket error: EOF

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "movemail.py", line 44, in <module>
    resp, data = imap.fetch(email_ids[i], "(UID)")
  File "C:\Users\lschaub\AppData\Local\Programs\Python\Python36-32\lib\imaplib.py", line 526, in fetch
    typ, dat = self._simple_command(name, message_set, message_parts)
  File "C:\Users\lschaub\AppData\Local\Programs\Python\Python36-32\lib\imaplib.py", line 1188, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "C:\Users\lschaub\AppData\Local\Programs\Python\Python36-32\lib\imaplib.py", line 1013, in _command_complete
    raise self.abort('command: %s => %s' % (name, val))
imaplib.abort: command: FETCH => socket error: EOF

1 个答案:

答案 0 :(得分:0)

好的我发现问题来自于创建一个已经创建的文件夹,但我在imaplib文档中找不到任何folder.exists()看起来类似的方法......