diff --git a/app/Console/Commands/FetchEmails.php b/app/Console/Commands/FetchEmails.php index 1ccbe2481..f2e6b6791 100644 --- a/app/Console/Commands/FetchEmails.php +++ b/app/Console/Commands/FetchEmails.php @@ -561,10 +561,18 @@ public function processMessage($message, $message_id, $mailbox, $mailboxes, $ext } // Make sure that prev_thread belongs to the current mailbox. - // It may happen when forwarding conversation for example. - if ($prev_thread) { + // Problems may arise when forwarding conversation for example. + // + // For replies to email notifications it's allowed to have prev_thread in + // another mailbox as conversation can be moved. + // https://github.com/freescout-helpdesk/freescout/issues/3455 + if ($prev_thread && $message_from_customer) { if ($prev_thread->conversation->mailbox_id != $mailbox->id) { // https://github.com/freescout-helpdesk/freescout/issues/2807 + // Behaviour of email sent to multiple mailboxes: + // If a user from either mailbox replies, then a new conversation is created + // in the other mailbox with another new conversation ID. + // // Try to get thread by generated message ID. if ($in_reply_to) { $prev_thread = Thread::where('message_id', \MailHelper::generateMessageId($in_reply_to, $mailbox->id.$in_reply_to))->first(); @@ -748,6 +756,15 @@ public function processMessage($message, $message_id, $mailbox, $mailboxes, $ext return; } + // Save user thread only if there prev_thread is set. + // https://github.com/freescout-helpdesk/freescout/issues/3455 + if (!$prev_thread) { + $this->logError("Support agent's reply to the email notification could not be processed as previous thread could not be determined."); + $this->setSeen($message, $mailbox); + + return; + } + if (\Eventy::filter('fetch_emails.should_save_thread', true, $data) !== false) { $new_thread = $this->saveUserThread($data['mailbox'], $data['message_id'], $data['prev_thread'], $data['user'], $data['from'], $data['to'], $data['cc'], $data['bcc'], $data['body'], $data['attachments'], $data['message']->getHeader(), $data['date']); } else {