pileofrogs has asked for the wisdom of the Perl Monks concerning the following question:

I've got an IMAP mail account where I work and I keep bumping against the quota. I don't have an obvious way to find out what messages/folders are hogging all the space. I'm trying to write a perl script that will find out the size of all my messages and the total size consumed in each folder. If you know a slick way to solve my actual problem, please let me have it!

I've been working with Net::IMAP, but the documentation is a little vague and it's giving me trouble. Net::IMAP::Simple doesn't seem to have a function that will return message sizes. I haven't looked at any others. Let me know if you recommend a different module. Preferably one with examples in the documentation!

Right now I'm stuck trying to get a list of folders from Net::IMAP. I can connect to my IMAP server, authenticate, and run a "list" command, but the response is a plain "Net::IMAP::Response" object (saying "LIST completed.") not a "Net::IMAP::List" object and I can't see any way to get a "Net::IMAP::List object. So, if you've ever bumped heads with Net::IMAP, maybe you can help me with that.

Replies are listed 'Best First'.
Re: IMAP & File Sizes
by jettero (Monsignor) on Jan 06, 2007 at 01:17 UTC

    Yeah, the docs aren't that vague for Net::IMAP::Simple in my opinion. It says that $imap->list($number) returns the number of bytes. It'll also return all the sizes as an array @a = $imap->list if you call it without an argument.

    Normally I wouldn't write out the whole thing (as I think it spoils most of the fun), but in this case I actually wanted it for my own purposes and it's really just a cut and paste from the docs anyway. (spoilers within)

    -Paul

Re: IMAP & File Sizes
by madbombX (Hermit) on Jan 06, 2007 at 13:14 UTC
    Personally, I prefer Mail::IMAPClient. It obviously isn't necessary to delete the messages like I do below, but that's the snippet of code that I have in my script. Check out the doc to see the other functions available.
    use Mail::IMAPClient; my @folders = qw( INBOX saved-messages folder1 folder2 ); my $imap = Mail::IMAPClient->new( Server => 'mail.server.com:143', User => 'user', Password => 'pass') or die "IMAP failure: $@"; foreach my $folder (@folders) { $imap->select($folder) or die "Select failure: $!"; my @msgs = $imap->search('ALL') or die "Search error: $!"; foreach my $msg (@msgs) { if ($imap->size($msg) > 100000) { # 100k $imap->delete_message($msg); next; } } $imap->expunge($folder); $imap->close($folder); } $imap->logout();
Re: IMAP & File Sizes
by bad_till (Initiate) on Nov 07, 2007 at 16:58 UTC
    You need to define a callback
    sub listCallback { my ($imap, $list) = @_; print($list->mailbox(), "\n"); } my %options = (); my $imap = new Net::IMAP($host, %options); $imap->login($user, $pass); $imap->set_untagged_callback('list', \&listCallback); my $response = $imap->list('', 'INBOX.*'); # function listCallback prints mailbox names