laminas / laminas-mail

Provides generalized functionality to compose and send both text and MIME-compliant multipart e-mail messages
https://docs.laminas.dev/laminas-mail/
BSD 3-Clause "New" or "Revised" License
94 stars 64 forks source link

issue fetching email #177

Open mjac1521 opened 2 years ago

mjac1521 commented 2 years ago

Summary

I have an email account with more than 15 000 emails in the Sent folder. I create a web interface similar to gmail using Laminas IMAP to fetch the emails from postfix wish use Dovecot to serve the IMAP service.

Current behavior

When I try to access the folder Sent in my interface it take more than 1 minute to show me the emails. I am using pagination so to show me the email in page 1 it take more than a minute, the same for page 2 and so on. I believe every time I hit a page it is reading the whole 15 000 emails to only show me 20 in a page.

How to reproduce

Adding 15 0000 emails o more to a folder. In my case the folder is Sent.

Expected behavior

Laminas-IMAP should fetch emails in a matter of seconds

Sincerely Maykel

froschdesign commented 2 years ago

@mjac1521

I believe every time I hit a page it is reading the whole 15 000 emails to only show me 20 in a page.

Unfortunately we don't know your code, so it would be helpful if you could show us how your query / pagination looks like.

mjac1521 commented 2 years ago

Hi Frank,

Thank you so much in advance for taking care of this so sun. Please find bellow the code we use to get the email and do the pagination

$mail = new Imap([
      'host'     => config('env.HOST'),
      'port'     => config('env.PORT'),
      'user'     => $mailbox->local_part,
      'password' => md5($pass->password),
      'ssl'   => config('env.ENCRY'),
      'novalidatecert' => config('env.VERIFY'),
]);

$cantidad =  $mail->countMessages();
$ini = ($page * 10) + 1;
$end = (((($page * 10) + 10) <= $cantidad ) ? ($page * 10) + 10 : $cantidad);

for($j = $ini - 1; $j < $end; $j++) {
   $mail[$j]->getContent();
}

Sincerely Maykel

Ocramius commented 2 years ago

From your logic, I can see that AbstractStorage#offsetGet($j) will be called:

https://github.com/laminas/laminas-mail/blob/b9c896edfe4e076440fb6427c47f6164ec4a2424/src/Storage/AbstractStorage.php#L211-L221

That will in turn go to Imap#getMessage():

https://github.com/laminas/laminas-mail/blob/b9c896edfe4e076440fb6427c47f6164ec4a2424/src/Storage/Imap.php#L104-L122

Could it be that counting messages is the slow part?

https://github.com/laminas/laminas-mail/blob/b9c896edfe4e076440fb6427c47f6164ec4a2424/src/Storage/Imap.php#L59-L87

I'd suggest writing a test to verify the issue and sending a patch, @mjac1521.

mjac1521 commented 2 years ago

Hi Marcos, Thank you for your help. Is like you said the issue should be counting so many emails, I have accounts with a couple of emails and those accounts work perfect.

Sincerely Maykel

Ocramius commented 2 years ago

Right, what I'm asking for is if you can design an integration test for the laminas/laminas-mail test suite 😁

We can't design this for you, given availability of contributors.