laravel-imap icon indicating copy to clipboard operation
laravel-imap copied to clipboard

Can't sort paginated message collection

Open jawadk116 opened this issue 3 years ago • 5 comments

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 imap

Desktop / Server (please complete the following information):

  • OS: [Windows 11 Home]
  • PHP: [8]

jawadk116 avatar Feb 19 '22 16:02 jawadk116

Experiencing the same issue..

ali-raza-saleem avatar Sep 22 '22 19:09 ali-raza-saleem

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

jawadk116 avatar Sep 22 '22 20:09 jawadk116

If you want to fetch mail body as well then change setFetchBody(false) to setFetchBody(true)

jawadk116 avatar Sep 22 '22 20:09 jawadk116

@jawadk116 Thanks! I was also thinking to implement it through front end as a final resort.

ali-raza-saleem avatar Sep 22 '22 20:09 ali-raza-saleem

@jawadk116 Thanks! I was also thinking to implement it through front end as a final resort.

You are welcome sir.

jawadk116 avatar Sep 22 '22 20:09 jawadk116

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.

absalan avatar Mar 13 '23 09:03 absalan

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: image

Pinnokkio avatar Mar 16 '23 22:03 Pinnokkio

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,

Webklex avatar Jun 23 '23 20:06 Webklex