简体   繁体   English

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

[英]Using python imaplib to “delete” an email from Gmail?

Can you delete emails with imaplib? 您可以使用imaplib删除电子邮件吗? If so how? 如果可以,怎么办?

Use the store method (of the IMAP4 object representing your connection) to set the r'\\Deleted' flag on the message number you want to delete, as the example in the docs show; 使用存储方法(代表您的连接的IMAP4对象的方法)在要删除的消息号上设置r'\\Deleted'标志,如docs所示。 then the expunge method to actually perform all deletions so marked. 然后使用expunge方法实际执行如此标记的所有删除操作。

Gmail's implementation of IMAP has subtly different semantics, by default, but if you want you can tweak it 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 ,等等)。

Deleting an email over IMAP is performed in two phases: 通过IMAP删除电子邮件分为两个阶段:

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

( imap is your IMAP4 object) imap是您的IMAP4对象)

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

ie

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

Here (2, 4, 9, 12) are uid of the messages which are going to be deleted. 这里(2, 4, 9, 12) uid (2, 4, 9, 12)是要删除的消息的uid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM