Search code examples
perlimapclient

Unable to sort using Mail::IMAPClient


Yesterday I was trying to sort the emails coming from imap using the Mail::IMAPClient library doing the following:

my $imap = Mail::IMAPClient->new(
#my imap settings
);
#folder selection
my @messages = $imap->sort('(REVERSE ARRIVAL) UTF-8 ALL');

But I got:

4 BAD Error in IMAP command UID SORT: Invalid sort list argument (0.000 + 0.000 secs). at /var/www/localhost/htdocs/user/mail.pl line 93.: /var/www/localhost/htdocs/user/mail.pl

I was several hours trying to resolve this issue until I found the answer. (below)


Solution

  • First I should point I was inspired from this question from came the wrong idea of put everything as a single argument.

    The correct answer was:

    my @messages = $imap->sort('(REVERSE ARRIVAL)', 'UTF-8', 'ALL');
    

    I should separe sort pattern from charset and charset from search pattern as the different arguments they are. Now my code is working.