I suppose I can share. You stick the following text in a file and run it in the python interpreter. Note that python cares about indentation, so if copy and paste from here doesn't work, I can be pursuaded to upload the file somewhere. With all of that setup, on Linux I just type ./spamdrain.py but YMMV.
Fill in your username, your password, and your domain as needed in the first couple of lines.
The code will skip mailboxes that are small (containing less than 30 messages) because they aren't a big deal and people who only get a couple of spams are more likely to have false positives in my experience. It is set to purge mail that is 7 days old. Both of these parameters is alterable.
# spamdrain -- a script that purges old e-mail in spam boxes
# given a maximum age and a threshold
# purgeimap was hacked to bits by Kevin Dwyer <firstname.lastname@example.org>
# to create spamdrain.
# purgeimap was written By Justin R. Miller <email@example.com>
import os, string, time, imaplib, sys
if __name__ == "__main__":
server = "mail.yourdomain.com"
port = 993
username = "youruserid"
password = "yourpassword"
directory = "yourdomain.com/"
folderMask = "*/spam"
age = 7 # days
purgeAmount = 30 # Only expunge a box if > purgeAmount
timestamp = time.localtime(time.time() - (age * 86400))
purgedate = time.strftime('%d-%b-%Y', timestamp)
print "Destroying spam older than %s at threshold %i" % (purgedate,
m = imaplib.IMAP4_SSL(server, port)
spamboxesResp = m.list(directory, folderMask)
spamboxes = map(lambda x:x.split(), spamboxesResp)
total = 0
totalOld = 0
totalExp = 0
for box in spamboxes:
print "Selecting %s..." % box,
numMsgs = m.select(box)
numMsgs = int(numMsgs)
print "%i messages." % numMsgs
typ, msgs = m.search(None, '(BEFORE ' + purgedate + ')')
if numMsgs < purgeAmount:
for num in string.split(msgs):
m.store(num, '+FLAGS', '(\Deleted)')
#print typ, msgs
totalOld += len(msgs.split())
total += numMsgs
expunged = m.expunge()
if expunged != [None]:
print "Expunged %s messages." % len(expunged)
totalExp += len(expunged)
if total > 0:
print "%i/%i (%.2f%%) spams over %i days old." \
% (totalOld, total, (totalOld*100.0)/total, age)
if totalOld > 0:
print "%i/%i (%.2f%%) spams expunged." % (totalExp, totalOld,