Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Check IMAP Folders for new messages

by chazzz (Pilgrim)
on Apr 21, 2005 at 12:56 UTC ( #449964=sourcecode: print w/replies, xml ) Need Help??
Category: E-Mail Programs
Author/Contact Info Chris Hills (
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
    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;
            # Output the footer
            $~ = 'Mailboxes_Footer';
            $new_messages = $total_new_messages;
            $total_messages = $total_account_messages;
    Replies are listed 'Best First'.
    Re: Check IMAP Folders for new messages
    by davidrw (Prior) on Apr 21, 2005 at 13:40 UTC
      I wrote a similar scriptusing Mail::IMAPClient, which I've liked a lot and found really easy to use and well doc'd.

      Looks like (w/o actually trying it) adding SSL support would be trivial with Net::IMAP::Simple::SSL.

      Here's my script (just a warning this is a quick & dirty solution, but suits my purposes) that i call 'nfrm' (i used to use that elm command all the time when i used pine) -- called by itself (no params), it will list new messages; called with a message index, it will print that message to the screen:
      [david@host david]$ nfrm 1 [INBOX] This is the Subject [david@host david]$ nfrm 1 1 [INBOX] This is the Subject =====> (Some Guy) This is the message text ...

      #!/usr/bin/perl my $N = $ARGV[0] || 0; use Mail::IMAPClient; use strict; my ($host, $id, $pass) = qw( YOURMAIL.HOSt.COM YOURNAME YOURPW ); my $imap = Mail::IMAPClient->new( Server => $host, User => $id, Password=> $pass, ) or die "Cannot connect to $host as $id: $@"; my $ct = 0; my @allMsgs; foreach my $folder ( $imap->folders ){ $imap->select($folder); $imap->Peek(1); my @msgs = grep $_, $imap->unseen(); next unless scalar @msgs; push @allMsgs, { folder => $folder, msgs => \@msgs }; $ct += scalar (@msgs); } foreach my $h (@allMsgs){ my $folder = $h->{folder}; my @msgs = @{$h->{msgs}}; $imap->select($folder); $imap->Peek(1); foreach my $msgId ( reverse @msgs ){ printf "%-2d [%s] %s\n", $ct, $folder, $imap->subject($msgId) unle +ss $N && $ct != $N; printf "=====> %s\n%s", $imap->get_header($msgId, 'From'), $imap-> +body_string($msgId) if $ct == $N; $ct--; } }
    Log In?

    What's my password?
    Create A New User
    Node Status?
    node history
    Node Type: sourcecode [id://449964]
    and a soft breeze sighs...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (4)
    As of 2018-06-19 00:14 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (111 votes). Check out past polls.