http://qs321.pair.com?node_id=480393

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

Hi monks.

I'm having a problem reading mail from an exahcnge 5.5 server using Mail::ImapClient;

As far as I can see it should be pretty straightforward, I've read the perl module documentation and checked out RFC2060 which gives specifics about the IMAP SEARCH command but I can't seem to get this supposedly simple thing to work.

The following code should login to the mail box and do a search for any mails that have 'test' in the subject header.

#!perl use strict; use Mail::IMAPClient; my $server='xxx'; my $user='yyy'; my $password='zzz'; my $imap=new Mail::IMAPClient( Server=>$server, User=>$user, Password=>$password ) or die "Cannot connect to \"$server\" as \"$user\" : $!"; my @search=("SUBJECT","test"); $imap->search(@search) or warn "Search error\n"; my @results = $imap->Results; foreach(@results) {print "Result : \"$_\"\n";} print "Inbox count : ".$imap->message_count('inbox');

But instead of retrieving the mails in question it just returns.

Search error Result : "2 UID SEARCH SUBJECT test " Result : "2 BAD SEARCH command received in invalid state. " Inbox count : 6

Whatever search fields I put into the search array I get he same error but the $imap->message_count always seem to work fine which indicates that I am successfully able to login to the account and retrieve information, it's just the syntax of the search command that seems to be the issue.

Could any monks offer any advice?

Replies are listed 'Best First'.
Re: Searching using Mail::IMAPClient on exchange
by rob_au (Abbot) on Aug 03, 2005 at 11:07 UTC
    You need to select the mailbox (folder) against which you wish to perform the search first using the select method - If you do not know the hierarchy, separators and namespace of your IMAP mail store, the folders and namespace methods may be useful.

    For example:

    my @folders = $imap->folders; foreach my $folder (@folders) { $imap->select($folder) or die('Cannot select IMAP folder - ', $@); if ($imap->search(@search)) { push @results, $imap->Results; } }

     

    perl -le "print unpack'N', pack'B32', '00000000000000000000001000000000'"