#!/usr/bin/python # Run this in the root of the IMAIL folder (or where the domain directories are) import os import email import mailbox outputdir = '..' excludeusers = ('root','temp') def convertdir(arg, dirname, names): parts = dirname.split('/') # Domain user directories have 4 parts if (len(parts) == 4): if (parts[2].lower() == 'users'): domain = parts[1].replace('_','.').replace('mail.','') username = parts[3] if username not in excludeusers: e = username + '@' + domain print("Creating " + e) rootmaildir = mailbox.Maildir(outputdir + "/" + e) for f in os.listdir(dirname): if f.endswith('.mbx'): boxname = f.replace('.mbx','') print('\tLocated mailbox: %s' % (f,)) mbox = mailbox.mbox(dirname + '/' + f) maildir = mailbox.Maildir(outputdir + '/' + e + '/.' + boxname) converttomaildir(mbox,maildir) maildir.close() rootmaildir.close() def converttomaildir(oldmbox, newmaildir): print("\tConverting " + str(len(oldmbox)) + " emails.") for m in oldmbox.values(): newmaildir.add(m) newmaildir.flush() os.path.walk('.', convertdir, None)