http://www.perlmonks.org?node_id=546613

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

I'm trying to write a fairly simple IMAP "polling" utility. The script iterates through the specified IMAP mail folders and displays the number of unread messages over the total number of messages. I'm using Net::IMAP::Simple::SSL since SSL access is imperative in my environment. Here's the snippet I use to get the number of "new" messages:
for (my $i = 1; $i <= $tmessages; $i++) { if (!$imap->seen($i)) { $nmessages++; } }
$tmessages, as you can probably guess, is the total number of messages. This works really well with small folders, even foldes with a few hundred messages, but once you hit the thousands it begins to take a fair amount of time. I looked at Mail::IMAPClient, and the 'unseen_count' method looks like it could be useful. However, IMAPClient doesn't appear to have any SSL support "out of the box." I have to admit, my knowledge is severely lacking when it comes to SSL communication in Perl. I tried this snippet, and it complained extensively:
use IO::Socket::SSL; use Mail::IMAPClient; $socket = new IO::Socket::SSL("my.mailserver:imaps"); $imap = Mail::IMAPClient->new(Socket => $socket, User => $user, Password => $pass,) or die "Couldn't connect to IMAP server $host as $user: $@\n"; if (!$imap->login) { die "Couldn't authenticate: $@\n"; }
If someone could point me at a more efficient way of getting the unread messages count, or working with IMAP SSL in Mail::IMAPClient, it'd be immensely appreciated! Thanks!