IMAP (webklex-imap): messages in INBOX are never imported when INBOX has subfolders
Bug report
Title
IMAP (webklex-imap): messages in INBOX are never imported when INBOX has subfolders
Issue Description
With MAIL_RECEIVER_DRIVER=webklex-imap, no email is ever imported (0 rows in emails, no error in the CLI or in laravel.log) when the mail server exposes INBOX as a parent folder with its own subfolders (e.g. INBOX.Sent, INBOX.Trash, INBOX.Drafts — the usual Dovecot / cPanel layout).
The cause is processMessagesFromLeafFolders() in packages/Webkul/Email/src/InboundEmailProcessor/WebklexImapEmailProcessor.php (branch 2.2, ~line 164): when a folder has children it recurses into them and returns without processing the folder's own messages, assuming only leaf folders contain mail:
if (! $folder->children->isEmpty()) {
$this->processMessagesFromLeafFolders($folder->children);
return; // INBOX's own messages are skipped
}Since INBOX has children on these servers, the messages in INBOX itself — i.e. all incoming mail — are never processed.
Preconditions
- Krayin CRM v2.2.6 (same code on the current
2.2branch), PHP 8.5, Laravel 12. MAIL_RECEIVER_DRIVER=webklex-imap, IMAP settings configured and connecting fine.- IMAP account whose
INBOXhas subfolders (Dovecot namespaceINBOX.*).
Steps to reproduce
- Configure IMAP against a Dovecot/cPanel mailbox where
INBOXhas subfolders. - Send a new email to that mailbox.
- Run
php artisan inbound-emails:process.
Expected result
The new email is imported into Mail → Inbox.
Actual result
Nothing is imported and no error is shown. Connecting with Webklex\IMAP\Facades\Client::make([...]) directly confirms the messages are in INBOX.
Suggested fix
Process every folder's own messages and recurse into its children (processMessage() already de-duplicates by message_id, so this is safe):
$rootFoldersCollection->each(function ($folder) {
if (! $folder->children->isEmpty()) {
$this->processMessagesFromLeafFolders($folder->children);
}
if (in_array($folder->name, ['All Mail'])) {
return;
}
$folder->query()->since(now()->subDays(10))->get()->each(function ($message) {
$this->processMessage($message);
});
});We've been running this change in production without issues.
Source: krayin/laravel-crm