【问题标题】:Python imaplib deleting multiple emails gmailPython imaplib删除多个电子邮件gmail
【发布时间】:2017-01-04 22:05:52
【问题描述】:

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

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 或其他模块一次删除多封电子邮件的方法。我正在寻找最简单的例子。这个例子是在这个链接上给出的Using python imaplib to "delete" an email from Gmail?最后一个答案的例子。我工作不正常。但是,我可以让第一个示例在每次运行脚本时删除一封电子邮件。我宁愿尝试多次而不是运行脚本数千次。我的主要目标是通过 imaplib 删除多封电子邮件,任何解决方法或其他工作模块或示例将不胜感激。

【问题讨论】:

  • “字符串”打印出来的是什么?
  • 你的逗号后面好像有一个空格。 UID 列表不能有空格。
  • String 以字符串形式打印出一个列表,例如 '1, 2, 3' 我也取出了空间,并且出现了 '1,2,3' 同样的问题。

标签: python email imaplib uid


【解决方案1】:

您可能会发现使用 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 的作者和维护者。

【讨论】:

  • 它返回了 imaplib.abort: socket error: EOF error
  • 抱歉,示例中创建 IMAPClient 实例的行是错误的。我已经修复了它们(注意“ssl=True”而不是端口号)
  • 这次它返回 OpenSSL.SSL.Error: []
【解决方案2】:

最初的帖子:

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

【讨论】:

    猜你喜欢
    • 2017-05-14
    • 2019-08-26
    • 2011-03-02
    • 2010-12-19
    • 2012-12-11
    • 2011-04-28
    • 2017-04-04
    • 2011-04-01
    • 1970-01-01
    相关资源
    最近更新 更多