Initial xentral_oss_20.3.c9ffacf
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Components\MailClient\Data;
|
||||
|
||||
use Xentral\Components\MailClient\Exception\InvalidArgumentException;
|
||||
|
||||
class MailAttachmentData implements MailAttachmentInterface
|
||||
{
|
||||
/** @var string $filename */
|
||||
private $filename;
|
||||
|
||||
/** @var string $content */
|
||||
private $content;
|
||||
|
||||
/** @var string $contentType */
|
||||
private $contentType;
|
||||
|
||||
/** @var string $encoding */
|
||||
private $encoding;
|
||||
|
||||
/** @var bool $isInlineAttachment*/
|
||||
private $isInlineAttachment;
|
||||
|
||||
/** @var string|null $cid */
|
||||
private $cid;
|
||||
|
||||
/**
|
||||
* @param string $filename
|
||||
* @param string $content
|
||||
* @param string $contentType
|
||||
* @param string $encoding
|
||||
* @param bool $isInlineAttachment
|
||||
* @param string|null $cid
|
||||
*/
|
||||
public function __construct(
|
||||
string $filename,
|
||||
string $content,
|
||||
string $contentType,
|
||||
string $encoding,
|
||||
bool $isInlineAttachment = false,
|
||||
string $cid = null
|
||||
)
|
||||
{
|
||||
$this->filename = $filename;
|
||||
$this->content = $content;
|
||||
$this->contentType = $contentType;
|
||||
$this->encoding = $encoding;
|
||||
$this->isInlineAttachment = $isInlineAttachment;
|
||||
$this->cid = $cid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MailMessagePartInterface $part
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*
|
||||
* @return MailAttachmentData
|
||||
*/
|
||||
public static function fromMailMessagePart(MailMessagePartInterface $part): MailAttachmentData
|
||||
{
|
||||
$encodingHeader = $part->getHeader('content-transfer-encoding');
|
||||
if ($encodingHeader === null) {
|
||||
throw new InvalidArgumentException('missing header: "Content-Transfer-Encoding"');
|
||||
}
|
||||
$encoding = $encodingHeader->getValue();
|
||||
$dispositionHeader = $part->getHeader('content-disposition');
|
||||
if ($dispositionHeader === null) {
|
||||
throw new InvalidArgumentException('missing header: "Content-Disposition"');
|
||||
}
|
||||
$disposition = $dispositionHeader->getValue();
|
||||
if (!preg_match('/(.+);\s*filename="([^"]+)".*$/m', $disposition, $matches)) {
|
||||
throw new InvalidArgumentException(
|
||||
sprintf('unexpected header value "Content-Disposition" = %s', $disposition)
|
||||
);
|
||||
}
|
||||
$isInline = strtolower($matches[1]) === 'inline';
|
||||
$filename = $matches[2];
|
||||
$cid = null;
|
||||
$contentIdHeader = $part->getHeader('content-id');
|
||||
if ($contentIdHeader !== null) {
|
||||
$cid = $contentIdHeader->getValue();
|
||||
if (preg_match('/[<]?([^<>]+)[>]?$/', $cid, $cidMatches)) {
|
||||
$cid = $cidMatches[1];
|
||||
}
|
||||
}
|
||||
|
||||
return new self(
|
||||
$filename,
|
||||
$part->getContent(),
|
||||
$part->getContentType(),
|
||||
$encoding,
|
||||
$isInline,
|
||||
$cid
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getFileName(): string
|
||||
{
|
||||
return $this->filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getContent(): string
|
||||
{
|
||||
switch ($this->encoding) {
|
||||
case 'base64':
|
||||
return base64_decode($this->content);
|
||||
|
||||
default:
|
||||
return $this->content;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getContentType(): string
|
||||
{
|
||||
return $this->contentType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTransferEncoding(): string
|
||||
{
|
||||
return $this->encoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isInlineAttachment(): bool
|
||||
{
|
||||
return $this->isInlineAttachment;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getCid(): ?string
|
||||
{
|
||||
return $this->cid;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Components\MailClient\Data;
|
||||
|
||||
interface MailAttachmentInterface
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getFileName(): string;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getContent(): string;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getContentType(): string;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getTransferEncoding(): string;
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isInlineAttachment(): bool;
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getCid(): ?string;
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Components\MailClient\Data;
|
||||
|
||||
final class MailBoxInfoData
|
||||
{
|
||||
/** @var int $messages */
|
||||
private $messages;
|
||||
|
||||
/** @var int $recent */
|
||||
private $recent;
|
||||
|
||||
/** @var int $uidvalidity */
|
||||
private $uidvalidity;
|
||||
|
||||
/** @var array $flags */
|
||||
private $flags;
|
||||
|
||||
/**
|
||||
* @param int $messages
|
||||
* @param int $recent
|
||||
* @param int $uidvalidity
|
||||
* @param array $flags
|
||||
*/
|
||||
public function __construct(
|
||||
int $messages,
|
||||
int $recent,
|
||||
int $uidvalidity,
|
||||
array $flags = []
|
||||
)
|
||||
{
|
||||
$this->messages = $messages;
|
||||
$this->recent = $recent;
|
||||
$this->uidvalidity = $uidvalidity;
|
||||
$this->flags = $flags;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int total amount of messages
|
||||
*/
|
||||
public function getMessages(): int
|
||||
{
|
||||
return $this->messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int amount of recent messages
|
||||
*/
|
||||
public function getRecentMessages(): int
|
||||
{
|
||||
return $this->recent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getUidvalidity(): int
|
||||
{
|
||||
return $this->uidvalidity;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getFlags(): array
|
||||
{
|
||||
return $this->flags;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $flag
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasFlag(string $flag): bool
|
||||
{
|
||||
return array_key_exists($flag, $this->flags);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,389 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Components\MailClient\Data;
|
||||
|
||||
use DateTime;
|
||||
use DateTimeInterface;
|
||||
use JsonSerializable;
|
||||
use Throwable;
|
||||
use Xentral\Components\MailClient\Exception\InvalidArgumentException;
|
||||
use Xentral\Components\Mailer\Data\EmailRecipient;
|
||||
use Xentral\Components\Util\StringUtil;
|
||||
|
||||
final class MailMessageData implements MailMessageInterface, JsonSerializable
|
||||
{
|
||||
/** @var EmailRecipient $sender */
|
||||
private $sender;
|
||||
|
||||
/** @var EmailRecipient[] $recipients */
|
||||
private $recipients;
|
||||
|
||||
/** @var EmailRecipient[] $ccs */
|
||||
private $ccs;
|
||||
|
||||
/** @var array $flags */
|
||||
private $flags;
|
||||
|
||||
/** @var MailMessagePartInterface $contentPart */
|
||||
private $contentPart;
|
||||
|
||||
/** @var string $rawContent */
|
||||
private $rawContent;
|
||||
|
||||
/**
|
||||
* @param EmailRecipient $sender
|
||||
* @param array $recipients
|
||||
* @param array $ccs
|
||||
* @param array $flags
|
||||
* @param array $headers
|
||||
* @param string|null $content
|
||||
* @param array $parts
|
||||
* @param string|null $rawContent
|
||||
*/
|
||||
public function __construct(
|
||||
EmailRecipient $sender,
|
||||
array $recipients,
|
||||
array $ccs,
|
||||
array $flags,
|
||||
array $headers,
|
||||
?string $content,
|
||||
array $parts = [],
|
||||
?string $rawContent = null
|
||||
) {
|
||||
$this->sender = $sender;
|
||||
$this->recipients = $recipients;
|
||||
$this->ccs = $ccs;
|
||||
$this->flags = $flags;
|
||||
$this->rawContent = $rawContent;
|
||||
$this->contentPart = new MailMessagePartData($headers, $content, $parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*
|
||||
* @return MailMessageData
|
||||
*/
|
||||
public static function fromJsonArray(array $data): MailMessageData
|
||||
{
|
||||
if (!isset($data['sender'], $data['recipients'], $data['ccs'], $data['flags'])) {
|
||||
throw new InvalidArgumentException('Message data incomplete');
|
||||
}
|
||||
$sender = new EmailRecipient($data['sender']['email'], $data['sender']['name']);
|
||||
$recipients = [];
|
||||
foreach ($data['recipients'] as $recipientArray) {
|
||||
$recipients[] = new EmailRecipient($recipientArray['email'], $recipientArray['name']);
|
||||
}
|
||||
$ccs = [];
|
||||
foreach ($data['ccs'] as $ccArray) {
|
||||
$ccs[] = new EmailRecipient($ccArray['email'], $ccArray['name']);
|
||||
}
|
||||
$raw = isset($data['raw']) ? $data['raw'] : null;
|
||||
$contentPart = isset($data['content'])
|
||||
? MailMessagePartData::fromJsonArray($data['content'])
|
||||
: null;
|
||||
$message = new self(
|
||||
$sender,
|
||||
$recipients,
|
||||
$ccs,
|
||||
$data['flags'],
|
||||
[],
|
||||
null,
|
||||
[],
|
||||
$raw
|
||||
);
|
||||
$message->contentPart = $contentPart;
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getFlags(): array
|
||||
{
|
||||
return $this->flags;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $flag
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasFlag(string $flag): bool
|
||||
{
|
||||
return array_key_exists($flag, $this->flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isMultipart(): bool
|
||||
{
|
||||
$contentType = $this->getContentType();
|
||||
if ($contentType === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return StringUtil::startsWith($contentType, 'multipart/');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getContentType(): string
|
||||
{
|
||||
return $this->contentPart->getContentType();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MailAttachmentInterface[]
|
||||
*/
|
||||
public function getAttachments(): array
|
||||
{
|
||||
$parts = [];
|
||||
$this->findAttachmentParts($this->contentPart, $parts);
|
||||
$attachments = [];
|
||||
foreach ($parts as $part) {
|
||||
$attachments[] = MailAttachmentData::fromMailMessagePart($part);
|
||||
}
|
||||
|
||||
return $attachments;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MailMessagePartInterface $part
|
||||
* @param array $resultArray
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function findAttachmentParts(MailMessagePartInterface $part, array &$resultArray): void
|
||||
{
|
||||
try {
|
||||
$header = $part->getHeader('content-disposition');
|
||||
$split = explode(';', $header->getValue());
|
||||
if ($split[0] === 'attachment' || $split[0] === 'inline') {
|
||||
$resultArray[] = $part;
|
||||
|
||||
return;
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
for ($i = 0; $i < $part->countParts(); $i++) {
|
||||
$this->findAttachmentParts($part->getPart($i), $resultArray);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @return MailMessageHeaderValue[]|[]
|
||||
*/
|
||||
public function getHeaders(): array
|
||||
{
|
||||
return $this->contentPart->getHeaders();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getContent(): ?string
|
||||
{
|
||||
return $this->contentPart->getContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getDecodedContent(): ?string
|
||||
{
|
||||
$this->contentPart->getDecodedContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $index
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @return MailMessagePartInterface
|
||||
*/
|
||||
public function getPart(int $index): MailMessagePartInterface
|
||||
{
|
||||
return $this->contentPart->getPart($index);
|
||||
}
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function countParts(): int
|
||||
{
|
||||
return $this->contentPart->countParts();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getHtmlBody(): ?string
|
||||
{
|
||||
$part = $this->findPartByContentType($this->contentPart, 'text/html');
|
||||
if ($part === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $part->getDecodedContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MailMessagePartInterface $part
|
||||
* @param string $contentType
|
||||
*
|
||||
* @return MailMessagePartInterface|null
|
||||
*/
|
||||
private function findPartByContentType(
|
||||
MailMessagePartInterface $part,
|
||||
string $contentType
|
||||
): ?MailMessagePartInterface {
|
||||
if ($part->getContentType() === $contentType) {
|
||||
return $part;
|
||||
}
|
||||
for ($i = 0; $i < $part->countParts(); $i++) {
|
||||
$subPart = $this->findPartByContentType($part->getPart($i), $contentType);
|
||||
if ($subPart !== null) {
|
||||
return $subPart;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getPlainTextBody(): ?string
|
||||
{
|
||||
$part = $this->findPartByContentType($this->contentPart, 'text/plain');
|
||||
if ($part === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $part->getDecodedContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSubject(): string
|
||||
{
|
||||
$subject = $this->getHeader('subject');
|
||||
if ($subject === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $subject->getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @codeCoverageIgnore
|
||||
*
|
||||
* @return MailMessageHeaderValue|null
|
||||
*/
|
||||
public function getHeader(string $name): ?MailMessageHeaderValue
|
||||
{
|
||||
return $this->contentPart->getHeader($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return DateTimeInterface|null
|
||||
*/
|
||||
public function getDate(): ?DateTimeInterface
|
||||
{
|
||||
$date = $this->getHeader('date');
|
||||
if ($date === null) {
|
||||
return null;
|
||||
}
|
||||
$dateTime = DateTime::createFromFormat(DateTimeInterface::RFC2822, $date->getValue());
|
||||
if ($dateTime === false) {
|
||||
$dateTime = DateTime::createFromFormat(DateTimeInterface::RFC822, $date->getValue());
|
||||
}
|
||||
if ($dateTime === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $dateTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return EmailRecipient
|
||||
*/
|
||||
public function getSender(): EmailRecipient
|
||||
{
|
||||
return $this->sender;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getReplyToAddress(): string
|
||||
{
|
||||
$header = $this->getHeader('return-path');
|
||||
if ($header === null) {
|
||||
return $this->sender->getEmail();
|
||||
}
|
||||
$address = $header->getValue();
|
||||
if (preg_match('/(.*)<(.+@.+)>/', $address, $matches)) {
|
||||
$address = $matches[2];
|
||||
}
|
||||
|
||||
return $address;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return EmailRecipient[]
|
||||
*/
|
||||
public function getRecipients(): array
|
||||
{
|
||||
return $this->recipients;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return EmailRecipient[]
|
||||
*/
|
||||
public function getCcRecipients(): array
|
||||
{
|
||||
return $this->ccs;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getRawContent(): ?string
|
||||
{
|
||||
if ($this->rawContent === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $this->rawContent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
'sender' => $this->sender,
|
||||
'recipients' => $this->recipients,
|
||||
'ccs' => $this->ccs,
|
||||
'flags' => $this->flags,
|
||||
'content' => $this->contentPart,
|
||||
'raw' => $this->rawContent,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Components\MailClient\Data;
|
||||
|
||||
interface MailMessageHeaderInterface
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getValue(): string;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getEncoding(): string;
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Components\MailClient\Data;
|
||||
|
||||
use JsonSerializable;
|
||||
use Xentral\Components\MailClient\Exception\InvalidArgumentException;
|
||||
|
||||
final class MailMessageHeaderValue implements MailMessageHeaderInterface, JsonSerializable
|
||||
{
|
||||
/** @var string $name */
|
||||
private $name;
|
||||
|
||||
/** @var string $value */
|
||||
private $value;
|
||||
|
||||
/** @var string $encoding */
|
||||
private $encoding;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $value
|
||||
* @param string $encoding
|
||||
*/
|
||||
public function __construct(string $name, string $value, string $encoding)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->value = $value;
|
||||
$this->encoding = $encoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*
|
||||
* @return MailMessageHeaderValue
|
||||
*/
|
||||
public static function fromJsonArray(array $data): MailMessageHeaderValue
|
||||
{
|
||||
if (!isset($data['name'], $data['value'], $data['encoding'])) {
|
||||
throw new InvalidArgumentException('Header incomplete');
|
||||
}
|
||||
|
||||
return new self($data['name'], $data['value'], $data['encoding']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getValue(): string
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getEncoding(): string
|
||||
{
|
||||
return $this->encoding;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
'name' => $this->name,
|
||||
'value' => $this->value,
|
||||
'encoding' => $this->encoding,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Components\MailClient\Data;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Xentral\Components\Mailer\Data\EmailRecipient;
|
||||
|
||||
interface MailMessageInterface extends MailMessagePartInterface
|
||||
{
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getFlags(): array;
|
||||
|
||||
/**
|
||||
* @param string $flag
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasFlag(string $flag): bool;
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getHtmlBody(): ?string;
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getPlainTextBody(): ?string;
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getRawContent(): ?string;
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getSubject(): ?string;
|
||||
|
||||
/**
|
||||
* @return DateTimeInterface|null
|
||||
*/
|
||||
public function getDate(): ?DateTimeInterface;
|
||||
|
||||
/**
|
||||
* @return EmailRecipient
|
||||
*/
|
||||
public function getSender(): EmailRecipient;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getReplyToAddress(): string;
|
||||
|
||||
/**
|
||||
* @return EmailRecipient[]
|
||||
*/
|
||||
public function getRecipients(): array;
|
||||
|
||||
/**
|
||||
* @return EmailRecipient[]
|
||||
*/
|
||||
public function getCcRecipients(): array;
|
||||
|
||||
/**
|
||||
* @return MailAttachmentInterface[]
|
||||
*/
|
||||
public function getAttachments(): array;
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Components\MailClient\Data;
|
||||
|
||||
use JsonSerializable;
|
||||
use Xentral\Components\MailClient\Exception\InvalidArgumentException;
|
||||
use Xentral\Components\Util\StringUtil;
|
||||
|
||||
final class MailMessagePartData implements MailMessagePartInterface, JsonSerializable
|
||||
{
|
||||
/** @var MailMessageHeaderValue[] $headers */
|
||||
private $headers;
|
||||
|
||||
/** @var MailMessagePartInterface[] $parts */
|
||||
private $parts;
|
||||
|
||||
/** @var string|null $content */
|
||||
private $content;
|
||||
|
||||
/**
|
||||
* @param array $headers
|
||||
* @param string|null $content
|
||||
* @param array $parts
|
||||
*/
|
||||
public function __construct(
|
||||
array $headers,
|
||||
?string $content,
|
||||
array $parts
|
||||
) {
|
||||
$headers = array_change_key_case($headers, CASE_LOWER);
|
||||
$this->headers = $headers;
|
||||
$this->content = $content;
|
||||
$this->parts = [];
|
||||
foreach ($parts as $part) {
|
||||
$this->addPart($part);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*
|
||||
* @return MailMessagePartData
|
||||
*/
|
||||
public static function fromJsonArray(array $data): MailMessagePartData
|
||||
{
|
||||
if (!array_key_exists('headers', $data)) {
|
||||
throw new InvalidArgumentException('Headers required');
|
||||
}
|
||||
if (!array_key_exists('content', $data)) {
|
||||
throw new InvalidArgumentException('content required');
|
||||
}
|
||||
if (!array_key_exists('parts', $data)) {
|
||||
throw new InvalidArgumentException('Message parts required');
|
||||
}
|
||||
|
||||
$headers = [];
|
||||
foreach ($data['headers'] as $header) {
|
||||
$headerValue = MailMessageHeaderValue::fromJsonArray($header);
|
||||
$headers[$headerValue->getName()] = $headerValue;
|
||||
}
|
||||
|
||||
$part = new self($headers, $data['content'], []);
|
||||
foreach ($data['parts'] as $subPart) {
|
||||
$part->addPart(self::fromJsonArray($subPart));
|
||||
}
|
||||
|
||||
return $part;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getHeader(string $name): ?MailMessageHeaderValue
|
||||
{
|
||||
if (!isset($this->headers[strtolower($name)])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->headers[strtolower($name)];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getContentType(): string
|
||||
{
|
||||
$header = $this->getHeader('content-type');
|
||||
if ($header === null) {
|
||||
return '';
|
||||
}
|
||||
$split = preg_split('/;/', $header->getValue(), -1, 0);
|
||||
|
||||
return $split[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getHeaders(): array
|
||||
{
|
||||
return $this->headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getContent(): ?string
|
||||
{
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getDecodedContent(): ?string
|
||||
{
|
||||
if ($this->content === null) {
|
||||
return null;
|
||||
}
|
||||
$encodingHeader = $this->getHeader('content-transfer-encoding');
|
||||
if ($encodingHeader === null ) {
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
return $this->decode($this->content, $encodingHeader->getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getPart(int $index): MailMessagePartInterface
|
||||
{
|
||||
return $this->parts[$index];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function countParts(): int
|
||||
{
|
||||
return count($this->parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isMultipart(): bool
|
||||
{
|
||||
$contentType = $this->getContentType();
|
||||
if ($contentType === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return StringUtil::startsWith($contentType, 'multipart/');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param MailMessagePartInterface $part
|
||||
*/
|
||||
private function addPart(MailMessagePartInterface $part): void
|
||||
{
|
||||
$this->parts[] = $part;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $content
|
||||
* @param string $encoding
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function decode(string $content, string $encoding): string
|
||||
{
|
||||
switch (strtolower($encoding)) {
|
||||
case 'quoted-printable':
|
||||
return quoted_printable_decode($content);
|
||||
//no break
|
||||
|
||||
case 'base64':
|
||||
return base64_decode($content);
|
||||
//no break
|
||||
|
||||
// default includes 7bit, 8bit and binary
|
||||
default:
|
||||
return $content;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
'headers' => $this->headers,
|
||||
'content' => $this->content,
|
||||
'parts' => $this->parts,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Components\MailClient\Data;
|
||||
|
||||
interface MailMessagePartInterface
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getContentType(): string;
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isMultipart(): bool;
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @return MailMessageHeaderValue|null
|
||||
*/
|
||||
public function getHeader(string $name): ?MailMessageHeaderValue;
|
||||
|
||||
/**
|
||||
* @return MailMessageHeaderValue[]|[]
|
||||
*/
|
||||
public function getHeaders(): array;
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getContent(): ?string;
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getDecodedContent(): ?string;
|
||||
|
||||
/**
|
||||
* @param int $index
|
||||
*
|
||||
* @return MailMessagePartInterface
|
||||
*/
|
||||
public function getPart(int $index): MailMessagePartInterface;
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function countParts(): int;
|
||||
}
|
||||
Reference in New Issue
Block a user