I am using PHP with IMAP. I need to retrieve the 20 most new emails from a folder. I user imap_sort to sort by date, but the problem is that for a large folder with 700 and more emails it takes ages.
Is there a way i can use PHP IMAP to sort messages by date and bring only the latest 20 emails?
Maybe to use imap_search ?
Here is my code:
$开发者_开发问答start_from = params::cleanDefault($_GET, 'start_from', 0);
$limit = params::cleanDefault($_GET, 'limit', 20);
$sort_by = params::cleanDefault($_GET, 'sort_by', 'SORTARRIVAL');
$emails = imap_sort($mbox, $sort_by, 1, SE_NOPREFETCH);
$emails = array_slice($emails, $start_from, $limit);
Thanks.
There's no straightforward way to do it.
You're already minimizing the data being fetched by the c-client library underlying PHP's imap_*
functions by sorting on SORTARRIVAL
instead of SORTDATE
. And, while there is an IMAP extension that allows a caller to request a subset of the SORT
results (e.g. the first 20 hits), very few IMAP servers support it and PHP can't make use of it.
You could try using imap_search
and asking for messages arrived since 1 day ago. If that's not enough hits, you could re-search for messages arrived since 2 days ago. And so on. But this can get messy to code, and it may not end up being any faster than what you're already doing.
精彩评论