diff --git a/app/Console/Commands/FetchEmails.php b/app/Console/Commands/FetchEmails.php index f16a030db..330969781 100644 --- a/app/Console/Commands/FetchEmails.php +++ b/app/Console/Commands/FetchEmails.php @@ -428,16 +428,35 @@ public function processMessage($message, $message_id, $mailbox, $mailboxes, $ext $prev_thread_id = ''; // Customer replied to the email from user - preg_match('/^'.\MailHelper::MESSAGE_ID_PREFIX_REPLY_TO_CUSTOMER."\-(\d+)\-/", $prev_message_id, $m); - if (!empty($m[1])) { - $prev_thread_id = $m[1]; + preg_match('/^'.\MailHelper::MESSAGE_ID_PREFIX_REPLY_TO_CUSTOMER."\-(\d+)\-([^@]+)@/", $prev_message_id, $m); + // Simply checking thread_id from message_id was causing an issue when + // customer was sending a message from FreeScout - the message was + // connected to the wrong conversation. + if (!empty($m[1]) && !empty($m[2])) { + $message_id_hash = $m[2]; + if (strlen($message_id_hash) == 16) { + if ($message_id_hash == \MailHelper::getMessageIdHash($m[1])) { + $prev_thread_id = $m[1]; + } + } else { + // Backward compatibility. + $prev_thread_id = $m[1]; + } } // Customer replied to the auto reply if (!$prev_thread_id) { - preg_match('/^'.\MailHelper::MESSAGE_ID_PREFIX_AUTO_REPLY."\-(\d+)\-/", $prev_message_id, $m); - if (!empty($m[1])) { - $prev_thread_id = $m[1]; + preg_match('/^'.\MailHelper::MESSAGE_ID_PREFIX_AUTO_REPLY."\-(\d+)\-([^@]+)@/", $prev_message_id, $m); + if (!empty($m[1]) && !empty($m[2])) { + $message_id_hash = $m[2]; + if (strlen($message_id_hash) == 16) { + if ($message_id_hash == \MailHelper::getMessageIdHash($m[1])) { + $prev_thread_id = $m[1]; + } + } else { + // Backward compatibility. + $prev_thread_id = $m[1]; + } } } diff --git a/app/Jobs/SendAutoReply.php b/app/Jobs/SendAutoReply.php index a278a316b..3e7f27c7c 100644 --- a/app/Jobs/SendAutoReply.php +++ b/app/Jobs/SendAutoReply.php @@ -51,7 +51,7 @@ public function handle() $headers['References'] = '<'.$this->thread->message_id.'>'; // Create Message-ID for the auto reply - $message_id = \App\Misc\Mail::MESSAGE_ID_PREFIX_AUTO_REPLY.'-'.$this->thread->id.'-'.md5($this->thread->id).'@'.$this->mailbox->getEmailDomain(); + $message_id = \App\Misc\Mail::MESSAGE_ID_PREFIX_AUTO_REPLY.'-'.$this->thread->id.'-'.\MailHelper::getMessageIdHash($this->thread->id).'@'.$this->mailbox->getEmailDomain(); $headers['Message-ID'] = $message_id; $customer_email = $this->conversation->customer_email; diff --git a/app/Jobs/SendReplyToCustomer.php b/app/Jobs/SendReplyToCustomer.php index e7a22c79a..60954f2ed 100644 --- a/app/Jobs/SendReplyToCustomer.php +++ b/app/Jobs/SendReplyToCustomer.php @@ -173,7 +173,7 @@ public function handle() $headers['References'] = '<'.$last_customer_thread->message_id.'>'; } - $this->message_id = \App\Misc\Mail::MESSAGE_ID_PREFIX_REPLY_TO_CUSTOMER.'-'.$this->last_thread->id.'-'.md5($this->last_thread->id).'@'.$mailbox->getEmailDomain(); + $this->message_id = \App\Misc\Mail::MESSAGE_ID_PREFIX_REPLY_TO_CUSTOMER.'-'.$this->last_thread->id.'-'.\MailHelper::getMessageIdHash($this->last_thread->id).'@'.$mailbox->getEmailDomain(); $headers['Message-ID'] = $this->message_id; $this->customer_email = $this->conversation->customer_email; diff --git a/app/Misc/Mail.php b/app/Misc/Mail.php index cf7c0da14..ac4f21a67 100644 --- a/app/Misc/Mail.php +++ b/app/Misc/Mail.php @@ -459,6 +459,11 @@ public static function fetchMessageMarkerValue($body) return ''; } + public static function getMessageIdHash($thread_id) + { + return substr(md5($thread_id.config('app.key')), 0, 16); + } + /** * Detect autoresponder by headers. * https://github.com/jpmckinney/multi_mail/wiki/Detecting-autoresponders