Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Check IMAP Folders for new messages

by davidrw (Prior)
on Apr 21, 2005 at 13:40 UTC ( [id://449976]=note: print w/replies, xml ) Need Help??


in reply to Check IMAP Folders for new messages

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 =====> sender@from.somewhere.com (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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://449976]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 13:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found