email methods

This commit is contained in:
Yuri Kuznetsov
2021-09-10 21:50:19 +03:00
parent 4fdcdec802
commit b2d6bd58e6
3 changed files with 172 additions and 59 deletions

View File

@@ -41,7 +41,9 @@ class Email extends Entity
public const ENTITY_TYPE = 'Email';
public const STATUS_BEING_IMPORTED = 'Being Imported';
public const STATUS_ARCHIVED = 'Archived';
public const STATUS_SENT = 'Sent';
protected function _getSubject()
@@ -115,9 +117,9 @@ class Email extends Entity
return null;
}
$string = trim(explode(';', $string)[0]);
return EmailService::parseFromName($string);
return EmailService::parseFromName(
trim(explode(';', $string)[0])
);
}
protected function _getReplyToAddress()
@@ -132,9 +134,9 @@ class Email extends Entity
return null;
}
$string = trim(explode(';', $string)[0]);
return EmailService::parseFromAddress($string);
return EmailService::parseFromAddress(
trim(explode(';', $string)[0])
);
}
protected function _setIsRead($value)
@@ -143,10 +145,11 @@ class Email extends Entity
if ($value === true || $value === false) {
$this->setInContainer('isUsers', true);
return;
}
else {
$this->setInContainer('isUsers', false);
}
$this->setInContainer('isUsers', false);
}
public function isManuallyArchived(): bool
@@ -201,6 +204,7 @@ class Email extends Entity
'&(copy|#169);',
'&(reg|#174);',
];
$replaceList = [
'',
'&',
@@ -221,36 +225,39 @@ class Email extends Entity
return $body;
}
public function getBodyPlainForSending()
public function getBodyPlainForSending(): string
{
return $this->getBodyPlain();
return $this->getBodyPlain() ?? '';
}
public function getBodyForSending()
public function getBodyForSending(): string
{
$body = $this->get('body');
$body = $this->get('body') ?? '';
if (!empty($body)) {
$attachmentList = $this->getInlineAttachments();
foreach ($attachmentList as $attachment) {
$id = $attachment->getId();
$body = str_replace(
"\"?entryPoint=attachment&id={$attachment->id}\"",
"\"cid:{$attachment->id}\"",
"\"?entryPoint=attachment&id={$id}\"",
"\"cid:{$id}\"",
$body
);
}
}
$body = str_replace(
return str_replace(
"<table class=\"table table-bordered\">",
"<table class=\"table table-bordered\" width=\"100%\">",
$body
);
return $body;
}
/**
* @return Attachment[]
*/
public function getInlineAttachments(): array
{
$idList = [];
@@ -315,76 +322,111 @@ class Email extends Entity
$this->set('body', $body);
}
public function isHtml(): ?bool
{
return $this->get('isHtml');
}
public function setIsHtml(bool $isHtml = true): void
{
$this->set('isHtml', $isHtml);
}
public function setIsPlain(bool $isPlain = true): void
{
$this->set('isHtml', !$isPlain);
}
public function addToAddress(string $address): void
{
$list = $this->getToAddressList();
$list[] = $address;
$this->set('to', implode(';', $list));
}
public function addCcAddress(string $address): void
{
$list = $this->getCcAddressList();
$list[] = $address;
$this->set('cc', implode(';', $list));
}
public function addBccAddress(string $address): void
{
$list = $this->getBccAddressList();
$list[] = $address;
$this->set('bcc', implode(';', $list));
}
public function addReplyToAddress(string $address): void
{
$list = $this->getReplyToAddressList();
$list[] = $address;
$this->set('replyTo', implode(';', $list));
}
/**
* @return string[]
*/
public function getToList(): array
public function getToAddressList(): array
{
$value = $this->get('to');
if ($value) {
$arr = explode(';', $value);
if (is_array($arr)) {
return $arr;
}
if (!$value) {
return [];
}
return [];
return explode(';', $value);
}
/**
* @return string[]
*/
public function getCcList(): array
public function getCcAddressList(): array
{
$value = $this->get('cc');
if ($value) {
$arr = explode(';', $value);
if (is_array($arr)) {
return $arr;
}
if (!$value) {
return [];
}
return [];
return explode(';', $value);
}
/**
* @return string[]
*/
public function getBccList(): array
public function getBccAddressList(): array
{
$value = $this->get('bcc');
if ($value) {
$arr = explode(';', $value);
if (is_array($arr)) {
return $arr;
}
if (!$value) {
return [];
}
return [];
return explode(';', $value);
}
/**
* @return string[]
*/
public function getReplyToList(): array
public function getReplyToAddressList(): array
{
$value = $this->get('replyTo');
if ($value) {
$arr = explode(';', $value);
if (is_array($arr)) {
return $arr;
}
if (!$value) {
return [];
}
return [];
return explode(';', $value);
}
public function setDummyMessageId(): void

View File

@@ -392,19 +392,19 @@ class Email extends Record implements
}
}
foreach ($entity->getToList() as $address) {
foreach ($entity->getToAddressList() as $address) {
if (!filter_var($address, \FILTER_VALIDATE_EMAIL)) {
throw new Error('To email address is not valid.');
}
}
foreach ($entity->getCcList() as $address) {
foreach ($entity->getCcAddressList() as $address) {
if (!filter_var($address, \FILTER_VALIDATE_EMAIL)) {
throw new Error('CC email address is not valid.');
}
}
foreach ($entity->getBccList() as $address) {
foreach ($entity->getBccAddressList() as $address) {
if (!filter_var($address, \FILTER_VALIDATE_EMAIL)) {
throw new Error('BCC email address is not valid.');
}

View File

@@ -29,12 +29,15 @@
namespace tests\unit\Espo\Entities;
use tests\unit\ReflectionHelper;
use \Espo\Entities\Email;
use Espo\Entities\Email;
class EmailTest extends \PHPUnit\Framework\TestCase
{
/**
* @var Email
*/
private $email;
// TODO defs test helper
protected $defs = array(
'fields' =>
@@ -85,6 +88,12 @@ class EmailTest extends \PHPUnit\Framework\TestCase
'notStorable' => true,
'len' => 255,
),
'replyTo' =>
array (
'type' => 'varchar',
'notStorable' => true,
'len' => 255,
),
'emailAddress' =>
array (
'type' => 'base',
@@ -418,7 +427,7 @@ class EmailTest extends \PHPUnit\Framework\TestCase
$this->entityManager = $this->getMockBuilder('\Espo\Core\ORM\EntityManager')->disableOriginalConstructor()->getMock();
$this->repository =
$this->getMockBuilder('\Espo\Core\ORM\Repositories\Database')->disableOriginalConstructor()->getMock();
$this->getMockBuilder('Espo\Core\ORM\Repositories\Database')->disableOriginalConstructor()->getMock();
$this->entityManager->expects($this->any())
->method('getRepository')
@@ -449,8 +458,11 @@ class EmailTest extends \PHPUnit\Framework\TestCase
function testGetBodyForSending()
{
$attachment =
$this->getMockBuilder('\Espo\Entities\Attachment')->disableOriginalConstructor()->getMock();
$attachment->id = 'Id01';
$this->getMockBuilder('Espo\Entities\Attachment')->disableOriginalConstructor()->getMock();
$attachment
->method('getId')
->willReturn('Id01');
$this->email->set('body', 'test <img src="?entryPoint=attachment&amp;id=Id01">');
@@ -469,4 +481,63 @@ class EmailTest extends \PHPUnit\Framework\TestCase
$bodyPlain = $this->email->getBodyPlain();
$this->assertEquals("\r\n &", $bodyPlain);
}
public function testSubjectBody(): void
{
$email = $this->email;
$email->setSubject('1');
$email->setBody('2');
$email->setIsHtml();
$this->assertEquals('1', $email->getSubject());
$this->assertEquals('2', $email->getBody());
$this->assertEquals(true, $email->isHtml());
}
public function testPlain(): void
{
$email = $this->email;
$email->setIsPlain();
$this->assertEquals(false, $email->isHtml());
}
public function testAddressList(): void
{
$email = $this->email;
$email->addToAddress('test1@test.com');
$email->addToAddress('test2@test.com');
$this->assertEquals(
['test1@test.com', 'test2@test.com'],
$email->getToAddressList()
);
$email->addCcAddress('test3@test.com');
$email->addCcAddress('test4@test.com');
$this->assertEquals(
['test3@test.com', 'test4@test.com'],
$email->getCcAddressList()
);
$email->addBccAddress('test5@test.com');
$email->addBccAddress('test6@test.com');
$this->assertEquals(
['test5@test.com', 'test6@test.com'],
$email->getBccAddressList()
);
$email->addReplyToAddress('test7@test.com');
$email->addReplyToAddress('test8@test.com');
$this->assertEquals(
['test7@test.com', 'test8@test.com'],
$email->getReplyToAddressList()
);
}
}