From f1839bcfbf13a3f4bfa110037b939c551fa36abc Mon Sep 17 00:00:00 2001 From: FreeScout Date: Mon, 11 Dec 2023 10:23:17 -0800 Subject: [PATCH] Fix user reply to a notification being incorrectly cropped - closes #3580 --- app/Console/Commands/FetchEmails.php | 18 ++++++++++++++---- app/Misc/Mail.php | 1 + .../emails/customer/reply_fancy.blade.php | 2 +- .../views/emails/user/notification.blade.php | 2 +- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/app/Console/Commands/FetchEmails.php b/app/Console/Commands/FetchEmails.php index e24383fea..c1679d383 100644 --- a/app/Console/Commands/FetchEmails.php +++ b/app/Console/Commands/FetchEmails.php @@ -593,12 +593,15 @@ public function processMessage($message, $message_id, $mailbox, $mailboxes, $ext // Get body and do not replace :cid with images base64 $html_body = $message->getHTMLBody(false); } + $is_html = true; if ($html_body) { - $body = $this->separateReply($html_body, true, $is_reply); + $body = $html_body; } else { + $is_html = false; $body = $message->getTextBody(); - $body = $this->separateReply($body, false, $is_reply); } + $body = $this->separateReply($body, $is_html, $is_reply, !$message_from_customer); + // We have to fetch absolutely all emails, even with empty body. // if (!$body) { // $this->logError('Message body is empty'); @@ -1230,7 +1233,7 @@ public function processAttachmentName($name) * * @return string */ - public function separateReply($body, $is_html, $is_reply) + public function separateReply($body, $is_html, $is_reply, $user_reply_to_notification = false) { $cmp_reply_length_desc = function ($a, $b) { if (mb_strlen($a) == mb_strlen($b)) { @@ -1280,12 +1283,19 @@ public function separateReply($body, $is_html, $is_reply) if ($is_reply) { // Check all separators and choose the shortest reply $reply_bodies = []; + $reply_separators = Mail::$alternative_reply_separators; if (!empty($this->mailbox->before_reply)) { $reply_separators[] = $this->mailbox->before_reply; } + // If user replied to the email notification use only predefined reply separator. + // https://github.com/freescout-helpdesk/freescout/issues/3580 + if ($user_reply_to_notification && strstr($result, \MailHelper::REPLY_SEPARATOR_NOTIFICATION)) { + $reply_separators = [\MailHelper::REPLY_SEPARATOR_NOTIFICATION]; + } + foreach ($reply_separators as $reply_separator) { if (\Str::startsWith($reply_separator, 'regex:')) { $regex = preg_replace("/^regex:/", '', $reply_separator); @@ -1294,7 +1304,7 @@ public function separateReply($body, $is_html, $is_reply) $parts = explode($reply_separator, $result); } if (count($parts) > 1) { - // Check if past contains any real text. + // Check if part contains any real text. $text = \Helper::htmlToText($parts[0]); $text = trim($text); $text = preg_replace('/^\s+/mu', '', $text); diff --git a/app/Misc/Mail.php b/app/Misc/Mail.php index 0515982cb..825d20c3b 100644 --- a/app/Misc/Mail.php +++ b/app/Misc/Mail.php @@ -15,6 +15,7 @@ class Mail */ const REPLY_SEPARATOR_HTML = 'fsReplyAbove'; const REPLY_SEPARATOR_TEXT = '-- Please reply above this line --'; + const REPLY_SEPARATOR_NOTIFICATION = 'fsNotificationReplyAbove'; /** * Message-ID prefixes for outgoing emails. diff --git a/resources/views/emails/customer/reply_fancy.blade.php b/resources/views/emails/customer/reply_fancy.blade.php index ea00d8a1f..831896c1e 100644 --- a/resources/views/emails/customer/reply_fancy.blade.php +++ b/resources/views/emails/customer/reply_fancy.blade.php @@ -10,7 +10,7 @@ -
+
@php $is_forwarded = !empty($threads[0]) ? $threads[0]->isForwarded() : false; @endphp diff --git a/resources/views/emails/user/notification.blade.php b/resources/views/emails/user/notification.blade.php index 5c6007512..953573b13 100644 --- a/resources/views/emails/user/notification.blade.php +++ b/resources/views/emails/user/notification.blade.php @@ -22,7 +22,7 @@ - +