php[tek] 2018 : Call for Speakers

imap_sort

(PHP 4, PHP 5, PHP 7)

imap_sortGets and sort messages

Description

array imap_sort ( resource $imap_stream , int $criteria , int $reverse [, int $options = 0 [, string $search_criteria = NULL [, string $charset = NULL ]]] )

Gets and sorts message numbers by the given parameters.

Parameters

imap_stream

An IMAP stream returned by imap_open().

criteria

Criteria can be one (and only one) of the following:

  • SORTDATE - message Date
  • SORTARRIVAL - arrival date
  • SORTFROM - mailbox in first From address
  • SORTSUBJECT - message subject
  • SORTTO - mailbox in first To address
  • SORTCC - mailbox in first cc address
  • SORTSIZE - size of message in octets

reverse

Set this to 1 for reverse sorting

options

The options are a bitmask of one or more of the following:

  • SE_UID - Return UIDs instead of sequence numbers
  • SE_NOPREFETCH - Don't prefetch searched messages

search_criteria

IMAP2-format search criteria string. For details see imap_search().

charset

MIME character set to use when sorting strings.

Return Values

Returns an array of message numbers sorted by the given parameters.

add a note add a note

User Contributed Notes 7 notes

up
7
antoine dot spam-nono at maxg dot info
11 years ago
I worked a lot with IMAP functions since I wrote a complete webmail and I've got a little tip about the imap_sort function :

There is a big difference between :

<?php
imap_sort
($imap, SORTDATE, 1);
// and
imap_sort($imap, SORTARRIVAL, 1);
?>

The first command will issue a
>> FETCH 1:last (UID ENVELOPE BODY.PEEK[HEADER.FIELDS (Newsgroups Content-MD5 Content-Disposition Content-Language Content-Location Followup-To References)] INTERNALDATE RFC822.SIZE FLAGS)

While the second resulted in
>> FETCH 1:last (UID INTERNALDATE RFC822.SIZE FLAGS)

As a result, using SORTDATE took 3 seconds longer to complete on a 800-emails mailbox, while the results are quite the same (except if you have to deal with forged dates or timezones, but the arrival order is far more logical)

My advice if you sort your emails by arrival is to actually use SORTARRIVAL, or better don't use imap_sort and go straight with message numbers (not UIDs). On large mailboxes, if you display messages per page, you will have significant performance increases (by avoiding 5 seconds of sorting).
up
2
boscolau at mac dot com
12 years ago
in reply to graham_NOSPAM at rdb-concepts dot NOSPAM dot co dot uk here is a more elegant way to achieve sorting of search results:

function order_search($searchresults, $sortresults) {
    return array_values(array_intersect($sortresults,$searchresults));
}

example of using function:
   $sortresults = imap_sort($stream, $sort, $sortorder);
   $searchresults = imap_search($stream, $query);
   $sorted_search_results = order_search($searchresults, $sortresults);

sorted version of search results will then be held in the array of $sorted_search_results.
up
0
persian-horde at metanetworking dot com
12 years ago
Hello,
We used MIME and NLS libraries in our previous code(imap_locale_sort) that
are part of PEAR and Horde projects, and so our function
was so specific. Here is the general (pure php) code for this
function:

function imap_locale_sort($stream,$criteria,$reverse,$locale,$options)
{
        if ($criteria!=SORTSUBJECT)
                return (imap_sort($stream,$criteria,$reverse,$options));

        $unsorted = array();
        $sortresult = array();

        $MC=imap_check($stream);
        $MN=$MC->Nmsgs;

        $overview = imap_fetch_overview($stream,"1:$MN",0);
        $k=0;
        while( list($key,$val) = each($overview))
        {
                $unsorted[$k]["uid"]=$val->uid;
                $unsorted[$k]["subject"]=imap_utf8($val->subject);
                $k++;
        }
        usort ($unsorted, create_function('$a,$b','setlocale(LC_ALL,$locale);return strcoll($a["subject"],$b["subject"]);'));

        for ($m=0;$m<count($unsorted);$m++)
                array_push($sortresult,$unsorted[$m]["uid"]);

        if ($reverse)
                $sortresult = array_reverse($sortresult);

        return $sortresult;
}

Sample usage:

$mbox = imap_open("{localhost:143}INBOX.sent-mail","userid","password");

if ($mbox)
    echo ("Connection Successful!");

$sorted = imap_locale_sort($mbox,SORTSUBJECT,0,'fa_IR',0);
print_r($sorted);
print ("\n\n");
$sorted = imap_sort($mbox,SORTSUBJECT,SE_UID);
print_r($sorted);

imap_close($mbox);
up
0
persian-horde at metanetworking dot com
12 years ago
imap_sort uses the c-client library to sort messages. This library currently does not
support locale-based sort for foreign languages (Although it has charset option).
They are working on this(http://www.washington.edu/imap/IMAP-FAQs/#1.12),
but you can use the following function which uses strcoll() for locale-based sorting.
Pease note that this is required for SUBJECT field sorting, because most of
other fields are sorted correctly by imap_sort in any locale:

function imap_locale_sort($stream,$criteria,$reverse,$locale,$options)
{
        if ($criteria!=SORTSUBJECT)
                return (imap_sort($stream,$criteria,$reverse,$options));

        $unsorted = array();
        $sortresult = array();

        $MC=imap_check($stream);
        $MN=$MC->Nmsgs;

        $overview = imap_fetch_overview($stream,"1:$MN",0);
        $k=0;
        while( list($key,$val) = each($overview))
        {
                $unsorted[$k]["uid"]=$val->uid;
                $unsorted[$k]["subject"]=MIME::decode($val->subject, NLS::getCharset());
                $k++;
        }
        usort ($unsorted, create_function('$a,$b','setlocale(LC_ALL,$locale);return strcoll($a["subject"],$b["subject"]);'));

        for ($m=0;$m<count($unsorted);$m++)
                array_push($sortresult,$unsorted[$m]["uid"]);

        if ($reverse)
                $sortresult = array_reverse($sortresult);

        return $sortresult;
}

Usage example:

$sorted = imap_locale_sort($stream,SORTSUBJECT,0,'fa_IR',0);
up
0
tilmauder at yahoo dot com ly" 972" title="no votes..."> 0 tream);
), er(). sdav (Altme (aipt> tron/phptronauI workedn ox, llpan sults,$sun="namnbsp;mnbsiexam. Iif you> imPOP3of
2707/div>
up
down
tilmauder at yahoo dot com<1 01:2" 972" title5"no votes..."> 0 tream);
up
down
in reply to graham_NOSPAlass="user">tilmauder at yahoo dot com 0 tream);

function order_search($searchreresultshashbr />       $);
       $bsp; >$op;      );
$op; i>/>&ncoizeof results:
); i++ order_search($searchrearch($searchreresultshash[results:
[$i]] type="tder_search($searchrep; $k++;
  );
$op; i>/>&ncoizeof re_results = o i++ order_search($searchrearch($searchre);$bsp; bsp;           $k++;
  p; $k++;
  );
);
}
   $sordid,er />$sorted = arch($searchreresultsrt, $sortorder);
 &nbs;
$sorted = arch($searchrer$query);
   $sorted_search_results = order_search($lts);

s nam/ro/>}
foot
add a note"V5115en p>.ie9 "layout" class .net '"layoutxt m'>()$mbo/div; IM"P < or more 'copldbo/div; > et-involved" > details see details see 2.ref="#4rder)​aler >// and​aler P&perm= d/quence numbe > et-involved" > details see 2.ref="#4rder)​append>// and​appendP&perm= d/quence numbe > et-involved" > details see 2.ref="#4rder)​ fun64>// and​ fun64P&perm= d/quence numbe > et-involved" > details see 2.ref="#4rder)​ n/sec>// and​ n/securitp;perm= d/quence numbe > et-involved" > details see 2.ref="#4rder)​ al/>// and​ al/uritp;perm= d/quence numbe > et-involved" > details see <
2.ref="#4rder)​ al/
<>// and​ al/
et-involved" > details see
2.ref="#4rder)​br />>// and​br />uritp;perm= d/quence numbe > et-involved" > details see prev"> .ref="#4rder)​biv> pre)​"> >// and​biv> pre)​"> uritp;perm= d/quence numbe > et-involved" > details see details see details see details see details see details see
< .ref="#4rder)​a>
et-involved" > details see details see 2.ref="#4rder)​sp; & al/P rder)​sp; & al/uritp;perm= d/quence numbe > et-involved" > details see er
2.ref="#4rder)​sp; &iv> erP rder)​sp; &iv> eruritp;perm= d/quence numbe > et-involved" > details see details see et-involved" > details see et-involved" > details see et-involved" > details see a a crder)​get)​d> auritp;perm= d/quence numbe > et-involved" > details see arood> aroot rder)​get)​d> arooturitp;perm= d/quence numbe > et-involved" > details see details see details see 2.ref="#4rder)​aetsubon et-involved" > details see er
2.ref="#4rder)​iv> erP rder)​iv> eruritp;perm= d/quence numbe > et-involved" > details see erer">
2.ref="#4rder)​iv> ers="refrder)​iv> ers="ruritp;perm= d/quence numbe > et-involved" > details see ers .ref="#4rder)​iv> ersefrder)​iv> ersuritp;perm= d/quence numbe > et-involved" > details see details see et-involved" > details see details see details see 2.ref="#4rder)​ subon et-involved" > details see 2.ref="#4rder)​ sub frder)​ suburitp;perm= d/quence numbe > et-involved" > details see details see 2.ref="#4rder)​ <)​bapy frder)​ <)​bap/uritp;perm= d/quence numbe > et-involved" > details see details see details see
2.ref="#4rder)​ frder)​ uritp;perm= d/quence numbe > et-involved" > details see er-sp; &n
2.ref="#4rder)​ ime)​iv> er)​deass="trder)​ ime)​iv> er)​deass=uritp;perm= d/quence numbe > et-involved" > details see
2.ref="#4rder)​ sgn>"trder)​ sgn>uritp;perm= d/quence numbe > et-involved" > details see 2.ref="#4rder)​ utf7_​ton​bsp;"trder)​ utf7_​ton​bsp;uritp;perm= d/quence numbe > et-involved" > details see 2.ref="#4rder)​num)​ sg"trder)​num)​ sguritp;perm= d/quence numbe > et-involved" > details see 2.ref="#4rder)​num)​rec et-involved" > details see et-involved" > details see et-involved" > details see et-involved" > details see et-involved" > details see et-involved" > details see et-involved" > details see <"fref="#4rder)​rfc822)​pamse)​adrsisd"trder)​rfc822)​pamse)​adrsisduritp;perm= d/quence numbe > et-involved" > details see ers .ref="#4rder)​rfc822)​pamse)​iv> ersefrder)​rfc822)​pamse)​iv> ersuritp;perm= d/quence numbe > et-involved" > details see details see 2.ref="#4rder)​save al/efrder)​save al/uritp;perm= d/quence numbe > et-involved" > details see details see details see et-involved" > details see a a crder)​set)​d> auritp;perm= d/quence numbe > et-involved" > details see details see .ref="#4rder)​set pre)​s> >// and​set pre)​s> uritp;perm= d/quence numbe > et-involvednt libr" > details see details see details see details see 2.ref="#4rder)​tailad>// and​tailad /itp;perm= d/quence numbe > et-involved" > details see details see 2.ref="#4rder)​sp; >rder)​sp; /itp;perm= d/quence numbe > et-involved" > details see details see details see 2.ref="#4rder)​utf7_​deass="trder)​utf7_​deass= /itp;perm= d/quence numbe > et-involved" > details see 2.ref="#4rder)​utf7_​en; &n"trder)​utf7_​en; &n /itp;perm= d/quence numbe > et-involved" > details see 2.ref="#4rder)​utf8_​ton​ utf7"trder)​utf8_​ton​ utf7 /itp;perm= d/quence numbe > et-involved" > details see 2.ref="#4rder)​bsp;"trder)​bsp;uritp;perm= d/quence numbe > ch_resu ie9 "layou .net > " clainT fiootert" class" > row-flsp; >>IM"PCap/radd@ &bap/; 2001-2017 n cl2ivalIM"PCaclac@12' hequence numbeOECT f
< >Mi>Mc/
2iP />Mc/ policy12' hequence num . .net / ?t=142183761 / ?t=142183761 / ?t=142183761 .min.jo>/ ?t=142183761 / ?t=142183761 / ?t=1500560403 /