feat: message send
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -16,6 +16,142 @@ use KTXF\Mail\Object\MessagePropertiesMutableAbstract;
|
||||
*/
|
||||
class MessageProperties extends MessagePropertiesMutableAbstract {
|
||||
|
||||
private function buildJmapBodyStructure(array &$bodyValues): ?array {
|
||||
$counter = 0;
|
||||
$body = isset($this->data[static::PROPERTY_BODY]) && is_array($this->data[static::PROPERTY_BODY])
|
||||
? $this->serializeBodyPartForJmap($this->data[static::PROPERTY_BODY], $bodyValues, $counter)
|
||||
: null;
|
||||
|
||||
$attachments = [];
|
||||
if (isset($this->data[static::PROPERTY_ATTACHMENTS]) && is_array($this->data[static::PROPERTY_ATTACHMENTS])) {
|
||||
foreach ($this->data[static::PROPERTY_ATTACHMENTS] as $attachment) {
|
||||
if (!is_array($attachment)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$serialized = $this->serializeBodyPartForJmap($attachment, $bodyValues, $counter);
|
||||
if ($serialized !== null) {
|
||||
$attachments[] = $serialized;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($body === null) {
|
||||
if ($attachments === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (count($attachments) === 1) {
|
||||
return $attachments[0];
|
||||
}
|
||||
|
||||
return [
|
||||
'type' => 'multipart/mixed',
|
||||
'subParts' => $attachments,
|
||||
];
|
||||
}
|
||||
|
||||
if ($attachments === []) {
|
||||
return $body;
|
||||
}
|
||||
|
||||
return [
|
||||
'type' => 'multipart/mixed',
|
||||
'subParts' => array_merge([$body], $attachments),
|
||||
];
|
||||
}
|
||||
|
||||
private function serializeBodyPartForJmap(array $part, array &$bodyValues, int &$counter): ?array {
|
||||
$type = isset($part['type']) && is_string($part['type']) && $part['type'] !== ''
|
||||
? strtolower($part['type'])
|
||||
: null;
|
||||
|
||||
if ($type === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$isMultipart = str_starts_with($type, 'multipart/');
|
||||
$serialized = [
|
||||
'type' => $type,
|
||||
];
|
||||
|
||||
if (isset($part['name']) && is_string($part['name']) && $part['name'] !== '') {
|
||||
$serialized['name'] = $part['name'];
|
||||
}
|
||||
|
||||
if (isset($part['disposition']) && is_string($part['disposition']) && $part['disposition'] !== '') {
|
||||
$serialized['disposition'] = $part['disposition'];
|
||||
}
|
||||
|
||||
if (isset($part['cid']) && is_string($part['cid']) && $part['cid'] !== '') {
|
||||
$serialized['cid'] = $part['cid'];
|
||||
}
|
||||
|
||||
if (isset($part['language']) && is_array($part['language']) && $part['language'] !== []) {
|
||||
$serialized['language'] = $part['language'];
|
||||
}
|
||||
|
||||
if (isset($part['location']) && is_string($part['location']) && $part['location'] !== '') {
|
||||
$serialized['location'] = $part['location'];
|
||||
}
|
||||
|
||||
if ($isMultipart) {
|
||||
$subParts = [];
|
||||
foreach ($part['subParts'] ?? [] as $subPart) {
|
||||
if (!is_array($subPart)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$serializedSubPart = $this->serializeBodyPartForJmap($subPart, $bodyValues, $counter);
|
||||
if ($serializedSubPart !== null) {
|
||||
$subParts[] = $serializedSubPart;
|
||||
}
|
||||
}
|
||||
|
||||
if ($subParts !== []) {
|
||||
$serialized['subParts'] = $subParts;
|
||||
}
|
||||
|
||||
return $serialized;
|
||||
}
|
||||
|
||||
$blobId = isset($part['blobId']) && is_string($part['blobId']) && $part['blobId'] !== ''
|
||||
? $part['blobId']
|
||||
: null;
|
||||
$content = isset($part['content']) && is_string($part['content']) ? $part['content'] : null;
|
||||
|
||||
if ($blobId !== null) {
|
||||
$serialized['blobId'] = $blobId;
|
||||
|
||||
if (isset($part['charset']) && is_string($part['charset']) && $part['charset'] !== '' && str_starts_with($type, 'text/')) {
|
||||
$serialized['charset'] = $part['charset'];
|
||||
}
|
||||
|
||||
if (isset($part['size']) && is_int($part['size'])) {
|
||||
$serialized['size'] = $part['size'];
|
||||
}
|
||||
|
||||
return $serialized;
|
||||
}
|
||||
|
||||
if ($content !== null && str_starts_with($type, 'text/')) {
|
||||
$partId = isset($part['partId']) && is_string($part['partId']) && $part['partId'] !== ''
|
||||
? $part['partId']
|
||||
: 'part-' . (++$counter);
|
||||
|
||||
$serialized['partId'] = $partId;
|
||||
$bodyValues[$partId] = [
|
||||
'value' => $content,
|
||||
'isEncodingProblem' => false,
|
||||
'isTruncated' => false,
|
||||
];
|
||||
|
||||
return $serialized;
|
||||
}
|
||||
|
||||
return $serialized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert JMAP parameters array to mail message properties object
|
||||
*
|
||||
@@ -162,11 +298,11 @@ class MessageProperties extends MessagePropertiesMutableAbstract {
|
||||
if (isset($this->data['size'])) {
|
||||
$parameters['size'] = $this->data['size'];
|
||||
}
|
||||
if (isset($this->data['receivedDate'])) {
|
||||
$parameters['receivedAt'] = $this->data['receivedDate'];
|
||||
if (isset($this->data['received'])) {
|
||||
$parameters['receivedAt'] = $this->data['received'];
|
||||
}
|
||||
if (isset($this->data['date'])) {
|
||||
$parameters['sentAt'] = $this->data['date'];
|
||||
if (isset($this->data['sent'])) {
|
||||
$parameters['sentAt'] = $this->data['sent'];
|
||||
}
|
||||
if (isset($this->data['inReplyTo'])) {
|
||||
$parameters['inReplyTo'] = $this->data['inReplyTo'];
|
||||
@@ -239,37 +375,17 @@ class MessageProperties extends MessagePropertiesMutableAbstract {
|
||||
$parameters['keywords'][$keyword] = $value;
|
||||
}
|
||||
}
|
||||
if (isset($this->data['bodyStructure'])) {
|
||||
$parameters['bodyStructure'] = $this->data['bodyStructure'];
|
||||
}
|
||||
if (isset($this->data['body'])) {
|
||||
$parameters['bodyValues'] = [];
|
||||
|
||||
if (isset($this->data['body']['text']['content'])) {
|
||||
$parameters['bodyValues']['0'] = [
|
||||
'value' => $this->data['body']['text']['content'],
|
||||
'isEncodingProblem' => false,
|
||||
'isTruncated' => false
|
||||
];
|
||||
}
|
||||
|
||||
if (isset($this->data['body']['html']['content'])) {
|
||||
$parameters['bodyValues']['1'] = [
|
||||
'value' => $this->data['body']['html']['content'],
|
||||
'isEncodingProblem' => false,
|
||||
'isTruncated' => false
|
||||
];
|
||||
}
|
||||
|
||||
$bodyValues = [];
|
||||
$bodyStructure = $this->buildJmapBodyStructure($bodyValues);
|
||||
if ($bodyStructure !== null) {
|
||||
$parameters['bodyStructure'] = $bodyStructure;
|
||||
$parameters['bodyValues'] = $bodyValues;
|
||||
}
|
||||
|
||||
if (isset($this->data['headers'])) {
|
||||
$parameters['headers'] = $this->data['headers'];
|
||||
}
|
||||
if (isset($this->data['attachments'])) {
|
||||
$parameters['attachments'] = $this->data['attachments'];
|
||||
$parameters['hasAttachment'] = !empty($this->data['attachments']);
|
||||
} else {
|
||||
$parameters['hasAttachment'] = false;
|
||||
}
|
||||
|
||||
return $parameters;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user