imap_sort限制结果数量?
我将 PHP 与 IMAP 结合使用。我需要从文件夹中检索 20 封最新电子邮件。我使用 imap_sort 按日期排序,但问题是,对于包含 700 封甚至更多电子邮件的大文件夹,需要很长时间。
有没有一种方法可以使用 PHP IMAP 按日期对邮件进行排序并仅显示最新的 20 封电子邮件?
也许使用 imap_search ?
这是我的代码:
$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);
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有直接的方法可以做到这一点。
您已经通过 按
SORTARRIVAL
排序,而不是SORTDATE
。而且,虽然有 IMAP 扩展,允许调用者请求SORT
结果的子集(例如前 20 个命中),很少有 IMAP 服务器支持它,PHP 无法使用它。您可以尝试使用
imap_search
并询问 1 天前收到的邮件。如果点击次数不够,您可以重新搜索 2 天前收到的邮件。等等。但这可能会使代码变得混乱,并且最终可能不会比您已经在做的更快。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 onSORTARRIVAL
instead ofSORTDATE
. And, while there is an IMAP extension that allows a caller to request a subset of theSORT
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.