IMAP - get size of mailboxes

F

foten

Hi guys,
I'm trying to make a script (or similar) to display and print the
folder size information for my outlook mailbox.
(So nothing for exchange administrators, just for my own mailbox
that keeps bouncing against the 45 MB limit ...)

The standard way (right-click on Outlook Today, Properties:Folder Size...)
has a
number of deficiencies:
- the window is fixed size, so you can either see the full folder name or
the size
- can't sort by size,
- can't print

So I came up with the folloing approach:
Please correct any flaws you see, since I'm new into python.
The problem (except for bad coding) is that I can't think of a way to get
the size of the mailbox.

Ideas any one?

#! /usr/local/util/python/2.2.2/bin/python
import sys
import os
import string
import imaplib
import getpass

user = os.environ['USER']
imap_server = "xxx-yyy.com"

# Open a connection to the IMAP server
M = imaplib.IMAP4(imap_server)
M.login(getpass.getuser(), getpass.getpass())
result,list = M.list()

print "%-30s%5s%10s\n" % ("Folder", "no Msg", "Result")

for item in list[:]:
x = item.split()
mailbox = string.join(x[2:])
result, size = M.select(mailbox,readonly=1)
print "%-30s%5d%10s" % (mailbox, int(size[0]), result);

M.logout()
 
F

foten

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
 
J

Jeff Epler

I played with imaplib a bit, and I think you want to use something like
# Find the first and last messages
m = [int(x) for x in msg[0].split()]
m.sort()
message_set = "%d:%d" % (m[0], m[-1])

# Get the RFC822.SIZE for each message
result, sizes_response = M.fetch(message_set, "(UID RFC822.SIZE)")
.. and parse sizes_response which looks like this:
>>> for i in range(10): print sizes_response

...
1 (UID 1 RFC822.SIZE 889)
2 (UID 3 RFC822.SIZE 3386)
3 (UID 4 RFC822.SIZE 2629)
4 (UID 5 RFC822.SIZE 989)
5 (UID 6 RFC822.SIZE 1566)
6 (UID 7 RFC822.SIZE 1591)
7 (UID 8 RFC822.SIZE 1894)
8 (UID 9 RFC822.SIZE 1546)
9 (UID 10 RFC822.SIZE 1372)
10 (UID 11 RFC822.SIZE 917)

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFBRcdyJd01MZaTXX0RAjygAJ9a4/8QPwhyBhyFCyg6qWivmI2s/ACffoHN
yf6vWK+u7Hp7RQhm3dUDH3o=
=YQ8l
-----END PGP SIGNATURE-----
 
F

foten

Jeff Epler said:
I played with imaplib a bit, and I think you want to use something like
# Find the first and last messages
m = [int(x) for x in msg[0].split()]
m.sort()
message set = "%d:%d" % (m[0], m[-1])

# Get the RFC822.SIZE for each message
result, sizes response = M.fetch(message set, "(UID RFC822.SIZE)")
.. and parse sizes response which looks like this:
for i in range(10): print sizes response

...
1 (UID 1 RFC822.SIZE 889)
2 (UID 3 RFC822.SIZE 3386)
3 (UID 4 RFC822.SIZE 2629)
4 (UID 5 RFC822.SIZE 989)
5 (UID 6 RFC822.SIZE 1566)
6 (UID 7 RFC822.SIZE 1591)
7 (UID 8 RFC822.SIZE 1894)
8 (UID 9 RFC822.SIZE 1546)
9 (UID 10 RFC822.SIZE 1372)
10 (UID 11 RFC822.SIZE 917)

Jeff

--


Thanx for your help! I think I have it working now. The speed is not bad at all.

//Fredrik

import sys, os, string, imaplib, getpass

imap_server = "myserver.com"

# Open a connection to the IMAP server
M = imaplib.IMAP4(imap_server)
M.login(getpass.getuser(), getpass.getpass())

# The list of all folders
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()
mailbox = string.join(x[2:])

# Select the desired folder
result, number_of_messages = M.select(mailbox, readonly=1)
number_of_messages_all += int(number_of_messages[0])

size_folder = 0
# Go through all the messages in the selected folder
typ, msg = M.search(None, 'ALL')
# Find the first and last messages
m = [int(x) for x in msg[0].split()]
m.sort()
if m:
message_set = "%d:%d" % (m[0], m[-1])
result, sizes_response = M.fetch(message_set, "(UID RFC822.SIZE)")
for i in range(m[-1]):
tmp = sizes_response.split()
size_folder += int(tmp[-1].replace(')', ''))
else:
size_folder = 0
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)

# Close the connection
M.logout()



Result:
Folder # Msg Size

Outbox 0 0
Journal 0 0
Drafts 10 54245
Notes 20 13265
PocketMirror 1 500
"Sent Items" 342 7054846
....
Sum 3115 43.341 MB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top