#!/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; }

In reply to Check IMAP Folders for new messages by chazzz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.