Description
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' => 'my_email@gmail.com',
'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 my_email@gmail.com "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.