簡體   English   中英

使用imaplib刪除gmail中的電子郵件時出現問題

[英]Problem deleting emails in gmail using imaplib

我嘗試從收件箱文件夾中刪除郵件,一切正常,但當我切換到所有郵件文件夾時刪除不起作用。 expunge()方法返回('OK', [None])並且未刪除消息:

>>>import imaplib
>>>server = imaplib.IMAP4_SSL('imap.gmail.com','993')
>>>server.login('likvidator89@gmail.com','Password')
>>>server.select('inbox')
>>>for i in server.search(None,'all')[1][0].split():
...    print i+"\n"+server.fetch(i,'(BODY[HEADER.FIELDS (Subject)])')[1][0][1]
...
#  that how i know what UID hame my message? I select by subject
#....
#28
#Subject: 1 Question Has 1 Answer - Stack Overflow
#
#
#29
#Subject: 2222222222
#...
>>>server.store(29,'+FLAGS','\\Deleted')
#('OK', ['29 (FLAGS (\\Seen \\Deleted))'])
>>>server.expunge()
#('OK', ['29'])
>>> server.select('[Gmail]/All Mail')
('OK', ['47'])
>>> for i in server.search(None,'all')[1][0].split():
...  print i+"\n"+server.fetch(i,'(BODY[HEADER.FIELDS (Subject)])')[1][0][1]
... 
#....
#
#46
#Subject: 2222222222
#
#
#47
#Subject: 3333333333333333
#
#....
>>> server.store(47,'+FLAGS','\\Deleted')
('OK', ['47 (FLAGS (\\Seen \\Deleted))'])
>>> server.expunge()
('OK', [None])

它將給定gmail標簽中的所有郵件移動到gmail Trash

#!usr/bin/python
import email, imaplib

user = 'xxx'
pwd = 'xxx'

m = imaplib.IMAP4_SSL("imap.gmail.com")
m.login(user,pwd)

m.select("some_gmail_label")
m.store("1:*",'+X-GM-LABELS', '\\Trash')

m.expunge() # should be useless, but gmail server says it is ok

請記住刷新g​​mail界面,因為它有緩存

正如它在gmail博客網站上所說,谷歌的IMAP實現有點不同。 當您按照說明獲取更常用的語義時,它有幫助嗎?

對於那些希望讓Gmail的IMAP工作更像傳統IMAP提供商的人來說,還有一些更加模糊的選項:當通過IMAP不再可見時,您可以關閉自動清除或廢棄郵件。

IMAP協議允許將消息標記為刪除,這是一種不明確狀態,其中消息仍然存在於文件夾中,但是下次在文件夾被清除時將被刪除。 在我們的標准IMAP實施中,當您將郵件標記為已刪除時,Gmail不會讓其在該狀態下逗留 - 它會立即從文件夾中刪除(或自動刪除)該郵件。 如果您需要兩階段刪除過程,請在啟用此實驗室后,在“設置”中的“轉發和POP / IMAP”標簽下選擇“不自動刪除郵件”。

同樣,大多數IMAP系統都不會共享Gmail的歸檔消息概念(將消息發送到[Gmail] / All Mail文件夾而不是[Gmail] / Trash)。 如果您希望將未保留在任何其他可見IMAP文件夾中的已刪除郵件發送到[Gmail] /已刪除郵件,則可以使用高級IMAP控件以這種方式設置首選項。 在“轉發和POP / IMAP”標簽的“IMAP訪問:”部分中,找到“當從上一個可見的IMAP文件夾中刪除郵件時:”選項。 選擇“將郵件移至Gmail回收站”。 如果您想更進一步,可以選擇“立即刪除郵件”。

使用Gmail高級IMAP控件,您可以設置在通過IMAP刪除郵件時會發生什么。

只需在Gmail實驗室中啟用“高級IMAP控件”即可。 設置頁面如下所示:

設置

然后,當您將郵件標記為已刪除並按照此答案進行清除時,它將被移至郵箱,永久刪除或存檔為“所有郵件”,具體取決於您選擇的設置。

這是一個適用於Gmail並且非常快速(沒有for循環存儲)的版本。 根據您的意願定制,但想法是如何使用選擇/搜索然后執行商店,或只是自己選擇具有特定標簽/文件夾的所有項目:

#!/bin/python

import datetime
import imaplib

m = imaplib.IMAP4_SSL("imap.gmail.com")  # server to connect to
print "Connecting to mailbox..."
m.login('gmail@your_gmail.com', 'your_password')

print m.select('[Gmail]/All Mail')  # required to perform search, m.list() for all lables, '[Gmail]/Sent Mail'
before_date = (datetime.date.today() - datetime.timedelta(365)).strftime("%d-%b-%Y")  # date string, 04-Jan-2013
typ, data = m.search(None, '(BEFORE {0})'.format(before_date))  # search pointer for msgs before before_date

if data != ['']:  # if not empty list means messages exist
    no_msgs = data[0].split()[-1]  # last msg id in the list
    print "To be removed:\t", no_msgs, "messages found with date before", before_date
    m.store("1:{0}".format(no_msgs), '+X-GM-LABELS', '\\Trash')  # move to trash
    print "Deleted {0} messages. Closing connection & logging out.".format(no_msgs)
else:
    print "Nothing to remove."

#This block empties trash, remove if you want to keep, Gmail auto purges trash after 30 days.
print("Emptying Trash & Expunge...")
m.select('[Gmail]/Trash')  # select all trash
m.store("1:*", '+FLAGS', '\\Deleted')  #Flag all Trash as Deleted
m.expunge()  # not need if auto-expunge enabled

print("Done. Closing connection & logging out.")
m.close()
m.logout()
print "All Done."

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM