laravel-imap
laravel-imap copied to clipboard
Can't sort paginated message collection
Describe the bug I have enabled auto-forwarding email from my Gmail account to my Webmail account, So when I receive a new email in my webmail and I check the message details, it looks like this:
From: [email protected] To: [email protected] Date: xxxxxxx
So as you can see "To" receiver is my gmail account. Now when I make query in larave-imap to filter those emails where "To" is equal to "[email protected]" and setFetchOrder("desc")->paginate(5) it does not show the messages list in reverse.
$oFolder->query()->to($my_gmail_account)->setFetchOrder("desc")->paginate(5);
Expected behavior It was expected to show all messages in descending order but it is neither in descending nor ascending
Screenshots

Desktop / Server (please complete the following information):
- OS: [Windows 11 Home]
- PHP: [8]
Experiencing the same issue..
Experiencing the same issue..
I have figured it out, so wanted to share my solution.
I hope my workaround will help you to sort this issue, it worked for me.
Controller method:
$oClient = Client::account('default');
$oClient->connect();
$oFolder = $oClient->getFolder('INBOX');
$client_mails = $oFolder->query()
->to($company->email)
->setFetchBody(false)
->setFetchOrder("desc")
->paginate(5);
Blade file/view:
@forelse($client_mails->reverse() as $mail)
<!-- your markup -->
@empty
<!-- messages not found -->
@endforelse
If you want to fetch mail body as well then change setFetchBody(false) to setFetchBody(true)
@jawadk116 Thanks! I was also thinking to implement it through front end as a final resort.
@jawadk116 Thanks! I was also thinking to implement it through front end as a final resort.
You are welcome sir.
The main method is not working. This is how I solved my issue:
$client->connect();
$inboxEmail = [];
$folders = $client->getFolders();
foreach ($folders as $folder) {
if($folder->name == "INBOX") {
$inboxEmail = $folder->messages()->all()->get();
}
}
$collection = $inboxEmail->sort(function ($a, $b) {
if ($a == $b) {
return 0;
}
return ($a > $b) ? -1 : 1;
});
$finalData = $collection->paginate(15);
The result is a list by latest emails first.
This always returns 0 mails, any ideas?
$pagination = $targetFolder->query()
->setFetchBody(false)
->setFetchOrder('desc')
->paginate(25);
And when trying to use
$pagination = $targetFolder->query()
->setFetchBody(false)
->setFetchOrder('desc')
->all()
->paginate(25);
sorting fails as well:

Hi @jawadk116 , @the-alichemist , @absalan , @Pinnokkio ,
Thanks a lot for your reports. Unfortunately, it's not possible to sort by date or any other header / body value - at least not without fetching every message first, which would add a huge overhead. This is limited by the IMAP protocol itself. Sorting by anything else (except for the message number / uid) is not possible.
I know this is not the answer you were hoping for, but I hope this helps you to understand the issue.
If you have any further questions or need further assistance, feel free to let me know - preferably over here: https://github.com/Webklex/php-imap/issues
Best regards and happy coding,