Skip to content

Email Deluge…

The other day I recieved about 250,000 emails from a script I wrote.. I was in a hurry and didn’t properly log the output like I should of, or limit by quantity, or use any one of the 1000 better methods in my app. I simply sent out an email when it hit an error at a certain point to let me know.

This has worked fine for some time, but an external team is responsible for writing plugins to the application, and their plugin hit a service that was throwing an error..
so yeah.. IMAP4 with 250K emails to download and delete. Thunderbird croaked under the pressure.
I fired up an instance of Roundcube which is a web-based client, and in my experience performs REALLY well under large email load hoping I could take care of it, but I ran into an unfortunate fact that I could only delete groups of 100 emails or so with it.
So yeah – that won’t work.
After googling around for a bit, I ran across an old post on http://lukecyca.com/ that had a nice Python recipe to solve my problem. Observe the power of Python:
import imaplib
from getpass import getpass

imap = imaplib.IMAP4_SSL('mail.example.com')
imap.login('luke',getpass())

imap.select()
typ, data = imap.search(None, '(FROM "zabbix@example.com")')
for num in data[0].split():
    imap.store(num, '+FLAGS', '\Deleted')

imap.expunge()
imap.close()
imap.logout()

This obviously filters by 'From' address, but you can easily filter by other stuff. Look here for more info.

This took a while, but it was automatic and saved me a lot of time. Thought I'd pass it along.
Like or share:
Published inSystem Admin

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *