get('name'); } protected function _setSubject(?string $value): void { $this->set('name', $value); } /** * @return bool */ protected function _hasSubject(): bool { return $this->has('name'); } protected function _hasFromName(): bool { return $this->has('fromString'); } protected function _hasFromAddress(): bool { return $this->has('fromString'); } protected function _hasReplyToName(): bool { return $this->has('replyToString'); } protected function _hasReplyToAddress(): bool { return $this->has('replyToString'); } protected function _getFromName(): ?string { if (!$this->has('fromString')) { return null; } $string = EmailService::parseFromName($this->get('fromString')); if ($string === '') { return null; } return $string; } protected function _getFromAddress(): ?string { if (!$this->has('fromString')) { return null; } return EmailService::parseFromAddress($this->get('fromString')); } protected function _getReplyToName(): ?string { if (!$this->has('replyToString')) { return null; } $string = $this->get('replyToString'); if (!$string) { return null; } return EmailService::parseFromName( trim(explode(';', $string)[0]) ); } protected function _getReplyToAddress(): ?string { if (!$this->has('replyToString')) { return null; } $string = $this->get('replyToString'); if (!$string) { return null; } return EmailService::parseFromAddress( trim(explode(';', $string)[0]) ); } protected function _setIsRead(?bool $value): void { $this->setInContainer('isRead', $value !== false); if ($value === true || $value === false) { $this->setInContainer('isUsers', true); return; } $this->setInContainer('isUsers', false); } public function isManuallyArchived(): bool { return $this->get('status') === 'Archived' && $this->get('createdById') !== 'system'; } public function addAttachment(Attachment $attachment): void { if (!$this->id) { return; } $attachment->set('parentId', $this->id); $attachment->set('parentType', 'Email'); if (!$this->entityManager) { throw new RuntimeException(); } $this->entityManager->saveEntity($attachment); } protected function _getBodyPlain(): ?string { return $this->getBodyPlain(); } public function hasBodyPlain(): bool { return $this->hasInContainer('bodyPlain') && $this->getFromContainer('bodyPlain'); } public function getBodyPlain(): ?string { if ($this->getFromContainer('bodyPlain')) { return $this->getFromContainer('bodyPlain'); } /** @var string */ $body = $this->get('body') ?? ''; $breaks = ["
", "
", "
", "
", "<br />", "<br/>", "<br>"]; $body = str_ireplace($breaks, "\r\n", $body); $body = strip_tags($body); $reList = [ '&(quot|#34);', '&(amp|#38);', '&(lt|#60);', '&(gt|#62);', '&(nbsp|#160);', '&(iexcl|#161);', '&(cent|#162);', '&(pound|#163);', '&(copy|#169);', '&(reg|#174);', ]; $replaceList = [ '', '&', '<', '>', ' ', chr(161), chr(162), chr(163), chr(169), chr(174), ]; foreach ($reList as $i => $re) { /** @var string */ $body = mb_ereg_replace($re, $replaceList[$i], $body, 'i'); } return $body; } public function getBodyPlainForSending(): string { return $this->getBodyPlain() ?? ''; } public function getBodyForSending(): string { $body = $this->get('body') ?? ''; if (!empty($body)) { $attachmentList = $this->getInlineAttachmentList(); foreach ($attachmentList as $attachment) { $id = $attachment->getId(); $body = str_replace( "\"?entryPoint=attachment&id={$id}\"", "\"cid:{$id}\"", $body ); } } return str_replace( "", "
", $body ); } /** * @return Attachment[] */ public function getInlineAttachmentList(): array { $idList = []; $body = $this->get('body'); if (empty($body)) { return []; } $matches = []; if (!preg_match_all("/\?entryPoint=attachment&id=([^&=\"']+)/", $body, $matches)) { return []; } if (empty($matches[1]) || !is_array($matches[1])) { return []; } $attachmentList = []; foreach ($matches[1] as $id) { if (in_array($id, $idList)) { continue; } $idList[] = $id; if (!$this->entityManager) { throw new RuntimeException(); } /** @var Attachment|null */ $attachment = $this->entityManager->getEntity('Attachment', $id); if ($attachment) { $attachmentList[] = $attachment; } } return $attachmentList; } public function getDateSent(): ?DateTime { /** @var ?DateTime */ return $this->getValueObject('dateSent'); } public function getSubject(): ?string { return $this->get('subject'); } public function setSubject(?string $subject): self { $this->set('subject', $subject); return $this; } public function getBody(): ?string { return $this->get('body'); } public function setBody(?string $body): self { $this->set('body', $body); return $this; } public function isHtml(): ?bool { return $this->get('isHtml'); } public function setIsHtml(bool $isHtml = true): self { $this->set('isHtml', $isHtml); return $this; } public function setIsPlain(bool $isPlain = true): self { $this->set('isHtml', !$isPlain); return $this; } public function setFromAddress(?string $address): self { $this->set('from', $address); return $this; } public function addToAddress(string $address): self { $list = $this->getToAddressList(); $list[] = $address; $this->set('to', implode(';', $list)); return $this; } public function addCcAddress(string $address): self { $list = $this->getCcAddressList(); $list[] = $address; $this->set('cc', implode(';', $list)); return $this; } public function addBccAddress(string $address): self { $list = $this->getBccAddressList(); $list[] = $address; $this->set('bcc', implode(';', $list)); return $this; } public function addReplyToAddress(string $address): self { $list = $this->getReplyToAddressList(); $list[] = $address; $this->set('replyTo', implode(';', $list)); return $this; } public function getFromAddress(): ?string { if (!$this->hasInContainer('from') && !$this->isNew()) { $this->getEmailRepository()->loadFromField($this); } return $this->get('from'); } /** * @return string[] */ public function getToAddressList(): array { if (!$this->hasInContainer('to') && !$this->isNew()) { $this->getEmailRepository()->loadToField($this); } $value = $this->get('to'); if (!$value) { return []; } return explode(';', $value); } /** * @return string[] */ public function getCcAddressList(): array { if (!$this->hasInContainer('cc') && !$this->isNew()) { $this->getEmailRepository()->loadCcField($this); } $value = $this->get('cc'); if (!$value) { return []; } return explode(';', $value); } /** * @return string[] */ public function getBccAddressList(): array { if (!$this->hasInContainer('bcc') && !$this->isNew()) { $this->getEmailRepository()->loadBccField($this); } $value = $this->get('bcc'); if (!$value) { return []; } return explode(';', $value); } /** * @return string[] */ public function getReplyToAddressList(): array { if (!$this->hasInContainer('replyTo') && !$this->isNew()) { $this->getEmailRepository()->loadReplyToField($this); } $value = $this->get('replyTo'); if (!$value) { return []; } return explode(';', $value); } public function setDummyMessageId(): self { $this->set('messageId', 'dummy:' . Util::generateId()); return $this; } public function getMessageId(): ?string { return $this->get('messageId'); } public function getParent(): ?LinkParent { /** @var ?LinkParent */ return $this->getValueObject('parent'); } public function setParent(?LinkParent $parent): self { $this->setValueObject('parent', $parent); return $this; } public function getStatus(): ?string { return $this->get('status'); } public function getCreatedBy(): ?Link { /** @var ?Link */ return $this->getValueObject('createdBy'); } private function getEmailRepository(): EmailRepository { if (!$this->entityManager) { throw new RuntimeException(); } /** @var EmailRepository */ return $this->entityManager->getRepository(self::ENTITY_TYPE); } }