Python imaplib deleting multiple emails gmail -
my code this...
import imaplib import email obj = imaplib.imap4_ssl('imap.gmail.com','993') obj.login('user','pass') obj.select('inbox') delete = [] in range(1, 10): typ, msg_data = obj.fetch(str(i), '(rfc822)') print x = response_part in msg_data: if isinstance(response_part, tuple): msg = email.message_from_string(response_part[1]) header in [ 'subject', 'to', 'from', 'received' ]: print '%-8s: %s' % (header.upper(), msg[header]) if header == 'from' , '<sender's email address>' in msg[header]: delete.append(x) string = str(delete[0]) 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()
the error is
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']
i looking way delete multiple emails @ once using imaplib or other module. looking simplest example go off of. example given @ link here using python imaplib "delete" email gmail? last answer's example. i'ts not working correctly. can the 1st example work delete 1 email every time script ran. i'd rather try doing multiple running script several thousand times. main goal delete multiple emails through imaplib workarounds or other working modules or examples appreciated.
you might find bit easier using imapclient takes care of lot more of low level protocol aspects you.
using imapclient code like:
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')) msg_id in msg_ids: msg_data = obj.fetch(msg_id, ('rfc822',)) msg = email.message_from_string(msg_data[msg_id]['rfc822']) header in [ 'subject', 'to', 'from', 'received' ]: print '%-8s: %s' % (header.upper(), msg[header]) if header == 'from' , '<senders email address>' in msg[header]: delete.append(x) obj.delete_messages(delete) obj.expunge() obj.close() obj.logout()
this made more efficient fetching multiple messages in single fetch() call rather fetching them 1 @ time i've left out clarity.
if you're wanting filter sender's address can imap server filtering you. avoids need download message bodies , makes process whole lot faster.
this like:
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()
disclaimer: i'm author , maintainer of imapclient.
Comments
Post a Comment