Mail (IMAP): a reply to a reply is saved as a grandchild of the thread and never shows up in the list or in the thread view

Author: raizen10Created Sep 25, 2026Updated Sep 25, 2026

Bug report

Title

Mail (IMAP): a reply to a reply is saved as a grandchild of the thread and never shows up in the list or in the thread view

Issue Description

When an incoming email answers a message that is itself a reply, WebklexImapEmailProcessor::processMessage() attaches it to that reply (parent_id = the reply), creating a third level in the thread. The UI only reads one level of children, so the new message is invisible everywhere:

  • The mail list (EmailDataGrid) only shows root emails: ->whereNull('parent_id').
  • The thread view only renders the root's direct children: Email::emails() is hasMany(..., 'parent_id') on the root.

The message is stored correctly in the emails table, but nobody sees it. In a normal back-and-forth conversation (customer → us → customer), the customer's second reply is the one that disappears.

The lookup that finds the parent (in packages/Webkul/Email/src/InboundEmailProcessor/WebklexImapEmailProcessor.php, branch 2.2) matches in_reply_to / references against any stored email, including replies:

php
$email = $this->emailRepository->findOneWhere(['message_id' => $inReplyTo]);
// ...
$email = $this->emailRepository->create([
    // ...
    'parent_id' => $parentEmail?->id, // may be a reply, not the thread root
]);

Preconditions

  1. Krayin CRM v2.2.6 (same code on the current 2.2 branch), PHP 8.5, Laravel 12.
  2. MAIL_RECEIVER_DRIVER=webklex-imap, inbound processing working (php artisan inbound-emails:process).

Steps to reproduce

  1. A contact sends an email to the CRM mailbox (it's imported as a thread root).
  2. Reply to it from the CRM (or from the mailbox, so the reply is imported into the same thread).
  3. The contact answers that reply (their In-Reply-To points to our reply).
  4. Run php artisan inbound-emails:process.

Expected result

The contact's answer appears in the same thread, both in Mail → Inbox and inside the thread view.

Actual result

It's saved with parent_id = our reply (a grandchild of the root). It doesn't appear in the list (not a root) nor in the thread view (not a direct child of the root). No error is logged.

Suggested fix

Before attaching the new message, walk up to the root of the thread, so every message in a conversation is a direct child of the root (which is what the list and the view expect):

php
while ($email && $email->parent_id) {
    $email = $this->emailRepository->find($email->parent_id);
}

if ($email) {
    $parentEmail = $this->emailRepository->update([...], $email->id);
}

Existing grandchildren can be fixed with a one-off update that sets their parent_id to the root of their thread.

We are running this change in production and threads now show every message.