繁体   English   中英

Python imaplib删除多个电子邮件gmail

[英]Python imaplib deleting multiple emails gmail

我的代码看起来像这样...

import imaplib
import email
obj = imaplib.IMAP4_SSL('imap.gmail.com','993')
obj.login('user','pass')
obj.select('inbox')
delete = []

for i in range(1, 10):
  typ, msg_data = obj.fetch(str(i), '(RFC822)')
  print i
  x = i
  for response_part in msg_data:
    if isinstance(response_part, tuple):
        msg = email.message_from_string(response_part[1])
        for header in [ 'subject', 'to', 'from', 'Received' ]:
            print '%-8s: %s' % (header.upper(), msg[header])
            if header == 'from' and '<sender's email address>' in msg[header]:
                delete.append(x)
string = str(delete[0])
for xx in delete:
    if xx != delete[0]:
        print xx
        string = string + ', '+ str(xx)
print string
obj.select('inbox')
obj.uid('STORE', string , '+FLAGS', '(\Deleted)')
obj.expunge()
obj.close()
obj.logout()

我得到的错误是

Traceback (most recent call last):
File "del_email.py", line 31, in <module>
obj.uid('STORE', string , '+FLAGS', '(\Deleted)')
File "C:\Tools\Python(x86)\Python27\lib\imaplib.py", line 773, in uid
typ, dat = self._simple_command(name, command, *args)
File "C:\Tools\Python(x86)\Python27\lib\imaplib.py", line 1088, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "C:\Tools\Python(x86)\Python27\lib\imaplib.py", line 918, in _command_complete
raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.error: UID command error: BAD ['Could not parse command']

我正在寻找一种使用imaplib或其他模块一次删除多封电子邮件的方法。 我正在寻找最简单的例子。 此示例在此处的链接处给出。 使用python imaplib从Gmail“删除”电子邮件吗? 最后一个答案的例子。 我无法正常工作。 但是,我可以得到第一个示例,该示例可以在每次运行脚本时删除一封电子邮件。 我宁愿尝试使用倍数执行此操作,也不愿运行脚本数千次。 我的主要目标是通过imaplib删除多封电子邮件,任何变通方法或其他工作模块或示例将不胜感激。

您可能会发现使用IMAPClient会容易一些,因为它为您解决了许多底层协议方面的问题。

使用IMAPClient,您的代码将类似于:

from imapclient import IMAPClient
import email

obj = IMAPClient('imap.gmail.com', ssl=True)
obj.login('user','pass')
obj.select('inbox')

delete = []
msg_ids = obj.search(('NOT', 'DELETED'))
for msg_id in msg_ids:
    msg_data = obj.fetch(msg_id, ('RFC822',))
    msg = email.message_from_string(msg_data[msg_id]['RFC822'])
    for header in [ 'subject', 'to', 'from', 'Received' ]:
        print '%-8s: %s' % (header.upper(), msg[header])
        if header == 'from' and '<senders email address>' in msg[header]:
            delete.append(x)

obj.delete_messages(delete)
obj.expunge()
obj.close()
obj.logout()

通过在单个fetch()调用中获取多个消息,而不是一次获取一个消息,可以使效率更高,但是为了清楚起见,我将其省略。

如果您只想按发件人的地址进行过滤,则可以让IMAP服务器为您进行过滤。 这避免了下载消息正文的需要,并使整个过程更快。

看起来像:

from imapclient import IMAPClient

obj = IMAPClient('imap.gmail.com', ssl=True)
obj.login('user','pass')
obj.select('inbox')
msg_ids = obj.search(('NOT', 'DELETED', 'FROM', '<senders email address>'))
obj.delete_messages(msg_ids)
obj.expunge()
obj.close()
obj.logout()

免责声明:我是IMAPClient的作者和维护者。

初始职位:

SyntaxError: '<sender's email address>'
# did you mean :
"<sender's email address>"

暂无
暂无

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

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