large mailbox -- best practices

Daniel O'Connor doconnor at gsoft.com.au
Mon Apr 8 21:57:26 EDT 2013


On 05/04/2013, at 22:08, mayak-cq <mayak at australsat.com> wrote:
> it works -- mail consulted from sogo web mail client and i am running 2.4.17 on x64, but i am wondering what is the best way to split this up, or, move it to a new directory and start a new archive ...

I wrote a script which moves messages into a new subfolder each month for this sort of account.#!/usr/local/bin/python

import imaplib, datetime, re

user = 'user'
password = 'password'
host = 'mail.foo.com'

def movemail(imap, base, fake = False):
    stat, msg = imap.select(base)
    assert(stat == 'OK')

    now = datetime.datetime.utcnow()
    lstMnthSt = datetime.date(now.year, now.month, 1)
    if (now.month != 1):
        lstMnthEn = datetime.date(now.year, now.month - 1, 1)
    else:
        lstMnthEn = datetime.date(now.year - 1, 12, 1)

    qry = ["SENTSINCE", "%02d-%s-%04d" % (lstMnthEn.day, lstMnthEn.strftime("%b"), lstMnthEn.year)]
    qry.extend(["SENTBEFORE", "%02d-%s-%04d" % (lstMnthSt.day, lstMnthSt.strftime("%b"), lstMnthSt.year)])
    stat, msg = imap.search(None, *qry)
    assert(stat == 'OK')
    msgcount = len(msg[0].split())
    msglist = msg[0]
    
    foldername = "%s.%04d-%02d-%02d" % (base, lstMnthEn.year, lstMnthEn.month, lstMnthEn.day)
    print "Copying to " + foldername
    stat, msg = imap.create(foldername)
    assert(stat == 'OK' or msg[0] == 'Mailbox already exists')
    stat, msg = imap.subscribe(foldername)
    assert(stat == 'OK')
    
    if (not fake):
        # Annoyingly imaplib does not take into account any line
        # length limits in the server so we have to manually split
        # everything up..
        pymsglist  = msglist.split()
        pidx = 0
        while (pidx < len(pymsglist)):
            imaplist = reduce(lambda a,  b: a + ',' + b, pymsglist[pidx:pidx + 100])

            stat, msg = imap.copy(imaplist, foldername)
            assert(stat == 'OK')
            stat, msg = imap.store(imaplist, '+FLAGS', '\\Deleted')
            assert(stat == 'OK')
            pidx = pidx + 100
            print "Copied " + imaplist
            
        stat, msg = imap.select(base)
        assert(stat == 'OK')
        stat, msg = imap.expunge()
        assert(stat == 'OK')
    else:
        print "Would move & delete %d messages" % (msgcount)
        splitlist = msglist.split()
        abbrevlist = splitlist[:10]
        abbrevlist.extend(splitlist[-10:])
        for m in abbrevlist:
            stat, msg = imap.fetch(m, '(ENVELOPE)')
            print "%s -> %s" % (m, re.findall('[0-9]+ [a-zA-Z]+ [0-9]+ ..:..:.. \+....', msg[0]))

def init():
    imap = imaplib.IMAP4_SSL(host)
    stat, msg = imap.login(user, password)
    assert(stat == 'OK')
    return(imap)

    
imap = init()
movemail(imap, 'INBOX.folder1', fake = False)
movemail(imap, 'INBOX.folder2', fake = False)

--
Daniel O'Connor software and network engineer
for Genesis Software - http://www.gsoft.com.au
"The nice thing about standards is that there
are so many of them to choose from."
  -- Andrew Tanenbaum
GPG Fingerprint - 5596 B766 97C0 0E94 4347 295E E593 DC20 7B3F CE8C






-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4358 bytes
Desc: not available
Url : http://lists.andrew.cmu.edu/pipermail/info-cyrus/attachments/20130409/ff56a966/attachment.bin 


More information about the Info-cyrus mailing list