Skip to content

Commit

Permalink
Fix an issue when customer was using FreeScout and reply sometimes co…
Browse files Browse the repository at this point in the history
…uld be connected to the wrong conversation - closes #2375
  • Loading branch information
freescout-help-desk committed Dec 11, 2022
1 parent 47e7162 commit 5e38dc9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
31 changes: 25 additions & 6 deletions app/Console/Commands/FetchEmails.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/SendAutoReply.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/SendReplyToCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions app/Misc/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5e38dc9

Please sign in to comment.