imap_sort limit the number of results?

末鹿安然 提交于 2020-01-17 03:00:48

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/4216934/imap-sort-limit-the-number-of-results

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!