使用imaplib删除gmail中的电子邮件时出现问题

2024-05-11 22:49:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从收件箱文件夹中删除邮件,一切正常,但当我切换到所有邮件文件夹时,删除将不起作用。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])

Tags: in文件夹comnoneforserver邮件ok
3条回答

正如它在gmail blog site上所说,谷歌对IMAP的实现有点不同。当你按照说明去获取更常见的语义时,它有帮助吗?

There are also some more obscure options for those of you who want to make Gmail's IMAP work more like traditional IMAP providers: you can turn off auto-expunge or trash messages when they're no longer visible through IMAP.

The IMAP protocol allows messages to be marked for deletion, a sort of limbo state where a message is still present in the folder but slated to be deleted the next time the folder is expunged. In our standard IMAP implementation, when you mark a message as deleted, Gmail doesn't let it linger in that state -- it deletes (or auto-expunges) it from the folder right away. If you want the two-stage delete process, after you've enabled this Lab, just select 'Do not automatically expunge messages' under the 'Forwarding and POP/IMAP' tab in Settings.

Similarly, most IMAP systems don't share Gmail's concept of archiving messages (sending messages to the [Gmail]/All Mail folder rather than [Gmail]/Trash). If you'd prefer that deleted messages not remaining in any other visible IMAP folders are sent to [Gmail]/Trash instead, Advanced IMAP Controls lets you set your preferences this way. In the 'IMAP Access:' section of the 'Forwarding and POP/IMAP' tab, find the 'When a message is deleted from the last visible IMAP folder:' option. Select 'Move the message to the Gmail Trash.' If you want to take it one step further, you can select 'Immediately delete the message forever.'

使用Gmail advanced IMAP controls可以设置通过IMAP删除邮件时对邮件的处理方式。

只需在Gmail实验室中启用“高级IMAP控件”。“设置”页将如下所示:

settings

然后,当您根据this answer将邮件标记为“已删除”和“已删除”时,根据您选择的设置,该邮件将被移动到收件箱、永久删除或存档到“所有邮件”。

它会将给定gmail标签中的所有邮件移动到gmail垃圾箱中

#!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

记住刷新你的gmail界面,因为它有缓存

相关问题 更多 >