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.
#!/usr/bin/python
#
# 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 <kevin@pheared.net>
# to create spamdrain.
# purgeimap was written By Justin R. Miller <justin@solidlinux.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,
purgeAmount)
m = imaplib.IMAP4_SSL(server, port)
m.login(username, password)
spamboxesResp = m.list(directory, folderMask)
spamboxes = map(lambda x:x.split()[3], spamboxesResp[1])
#print spamboxes
total = 0
totalOld = 0
totalExp = 0
for box in spamboxes:
print "Selecting %s..." % box,
numMsgs = m.select(box)
numMsgs = int(numMsgs[1][0])
print "%i messages." % numMsgs
typ, msgs = m.search(None, '(BEFORE ' + purgedate + ')')
if numMsgs < purgeAmount:
print "Skipping."
continue
for num in string.split(msgs[0]):
m.store(num, '+FLAGS', '(\Deleted)')
#print typ, msgs
totalOld += len(msgs[0].split())
total += numMsgs
expunged = m.expunge()
if expunged[1] != [None]:
print "Expunged %s messages." % len(expunged[1])
totalExp += len(expunged[1])
m.logout()
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,
(totalExp*100.0)/totalOld)