使用 python imaplib 从 Gmail 中“删除”一封电子邮件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1777264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:01:40  来源:igfitidea点击:

Using python imaplib to "delete" an email from Gmail?

pythonemailimap

提问by Demon Labs

Can you delete emails with imaplib? If so how?

您可以使用 imaplib 删除电子邮件吗?如果是这样怎么办?

回答by Alex Martelli

Use the storemethod (of the IMAP4object representing your connection) to set the r'\Deleted'flag on the message number you want to delete, as the example in the docs show; then the expungemethod to actually perform all deletions so marked.

使用store方法(IMAP4代表您的连接的对象)r'\Deleted'在要删除的消息编号上设置标志,如文档中的示例所示;然后expunge方法实际执行所有如此标记的删除。

Gmail's implementation of IMAP has subtly different semantics, by default, but if you want you can tweakit to behave much more like a traditional IMAP implementation (where the above sequence works) -- basically you have to enable the "Advanced IMAP Controls" lab, then follow the instructions at the URL I gave to get exactly the IMAP semantics you desire (physically deleting rather than archiving "deleted" mails, waiting or not for expunge, and so forth).

默认情况下,Gmail 的 IMAP 实现具有微妙的不同语义,但如果您愿意,您可以对其进行调整,使其表现得更像传统的 IMAP 实现(上述序列有效)——基本上,您必须启用“高级 IMAP 控制”实验室,然后按照我提供的 URL 中的说明准确获取您想要的 IMAP 语义(物理删除而不是归档“已删除”的邮件、等待或不等待expunge,等等)。

回答by SimonJ

Deleting an email over IMAP is performed in two phases:

通过 IMAP 删除电子邮件分两个阶段执行:

  • mark one or more items for deletion: imap.store(msg_no, '+FLAGS', '\\Deleted')
  • expunge the mailbox: imap.expunge()
  • 标记一个或多个要删除的项目: imap.store(msg_no, '+FLAGS', '\\Deleted')
  • 删除邮箱: imap.expunge()

(imapis your IMAP4object)

imap是你的IMAP4对象)

回答by Avadhesh

imap.uid('STORE', list_of_msgno , '+FLAGS', '(\Deleted)')  
imap.expunge() 

i.e

IE

imap.uid('STORE', '2, 4, 9, 12' , '+FLAGS', '(\Deleted)') 

Here (2, 4, 9, 12)are uidof the messages which are going to be deleted.

以下(2, 4, 9, 12)uid将要删除的消息。