PHP Conference Nagoya 2025

imap_sort

(PHP 4, PHP 5, PHP 7, PHP 8)

imap_sortGets and sort messages

说明

imap_sort(
    IMAP\Connection $imap,
    int $criteria,
    bool $reverse,
    int $flags = 0,
    ?string $search_criteria = null,
    ?string $charset = null
): array|false

Gets and sorts message numbers by the given parameters.

参数

imap

IMAP\Connection 实例。

criteria

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

reverse

Whether to sort in reverse order.

flags

The flags 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.

返回值

Returns an array of message numbers sorted by the given parameters, 或者在失败时返回 false.

更新日志

版本 说明
8.1.0 现在 imap 参数接受 IMAP\Connection 实例,之前接受有效的 imap resource
8.0.0 reverse is now bool instead of int.
8.0.0 search_criteria and charset are now nullable.
添加备注

用户贡献的备注 1 note

up
9
antoine dot spam-nono at maxg dot info
18 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).
To Top