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

自闭症网瘾萝莉.ら 提交于 2019-12-18 03:16:12

问题


Can you delete emails with imaplib? If so how?


回答1:


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; then the expunge method to actually perform all deletions so marked.

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).




回答2:


Deleting an email over IMAP is performed in two phases:

  • mark one or more items for deletion: imap.store(msg_no, '+FLAGS', '\\Deleted')
  • expunge the mailbox: imap.expunge()

(imap is your IMAP4 object)




回答3:


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

i.e

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.



来源:https://stackoverflow.com/questions/1777264/using-python-imaplib-to-delete-an-email-from-gmail

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!