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] Webklex\PHPIMAP\Exceptions\RuntimeException: idle failed #682

Closed dokicro closed 1 year ago

dokicro commented 1 year ago

Describe the bug After 10-30minutes of running custom idle command per instructions (https://www.php-imap.com/frameworks/laravel/commands) I get the following error:

[2022-10-11 13:30:58] production.ERROR: Webklex\PHPIMAP\Exceptions\RuntimeException: idle failed in /var/www/laravel/vendor/webklex/php-imap/src/Connection/Protocols/ImapProtocol.php:1055
Stack trace:
#0 /var/www/laravel/vendor/webklex/php-imap/src/Folder.php(402): Webklex\PHPIMAP\Connection\Protocols\ImapProtocol->idle()
#1 /var/www/laravel/app/Console/Commands/PayTicketIdleCommand.php(81): Webklex\PHPIMAP\Folder->idle()
#2 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): App\Console\Commands\PayTicketIdleCommand->handle()
#3 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Container/Util.php(40): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#4 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure()
#5 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod()
#6 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php(653): Illuminate\Container\BoundMethod::call()
#7 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Console/Command.php(136): Illuminate\Container\Container->call()
#8 /var/www/laravel/vendor/symfony/console/Command/Command.php(298): Illuminate\Console\Command->execute()
#9 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Console/Command.php(121): Symfony\Component\Console\Command\Command->run()
#10 /var/www/laravel/vendor/symfony/console/Application.php(1015): Illuminate\Console\Command->run()
#11 /var/www/laravel/vendor/symfony/console/Application.php(299): Symfony\Component\Console\Application->doRunCommand()
#12 /var/www/laravel/vendor/symfony/console/Application.php(171): Symfony\Component\Console\Application->doRun()
#13 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Console/Application.php(94): Symfony\Component\Console\Application->run()
#14 /var/www/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(129): Illuminate\Console\Application->run()
#15 /var/www/laravel/artisan(37): Illuminate\Foundation\Console\Kernel->handle()
#16 {main}

I am connected to Gmail business account

I played with debug mode and here is the output:

<< TAG3 OK [READ-WRITE] INBOX selected. (Success)
>> TAG4 IDLE
<< + idling
<< * 1 EXPUNGE
>> DONE
<< * 1 EXISTS
>> TAG5 IDLE
<< TAG4 OK IDLE terminated (Success)
>> DONE
<< + idling
>> TAG6 IDLE
<< TAG5 OK IDLE terminated (Success)

   Webklex\PHPIMAP\Exceptions\RuntimeException

  idle failed

  at vendor/webklex/php-imap/src/Connection/Protocols/ImapProtocol.php:1055
    1051▕      */
    1052▕     public function idle() {
    1053▕         $this->sendRequest("IDLE");
    1054▕         if (!$this->assumedNextLine('+ ')) {
  ➜ 1055▕             throw new RuntimeException('idle failed');
    1056▕         }
    1057▕     }
    1058▕
    1059▕     /**

      +1 vendor frames
  2   app/Console/Commands/InfoTicketIdleCommand.php:81
      Webklex\PHPIMAP\Folder::idle()

      +13 vendor frames
  16  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()
>> TAG7 LOGOUT
<< + idling
<< TAG6 BAD Could not parse command

The problem could be that TAG5 came after TAG6 ?

Desktop / Server (please complete the following information):

gazben commented 1 year ago

@dokicro Did you get a chance to solve this?

We got a similar exception:

<< * 18 EXISTS
>> DONE
<< * 2 RECENT
>> DONE
<< TAG15 OK Idle completed (244.999 + 244.999 + 244.998 secs).
>> TAG16 IDLE
<< DONE BAD Error in IMAP command: Invalid command name (0.001 + 0.000 secs).
[2022-11-14 10:02:15] production.ERROR: idle failed {"exception":"[object] (Webklex\\PHPIMAP\\Exceptions\\RuntimeException(code: 0): idle failed at /var/www/html/vendor/webklex/php-imap/src/Connection/Protocols/ImapProtocol.php:1062)
dokicro commented 1 year ago

I did but hacky way take a look at this: https://github.com/Webklex/laravel-imap/issues/453#issuecomment-1291856332 Everything was working fine with that workaround but then Gmail started hitting me with quota limits so I had to switch to https://github.com/dacastro4/laravel-gmail and have a corn job every minute pulling the data...

gazben commented 1 year ago

I'm using mailbox.org and I'll try out this solution: https://github.com/Webklex/php-imap/compare/master...apicore-engineering:php-imap:master

In our case the problem was that after the DONE and IDLE commands the package expects the + idling or OK responses and if a new email comes in the command will fail.

Example:

>> DONE
<< * 1 RECENT
<< TAG18 OK Idle completed (0.591 + 0.590 + 0.590 secs).