http://qs321.pair.com?node_id=449964
Category: E-Mail Programs
Author/Contact Info Chris Hills (chills@ne-worcs.ac.uk)
Description: This script displays the number of new, and total messages, in each folder of an IMAP account. It currently has the following limitations:-
  • The folder separator is hard-coded with split()
  • Not as fast as it could be, as I could find no way to fetch just the new message count for a folder (with Net::IMAP::Simple). Instead it checks the status of each and every message.
  • Currently no support for SSL
    #!/usr/bin/perl
    
    use strict;
    use Net::IMAP::Simple;
    
    # This is the folder separator your IMAP server uses.
    # NOTE: Not used yet.
    my $separator = ".";
    
    # IMAP Account Details
    my %servers = (
          'imap.server.1' => [ 'username' , 'password' ] ,
    #     'imap.server.2' => [ 'username' , 'password' ]
    #     ...
    );
    
    my $mailbox_name;
    my $new_messages;
    my $total_messages;
    
    # Define the format rules
    
    format Mailboxes_Top =
    
    Mailbox                                      New      Total
    -------------------------------------------- -------- --------
    .
    
    format Mailboxes_Footer =
    -------------------------------------------- -------- --------
    Total                                        @<<<<<<< @<<<<<<<
    $new_messages $total_messages
    -------------------------------------------- -------- --------
    .
    
    format Mailboxes =
    @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<< @<<<<<<<
    $mailbox_name $new_messages $total_messages
    .
    
    # For each server...
    foreach my $s (keys %servers){
    
            # Fetch the username
            my $u = @{$servers{$s}}[0];
    
            # Fetch the password
            my $p = @{$servers{$s}}[1];
    
            # Output the heading
            $~ = 'Mailboxes_Top'; write;
    
            # Open a connection to the IMAP server
            my $server = new Net::IMAP::Simple( $s );
            $server->login ( $u , $p );
    
            # Get a list of all the mailboxes
            my @folders = $server->mailboxes();
    
            # Reset the message counters
            my $total_account_messages = 0;
            my $total_new_messages = 0;
    
            # For each folder on the server...
            foreach(sort @folders){
    
                    # Select the folder and get the number of messages
                    my $folder_messages = $server->select($_);
    
                    # Increment the total message counter
                    $total_account_messages += $folder_messages;
    
                    # For each message in the folder check if it is new
                    my $folder_new_messages = 0;
                    foreach my $msg (1 .. $folder_messages){
                            $folder_new_messages++ if (!$server->seen($msg
    +));
                    };
    
                    # Increment the total new message counter
                    $total_new_messages += $folder_new_messages;
    
                    # Prepare the folder name for pretty output
                    my $title = $_;
                    my @tmp = split(/\./, $title);
                    my $start = "    " x $#tmp;
                    $title = $start . pop(@tmp);
    
                    # Output the current folder details
                    $~ = 'Mailboxes';
                    $mailbox_name = $title;
                    $new_messages = $folder_new_messages;
                    $total_messages = $folder_messages;
                    write;
    
            }
    
            # Output the footer
            $~ = 'Mailboxes_Footer';
            $new_messages = $total_new_messages;
            $total_messages = $total_account_messages;
            write;
    
    }