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

I'm trying to build a simple imap mail reader, but for some reason, I cannot get the $imap->messages method to work. Any help would be greatly appreciated. And Happy New Year!

Here is the code (with username, password and host changed):

use strict; use Mail::IMAPClient; use Data::Dumper; use IO::File; my $imap = Mail::IMAPClient->new( Server => "some.secure.imap.server", User => "someuser", Password => "somepassword", debug => 1, ) or die "Cannot connect: $@"; my $folder = "Inbox"; $imap->exists($folder) or warn "$folder not found: $@\n"; my $msgcount = $imap->message_count($folder); defined($msgcount) or die "Could not message_count: $@\n"; print "msg count = ", $msgcount, "\n"; my @msgs = $imap->messages or die "Could not messages: $@\n"; my $i; foreach $i (@msgs) { print "\$i = $i\n"; } $imap->disconnect or warn "Could not disconnect: $@\n";

And here is the output

C:\>"C:\Documents and Settings\rsh\My Documents\myperl\imap\imaptest.p +l" Using Mail::IMAPClient version 2.2.9 and perl version 5.8.2 (5.008002) Mail::IMAPClient not using Fast_IO; not available on this platform at +C:\Documents and Settings\rsh\My Documents\myperl\imap\imaptest.pl li +ne 5 Read: * OK [CAPABILITY IMAP4REV1 LOGIN-REFERRALS STARTTLS AUTH=LOGIN] +somehost IMAP4rev1 2003.339 at Wed, 31 Dec 2003 23:25:17 -0600 (CST) Connect: Received this from readline: 0/OUTPUT/* OK [CAPABILITY IMAP4R +EV1 LOGIN-REFERRALS STARTTLS AUTH=LOGIN] somehost IMAP4rev1 2003.339 +at Wed, 31 Dec 2003 23:25:17 -0600 (CST) Sending literal string in two parts: 1 Login "someuser" {6} then: somepassword Sending: 1 Login "XXXXXX" {6} Sent 36 bytes Read: + Ready for argument Sending: somepassword Sent 8 bytes Read: 1 OK [CAPABILITY IMAP4REV1 IDLE NAMESPACE MAILBOX-REFERRALS BINA +RY UNSELECT SCAN SORT THREAD=REFERENCES THREAD=ORDEREDSUBJECT MULTIAP +PEND] User someuser authenticated Autoloading: STATUS Inbox (MESSAGES) Sending: 2 STATUS Inbox (MESSAGES) Sent 27 bytes Read: * STATUS INBOX (MESSAGES 90) 2 OK STATUS completed Sending: 3 STATUS Inbox (MESSAGES) Sent 27 bytes Read: * STATUS INBOX (MESSAGES 90) 3 OK STATUS completed msg count = 90 Sending: 4 UID SEARCH ALL Sent 18 bytes Read: 4 BAD Command unrecognized: UID SEARCH $i = Sending: 5 LOGOUT Sent 10 bytes Read: * BYE somehost IMAP4rev1 server terminating connection 5 OK LOGOUT completed C:\>

Edited by Chady -- fixed formatting, added code tags.

Replies are listed 'Best First'.
Re: Mail::IMAPClient wont get messages
by rob_au (Abbot) on Jan 01, 2004 at 07:43 UTC
    You need to select the mail folder from which you want to list messages before you can call the messages method - For example:

    $imap->select($folder) or warn('Cannot select mail folder - ', $!); my @msgs = $imap->messages or die "Could not messages: $@\n";

     

    perl -le "print+unpack'N',pack'B32','00000000000000000000001010100111'"

      That worked perfectly! Thanks!
Re: Mail::IMAPClient wont get messages
by markov (Scribe) on Jan 01, 2004 at 11:21 UTC

    Although IMAP::Client works quite well on its own, you may consider to use Mail::Box as abstraction level. Mail::Box supports various folder formats and hides implementation differences. See its website. You can simply use messages on the remote IMAP4 server as if they are local.

     use Mail::Box::IMAP4;
     my $f = Mail::Box::IMAP4->new(host => ..., username => ...,
          password => ..., folder => ...) or die;
    
     foreach my $msg ($f->messages) ...
     $f->message(1)->delete;
     $f->message(3)->head->add('X-Handler' => 'my own');
    
      I looked through the documentation for this on cpan, and it seemed that the imap support was experimental. Have you used it and had success with it? I'm going to give it a try, but cautiously.
      Richard