首页 > 解决方案 > 在 Python 中禁用 IMAP 注销

问题描述

好的,所以我今天决定从旧电子邮件帐户中删除所有旧电子邮件。现在我有大约 40,000 多封电子邮件需要删除,所以我采用了显而易见的方法并制作了一个脚本来为我执行此操作。

一切正常运行了大约10 分钟,直到我遇到 IMAP 给出 Loggout 错误的障碍。代码运行良好,我只需要知道是否有办法禁用或绕过它,这样我就可以运行这个脚本,直到我的所有电子邮件都被删除。

这是脚本(由于明显的原因更改了用户名和密码):

import imaplib
import email
from tqdm import tqdm
from email.header import decode_header

# Account credentials
username = "username@domain.com"
password = "password"

imap = imaplib.IMAP4_SSL("imap.mail.yahoo.com")
imap.login(username, password)

imap.select("Archive")

status, messages = imap.search(None, "ALL")
messages = messages[0].split(b' ')
amtOfMessages = len(messages)

print("Clearing all emails in the ARCHIVE folder")
print("Total emails in folder: ", amtOfMessages)

#tqdm is a library I'm using to display a progress bar in the cmd line
for i in tqdm(range(amtOfMessages)):
    _, msg = imap.fetch(messages[i], "(RFC822)")
    imap.store(messages[i], "+FLAGS", "\\Deleted")

imap.expunge()
imap.close()
imap.logout()

print("ALL MESSAGES DELETED FROM ARCHIVE FOLDER")
print("Logged out of account...")

最后,这是我得到的错误的回溯

Traceback (most recent call last):
  File "/Documents/EmailScripts/quickstart.py", line 27, in <module>
    _, msg = imap.fetch(messages[i], "(RFC822)")
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/imaplib.py", line 548, in fetch
    typ, dat = self._simple_command(name, message_set, message_parts)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/imaplib.py", line 1230, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/imaplib.py", line 1049, in _command_complete
    raise self.abort('command: %s => %s' % (name, val))
imaplib.abort: command: FETCH => IMAP4rev1 Server logging out

编辑:说“超时”时说错了。意思是注销。已进行更改。

标签: pythonemailtimeoutruntime-errorimap

解决方案


推荐阅读