email note strip reply part

This commit is contained in:
Yuri Kuznetsov
2024-11-16 16:10:26 +02:00
parent c0a13f79df
commit b3606ee35a
4 changed files with 60 additions and 3 deletions
+14
View File
@@ -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')) {
+31
View File
@@ -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);
}
}
+2 -2
View File
@@ -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();
}
+13 -1
View File
@@ -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 <img src="cid:Id01">', $body);
}
function testBodyPlain()
public function testBodyPlain(): void
{
$this->email->set('body', '<br />&nbsp;&amp;');
$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;