1
以下代码是我对如何使用imaplib删除电子邮件的理解。它包括在删除之前将电子邮件移动到“垃圾箱”,我相信这是使用Gmail时的要求。使用imaplib(gmail)删除电子邮件
但是,正如你所看到的,我似乎正处于标签修改阶段。我已经在堆栈中查看过其他类似的主题,尽管尝试了多个建议的解决方案,但我一直无法解决此问题。
>>> import imaplib
>>> server = imaplib.IMAP4_SSL(GMAIL_IMAP)
>>> server.login(EMAIL, PASSWORD)
('OK', [b'[email protected] authenticated (Success)'])
>>> server.select("INBOX")
('OK', [b'17'])
>>> status, uids = server.uid("search", None, "ALL")
>>> uids
[b'1 2 3 4 5 6 7 8 9 10 15 16 17 18 19 43 44']
uids = [uid for uid in uids[0].split()]
>>> uids
[b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'10', b'15', b'16', b'17', b'18', b'19', b'43', b'44']
>>> server.store(uids[-1], "X-GM-LABELS", "\\Trash")
('OK', [None])
>>> server.store(uids[-1], "+FLAGS", "\\Deleted")
('OK', [None])
>>> server.expunge()
('OK', [None])
>>> server.close()
('OK', [b'Returned to authenticated state. (Success)'])
>>> server.logout()
('BYE', [b'LOGOUT Requested'])