diff --git a/application/Espo/Entities/Email.php b/application/Espo/Entities/Email.php index 1d37600e28..3d0e20ac35 100644 --- a/application/Espo/Entities/Email.php +++ b/application/Espo/Entities/Email.php @@ -207,6 +207,20 @@ class Email extends Entity return $this->hasInContainer('bodyPlain') && $this->getFromContainer('bodyPlain'); } + /** + * @since 9.0.0 + */ + public function getBodyPlainWithReplyPart(): ?string + { + $body = $this->getBodyPlain(); + + if (!$body) { + return null; + } + + return EmailUtil::stripBodyPlainQuotePart($body) ?: null; + } + public function getBodyPlain(): ?string { if ($this->getFromContainer('bodyPlain')) { diff --git a/application/Espo/Tools/Email/Util.php b/application/Espo/Tools/Email/Util.php index 4bd1150e96..1b55a57fa6 100644 --- a/application/Espo/Tools/Email/Util.php +++ b/application/Espo/Tools/Email/Util.php @@ -63,4 +63,35 @@ class Util return $string; } + + static public function stripBodyPlainQuotePart(string $body): string + { + if (!$body) { + return ''; + } + + $lines = preg_split("/\r\n|\n|\r/", $body); + + if (!is_array($lines)) { + return ''; + } + + $endIndex = count($lines) - 1; + + for ($i = count($lines) - 1; $i >= 0; $i--) { + $line = $lines[$i]; + + if (str_starts_with($line, '>')) { + $endIndex = $i; + + continue; + } + + break; + } + + $lines = array_slice($lines, 0, $endIndex); + + return implode("\r\n", $lines); + } } diff --git a/application/Espo/Tools/Stream/Service.php b/application/Espo/Tools/Stream/Service.php index 6cc0f712b1..a1a09b2ce2 100644 --- a/application/Espo/Tools/Stream/Service.php +++ b/application/Espo/Tools/Stream/Service.php @@ -506,7 +506,7 @@ class Service $withContent = $this->toStoreEmailContent($entityType); if ($withContent) { - $note->setPost($email->getBodyPlain()); + $note->setPost($email->getBodyPlainWithReplyPart()); $data['attachmentsIds'] = $email->getAttachmentIdList(); } @@ -563,7 +563,7 @@ class Service $withContent = $this->toStoreEmailContent($entityType); if ($withContent) { - $note->setPost($email->getBodyPlain()); + $note->setPost($email->getBodyPlainWithReplyPart()); $data['attachmentsIds'] = $email->getAttachmentIdList(); } diff --git a/tests/unit/Espo/Entities/EmailTest.php b/tests/unit/Espo/Entities/EmailTest.php index 2690360b84..40ab5ce04d 100644 --- a/tests/unit/Espo/Entities/EmailTest.php +++ b/tests/unit/Espo/Entities/EmailTest.php @@ -30,6 +30,7 @@ namespace tests\unit\Espo\Entities; use Espo\Entities\Email; +use Espo\Tools\Email\Util; class EmailTest extends \PHPUnit\Framework\TestCase { @@ -475,13 +476,24 @@ class EmailTest extends \PHPUnit\Framework\TestCase $this->assertEquals('test ', $body); } - function testBodyPlain() + public function testBodyPlain(): void { $this->email->set('body', '
 &'); $bodyPlain = $this->email->getBodyPlain(); $this->assertEquals("\r\n &", $bodyPlain); } + public function testBodyPlainWithoutQuotePart(): void + { + $body = "Test\nHello\n> Test\n> Test\r\n>> Test"; + + $this->email->set('bodyPlain', $body); + + $expected = "Test\r\nHello"; + + $this->assertEquals($expected, $this->email->getBodyPlainWithReplyPart()); + } + public function testSubjectBody(): void { $email = $this->email;