refactor: use new mail interface desing

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-05-14 22:44:28 -04:00
parent b37da945f5
commit ca646eec3c
9 changed files with 311 additions and 200 deletions

View File

@@ -25,71 +25,74 @@ class MessageProperties extends MessagePropertiesMutableAbstract {
*/
public function fromImap(Message $message): static
{
$this->data['size'] = $message->size();
$this->data['flags'] = [];
foreach ($message->flags() as $flag) {
$flag = ltrim($flag, '\\');
$normalized = match (strtolower($flag)) {
'seen' => 'read',
'flagged' => 'flagged',
'answered' => 'answered',
'draft' => 'draft',
'deleted' => 'deleted',
default => strtolower($flag),
};
$this->data['flags'][$normalized] = true;
}
$this->data[static::PROPERTY_SIZE] = $message->size();
if ($message->messageId() !== null) {
$this->data['urid'] = $message->messageId();
$this->data[static::PROPERTY_URID] = $message->messageId();
}
if ($message->subject() !== null) {
$this->data['subject'] = $message->subject();
if ($message->inReplyTo() !== null) {
$this->data[static::PROPERTY_IN_REPLY_TO] = $message->inReplyTo();
}
//if ($message->references() !== []) {
// $this->data[static::PROPERTY_REFERENCES] = $message->references();
//}
$receivedAt = $message->receivedAt() ?? $message->internalDate();
if ($receivedAt !== null) {
$date = new DateTimeImmutable($receivedAt);
$this->data[static::PROPERTY_RECEIVED] = $date->format(DateTimeInterface::ATOM);
}
if ($message->sentAt() !== null) {
$date = new DateTimeImmutable($message->sentAt());
$this->data['date'] = $date->format(DateTimeInterface::ATOM);
}
if ($message->inReplyTo() !== null) {
$this->data['inReplyTo'] = $message->inReplyTo();
}
if ($message->from() !== []) {
$this->data['from'] = $message->from()[0]->toArray();
$this->data[static::PROPERTY_SENT] = $date->format(DateTimeInterface::ATOM);
}
if ($message->sender() !== []) {
$this->data['sender'] = $message->sender()[0]->toArray();
$this->data[static::PROPERTY_SENDER] = $message->sender()[0]->toArray();
}
foreach (['to', 'cc', 'bcc', 'replyTo'] as $field) {
if ($message->from() !== []) {
$this->data[static::PROPERTY_FROM] = $message->from()[0]->toArray();
}
$addressProperties = [
'to' => static::PROPERTY_TO,
'cc' => static::PROPERTY_CC,
'bcc' => static::PROPERTY_BCC,
'replyTo' => static::PROPERTY_REPLY_TO,
];
foreach ($addressProperties as $field => $property) {
$addresses = $message->{$field}();
if ($addresses === []) {
continue;
}
$this->data[$field] = array_map(
$this->data[$property] = array_map(
static fn ($address): array => $address->toArray(),
$addresses,
);
}
if ($message->subject() !== null) {
$this->data[static::PROPERTY_SUBJECT] = $message->subject();
}
if ($message->bodyStructure() !== null) {
$this->data['body'] = $message->bodyStructure()->toArray();
$this->data[static::PROPERTY_BODY] = $message->bodyStructure()->toArray();
$attachments = [];
self::collectAttachments($message->bodyStructure(), $attachments);
$this->collectAttachments($message->bodyStructure(), $attachments);
if ($attachments !== []) {
$this->data['attachments'] = $attachments;
$this->data[static::PROPERTY_ATTACHMENTS] = $attachments;
}
}
if ($message->bodyStructure() !== null) {
$this->data['body'] = $message->bodyStructure()->toArray();
$this->data[static::PROPERTY_BODY] = $message->bodyStructure()->toArray();
// Recursively add content from bodyValues to matching parts
if (is_array($message->bodySections())) {
$addContentToParts = function(&$structure, $bodyValues) use (&$addContentToParts) {
@@ -105,77 +108,36 @@ class MessageProperties extends MessagePropertiesMutableAbstract {
}
};
$addContentToParts($this->data['body'], $message->bodySections());
$addContentToParts($this->data[static::PROPERTY_BODY], $message->bodySections());
}
}
$this->data[static::PROPERTY_FLAGS] = [];
foreach ($message->flags() as $flag) {
$flag = ltrim($flag, '\\');
$normalized = match (strtolower($flag)) {
'seen' => 'read',
'flagged' => 'flagged',
'answered' => 'answered',
'draft' => 'draft',
'deleted' => 'deleted',
default => strtolower($flag),
};
$this->data[static::PROPERTY_FLAGS][$normalized] = true;
}
return $this;
}
public function toImap(): array
{
$message = [];
if (isset($this->data['flags'])) {
$message['flags'] = $this->data['flags'];
}
return $message;
}
/**
* Hydrate from store array
*/
public function fromStore(array $data): static {
$this->data = $data;
return $this;
}
/**
* Serialize to store array
*/
public function toStore(): array {
return $this->data;
}
/**
* Convert a string to UTF-8 from the given charset.
*
* Tries mb_convert_encoding first; falls back to iconv when mbstring does
* not recognise the charset name (e.g. "windows-1250").
*/
public static function toUtf8(string $content, string $charset): string
{
if ($charset === '' || in_array(strtolower($charset), ['utf-8', 'utf8'], true)) {
// Content claims to be UTF-8 but may still have invalid sequences; scrub to be safe.
return mb_convert_encoding($content, 'UTF-8', 'UTF-8');
}
// Try mbstring first
try {
$converted = mb_convert_encoding($content, 'UTF-8', $charset);
if ($converted !== false) {
return $converted;
}
} catch (\ValueError) {
// charset not recognised by mbstring — fall through to iconv
}
// iconv fallback (handles Windows-125x, ISO-8859-*, etc.)
$converted = @iconv($charset, 'UTF-8//TRANSLIT//IGNORE', $content);
$content = ($converted !== false) ? $converted : $content;
// Final scrub: strip any residual invalid UTF-8 bytes so json_encode never fails.
return mb_convert_encoding($content, 'UTF-8', 'UTF-8');
}
/**
* Recursively collect attachment parts from body structure
*/
private static function collectAttachments(ClientMessagePart $part, array &$attachments): void
private function collectAttachments(ClientMessagePart $part, array &$attachments): void
{
$children = $part->parts();
if ($children !== []) {
foreach ($children as $childPart) {
self::collectAttachments($childPart, $attachments);
$this->collectAttachments($childPart, $attachments);
}
return;
}