Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

More efficient way to get unseen IMAP messages

by captkirk (Acolyte)
on Apr 30, 2006 at 23:37 UTC ( #546613=perlquestion: print w/replies, xml ) Need Help??

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!

Replies are listed 'Best First'.
Re: More efficient way to get unseen IMAP messages
by mda2 (Hermit) on May 01, 2006 at 02:49 UTC
    The more eficient is use of STATUS command of imap protocol. But itīs not available on Net::IMAP::Simple...

    Can you implement a hack todo it.

    1. Examing the protocol:

    2. Understanding module struture (and how Net::IMAP implements):

    3. Suggested hack:

    4. Resume:
    Examine the module options on code and protocol to improve your process ;)

    on time Mail::IMAPClient suggest from davidrw implements many others functions...

    --
    Marco Antonio
    Rio-PM

Re: More efficient way to get unseen IMAP messages
by davidrw (Prior) on May 01, 2006 at 02:11 UTC
    where does $tmessages come from? You can just use Mail::IMAPClient's unseen method:
    my @unread = $imap->unseen or warn "Could not find unseen msgs: $@\n";
    Also from the docs:
    foreach my $f ($imap->folders) { print "The $f folder has ", $imap->unseen_count($f)||0, " unseen messages.\n"; }
    I don't know if these are more efficient runtime-wise, but certainly make the code cleaner..

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://546613]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2020-01-22 13:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?