IMAP Folders

A Cal Henderson Thingy
April 13th, 2010

The problem

I host my email at Tuffmail, who are pretty awesome. Not having to worry about backups and spam and so on is quite lovely. If you use GMail, good for you - I like folders and have a very large mail archive that goes back over 10 years, so GMail is not for me.

One issue is that Tuffmail uses IMAP quotas to limit your inbox size. This is perfectly reasonable, but with many folders, it's hard to know where your quota is being used. So in steps Perl.

My Solution

After installing the excellent Mail::IMAPClient, I whipped up this quick script:


use Mail::IMAPClient;

my $imap = Mail::IMAPClient->new(
	Server   => 'mail.mxes.net',
	User     => '**********',
	Password => '**********',
	Ssl      => 0,
	Uid      => 1,

my $folders = $imap->folders or die "Failed to get folders\n";
my $sizes = {};

foreach my $folder (@{$folders}){
	$imap->examine($folder) or next;

	$sizes->{$folder} = 0;

	my $hash = $imap->fetch_hash("RFC822.SIZE");
	foreach my $msg (keys %{$hash}){
		$sizes->{$folder} += $hash->{$msg}->{'RFC822.SIZE'};

my @keys = sort { $sizes->{$b} <=> $sizes->{$a} } @{$folders};

print "Only showing folders over 1MB:\n";

for my $folder (@keys){

	my $size = $sizes->{$folder};
	if ($sizes->{$folder} > 1024){ $size = int($sizes->{$folder} / 1024) . "KB"; }
	if ($sizes->{$folder} > 1024 * 1024){
		$size = int($sizes->{$folder} / (1024 * 1024)) . "MB";

	next unless $size =~ /MB/;

	print "$size\t$folder\n";

After adding in my own account details, running it gives what I need:

[cal@mt1 ~]# perl imap_folders.pl
Only showing folders over 1MB:
108MB   lists/tinyspeck
71MB    lists/tinyspeck/svn
20MB    lists/tinyspeck/jobs
15MB    lists/colourlovers
11MB    lists/everyone
10MB    lists/jobs
9MB     lists/b4ta
5MB     friends/dnd
5MB     friends
4MB     ecom
4MB     Sent
4MB     lists/software
3MB     ecom/travel
3MB     lists/hackers
2MB     lists/houses-bridgeview
2MB     lists/oembed
1MB     lists/conferences
1MB     lists/tax
1MB     Discard
1MB     Sent Items
1MB     lists/books

There's some cruft in the code, but it's short and simple enough to easily refactor. Hope you find it useful.


Written by Cal Henderson, inspired by a post in some Java forum somewhere.