Skip to content

Instantly share code, notes, and snippets.

@waffle2k
Created November 15, 2010 16:50
Show Gist options
  • Save waffle2k/700584 to your computer and use it in GitHub Desktop.
Save waffle2k/700584 to your computer and use it in GitHub Desktop.
expunge_email.py
#!/usr/bin/python
import getpass, imaplib, time
import create_sub
import sys
lt = time.localtime()
sztime = time.strftime('%Y.%m.%d')
class IMAPReporting:
'''
This class provides the ability to pull a given archive from the
shared imap folder and create a tarball with all messages
to be delivered to mailshell.
'''
def __init__(self, un, pw, host='IMAP.HOST.GOES.HERE' ):
self.M = imaplib.IMAP4( host )
self.M.login( un, pw )
def select(self, folder ):
self.M.select( folder )
def dump( M, mbox):
M.select( mbox )
typ,data = M.search(None, 'ALL')
for num in data[0].split():
typ, data = M.fetch(num, '(RFC822)')
fname = mbox + "."+str(num)
print 'dumping ' + fname
fd = open( fname, "w" )
fd.write( data[0][1])
print 'logging into YOURHOSTNAMEHERE'
M = imaplib.IMAP4('NAME.OF.YOUR.IMAP.HOSE')
M.login('ACCOUNT.NAME', 'ACCOUNT.PASSWORD')
#
# Select a given IMAP folder.. I'll keep my spam as an
# example
print 'opening INBOX.spam'
ret = M.select("INBOX.spam")
print M.list()[1]
typ, data = M.search(None, 'ALL')
print "data is ",data
# Delete all of that good stuff
for num in data[0].split():
print num,ret
ok,err = M.store(num, 'FLAGS', '(\Deleted)')
print (ok,err)
modu = int( num ) % 100
if modu == 0:
print 'Expunging...'
M.expunge()
M.close()
M.logout()
sys.exit(0)
print 'Expunging...'
M.expunge()
M.close()
M.logout()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment