php-imap
php-imap copied to clipboard
Idle callback not fired when sending an email
Describe the bug
Idle connection is established successfully with a callback for the new email, but when sending an email to that connected email from another email and being successfully added to the INBOX folder but callback was not fired.
Code to Reproduce
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Webklex\PHPIMAP\ClientManager;
use Webklex\PHPIMAP\Exceptions\ConnectionFailedException;
use Webklex\PHPIMAP\Exceptions\FolderFetchingException;
use Webklex\PHPIMAP\Message;
class ImapIdleTest extends Command
{
protected $signature = 'imap:test-idle';
protected $description = 'Test raw IMAP IDLE using Webklex/php-imap directly';
public function handle()
{
$this->info("š Connecting to Gmail via IMAP...");
$clientManager = new ClientManager();
$client = $clientManager->make([
'host' => 'imap.gmail.com',
'port' => 993,
'encryption' => 'ssl', // or 'tls' if using 587
'validate_cert' => true,
'username' => '[email protected]',
'password' => 'App password', // App Password, not Gmail password
'protocol' => 'imap'
]);
try {
$client->connect();
$this->info("ā
Connected.");
} catch (ConnectionFailedException $e) {
$this->error("ā Connection failed: " . $e->getMessage());
return 1;
}
try {
$folder = $client->getFolder('INBOX');
} catch (FolderFetchingException $e) {
$this->error("ā Folder error: " . $e->getMessage());
return 1;
}
$this->info("šØ Listening for new messages via IDLE...");
try {
$folder->idle(function (Message $message) {
echo "\nš„ New message: " . $message->getSubject() . "\n";
});
} catch (ConnectionFailedException $e) {
$this->error("ā IDLE failed: " . $e->getMessage());
}
}
}
Expected behavior What is expected is when a new email is received in the INBOX, it would echo in the terminal "š„ New message: " with the subject of the new email.
Desktop / Server:
- OS: MacOS 15.3.1 (24D70)
- PHP: 8.3.16
- Gmail
Additional context
I also tried idling manually to the gmail imap servers using openssl s_client -connect imap.gmail.com:993 -crlf. After that, I logged in using a LOGIN [email protected] "my app password" which would give you success message. using a SELECT INBOX to select inbox and then a IDLE to open an idle connection. When sending a new email, it would print a number that I guess is the number of unread emails.