barbushin / php-imap

Manage mailboxes, filter/get/delete emails in PHP (supports IMAP/POP3/NNTP)
MIT License
1.65k stars 459 forks source link

[BUG] after updating to PHP 8.1 it is very slow #655

Closed slewocom closed 2 years ago

slewocom commented 2 years ago

Hi there,

we have updated from PHP 7.4 to PHP 8.1 and after the update the PHP Imap email import is very slow.

We have now done a time measurement and particularly concerns the getMail function. This function took about 3 seconds for the same email with PHP 7.4 and with PHP 8.1 the function takes over 63 seconds. The system is the same and the PHP settings are the same.

Is this problem known and is there a solution?

christianasche commented 2 years ago

Hi there,

\PhpImap\Imap::open returns a resource with php 8.0. But with 8.1 it returns an IMAP\Connection Object.

Source: The IMAP functions now accept and return IMAP\Connection objects instead of imap resources. https://www.php.net/manual/en/migration81.incompatible.php

This causes \PhpImap\Mailbox::initImapStreamWithRetry to retry the connection multiple times.

A fix could probably be to change \PhpImap\Imap::ping to something like this:

I think this needs further investigation.

Sebbo94BY commented 2 years ago

Thanks for the bug report @slewocom !

Also thanks for the hints and solution @christianasche !

I've implemented the suggested change and tested it using different PHP versions. It seems to fix the here reported issue, which I could reproduce before and it does not break on an older PHP version as far as I could see using my tests and the PHP Unit tests.

I've released a new version 4.5.1 with this fix.