我正在使用laravel-imap从邮箱中检索邮件:
$aMessage = $selectedFolder->query()->setFetchFlags(true)->setFetchBody(true)->setFetchAttachment(false)->leaveUnread()
->limit(40, $highest_page)->get();
默认情况下,最新消息位于最高页面上。如果我仅获取所有消息并按原样显示它们,那么我会首先看到旧消息(第1页)。
到目前为止我尝试过的:
在我调用->get()
之后,可以将结果视为普通的laravel集合,但是使用这些方法没有成功:
$aMessage = $aMessage->sortByDesc('date')->values()->all();
$aMessage = $aMessage->sortByDesc(function ($item) {
//\Log::info(print_r($item, true));
return $item->date;
})->values()->all();
我确信上面的方法不起作用,因为我使用了->limit(40, $highest_page)
,这意味着该集合的大小为40。
如果我不限制结果并使用切片,则会遇到超时错误,因为邮箱中的邮件太多:
$aMessage = $selectedFolder->query()->setFetchFlags(true)->setFetchBody(true)->setFetchAttachment(false)->leaveUnread()
->get()->reverse()->slice(0, $messages_per_page);
我也尝试过laravel-imap的方法paginate()
。结果与limit()
相同。
至此,我确定在将结果转换成集合之前,我必须进行某种排序。我还发现:
'options' => [
'fetch_order' => 'desc',
]
选项已添加到客户端。我尝试了asc和desc。我还清除了配置缓存,但没有任何效果。
我仍然在第1页上看到最早的结果,最后看到最新的结果。
有人有更多想法吗?
在尝试了几种解决方案之后,我决定进入供应商文件(config / imap.php)并在其中更改fetch_order:
'fetch_order' => 'desc'
默认为asc。似乎在创建客户端时更改fetch_order不会覆盖配置。我以前有这个,它不起作用,也没有抛出错误:
$this->oClient = new Client([
'host' => env('MAIL_HOST_IN'),
'port' => env('MAIL_PORT_IN'),
'encryption' => env('MAIL_ENCRYPTION_IN'),
'validate_cert' => true,
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'protocol' => 'imap',
'options' => [
'fetch_order' => 'desc',
]
]);