IMAP - get size of mailboxes

foten valinor at linuxmail.org
Mon Sep 13 11:07:07 EDT 2004


valinor at linuxmail.org (foten) wrote in message news:<cf4db78a.0409100420.58efa003 at posting.google.com>...
> Hi guys,
> I'm trying to make a script (or similar) to display and print the
> folder size information for my outlook mailbox.
--snip

Well, the scipt below seems to be working. Although, it's unbelivable slow!
A better way has to exist, not reading every single message from the server
to calculate the size.

import sys
import os
import string
import imaplib
import getpass

imap_server = "myserver.com"

M = imaplib.IMAP4(imap_server)
M.login(getpass.getuser(), getpass.getpass())

result,list = M.list()

print "%-30s%5s%10s\n" % ("Folder", "# Msg", "Size")

number_of_messages_all = 0
size_all = 0

for item in list[:]:
    x = item.split()              # Don't like this part
    mailbox = string.join(x[2:])  # or this...

    result, number_of_messages  = M.select(mailbox,readonly=1)
    number_of_messages_all += int(number_of_messages[0])

    size_folder = 0
    typ, msg = M.search(None, 'ALL')
    for num in msg[0].split():
        typ, message = M.fetch(num, '(RFC822)')
        size_folder += len(message[0][1])
    print "%-30s%5d%10s" % (mailbox, int(number_of_messages[0]), size_folder);
    size_all += size_folder


print "\n%-30s%5i%10.3f MB\n" % ("Sum", number_of_messages_all, size_all/1e6)
M.logout()



Result:

Folder                        # Msg      Size

Outbox                            0         0
Journal                           0         0
Drafts                           10     54245
Notes                            20     13265
PocketMirror                      1       500
"Test/Mari Nilsson"              23    156401
...

//Fredrik



More information about the Python-list mailing list