Python imaplib deleting multiple emails gmail
<p>my code look like this...</p>

<pre><code>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 '&lt;sender's email address&gt;' 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()
</code></pre>

<p>the error I get is</p>

<pre><code>Traceback (most recent call last):
File "del_email.py", line 31, in &lt;module&gt;
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']
</code></pre>

<p>I am looking for a way to delete multiple emails at once using imaplib or other module. I am looking for the simplest example to go off of. This example was given at this link here <a href="http://stackoverflow.com/questions/1777264/using-python-imaplib-to-delete-an-email-from-gmail">Using python imaplib to &quot;delete&quot; an email from Gmail?</a> the last answer's example. I'ts not working correctly. I can however get the the 1st example to work to delete one email every time the script is ran. I'd rather try the doing it with a multiple than running the script several thousand times. my main goal is to delete multiple emails through imaplib any workarounds or other working modules or examples would be appreciated. </p>

<p>You might find this a bit easier using <a href="https://pypi.python.org/pypi/IMAPClient/1.0.2" rel="nofollow">IMAPClient</a> as it takes care of a lot more of low level protocol aspects for you.</p>

<p>Using IMAPClient your code would look something like:</p>

<pre><code>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 '&lt;senders email address&gt;' in msg[header]:
            delete.append(x)

obj.delete_messages(delete)
obj.expunge()
obj.close()
obj.logout()
</code></pre>

<p>This could be made more efficient by fetching multiple messages in a single fetch() call rather than fetching them one at a time but I've left that out for clarity.</p>

<p>If you're just wanting to filter by the sender's address you can get the IMAP server to do the filtering for you. This avoids the need to download the message bodies and makes the process a whole lot faster.</p>

<p>This would look like:</p>

<pre><code>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', '&lt;senders email address&gt;'))
obj.delete_messages(msg_ids)
obj.expunge()
obj.close()
obj.logout()
</code></pre>

<p>Disclaimer: I'm the author and maintainer of IMAPClient.</p>

python email imaplib uid