* SPDX-License-Identifier: AGPL-3.0-or-later */ namespace KTXM\ProviderImap\Providers; use KTXF\Mail\Object\MessagePartMutableAbstract; use KTXM\ProviderImap\Client\MessagePart as ImapMessagePart; /** * Mail Message Part Implementation */ class MessagePart extends MessagePartMutableAbstract { /** * Convert gricob BodyStructure part to message part object * * @param Part $part gricob BodyStructure Part (SinglePart or MultiPart) * @param string $partId numeric part identifier (e.g. "1", "1.1", "2") */ public function fromImap(ImapMessagePart $part, string $partId = '1'): static { $this->data['partId'] = $partId; if ($part instanceof SinglePart) { $mimeType = strtolower($part->type) . '/' . strtolower($part->subtype); $this->data['type'] = $mimeType; if ($part->id !== null) { $this->data['blobId'] = trim($part->id, '<>'); } // Content-Type parameters (name, charset, etc.) if (!empty($part->attributes)) { foreach ($part->attributes as $key => $value) { $keyLower = strtolower($key); if ($keyLower === 'name') { $this->data['name'] = $value; } elseif ($keyLower === 'charset') { $this->data['charset'] = $value; } } } if ($part->encoding !== null) { $this->data['encoding'] = strtolower($part->encoding); } if ($part->size !== null) { $this->data['size'] = $part->size; } if ($part->disposition !== null) { $this->data['disposition'] = strtolower($part->disposition->type); // disposition filename attribute if (!empty($part->disposition->attributes)) { foreach ($part->disposition->attributes as $key => $value) { if (strtolower($key) === 'filename') { $this->data['name'] = $this->data['name'] ?? $value; } } } } if (!empty($part->language)) { $this->data['language'] = implode(',', $part->language); } if ($part->location !== null) { $this->data['location'] = $part->location; } } elseif ($part instanceof MultiPart) { $this->data['type'] = 'multipart/' . strtolower($part->subtype); if ($part->disposition !== null) { $this->data['disposition'] = strtolower($part->disposition->type); } if (!empty($part->language)) { $this->data['language'] = implode(',', $part->language); } if ($part->location !== null) { $this->data['location'] = $part->location; } // Recursively process sub-parts // When this part has no section ID (root multipart) children are // numbered "1", "2", … to match IMAP section numbering. foreach ($part->parts as $index => $subPart) { $subPartId = ($partId === '') ? (string)($index + 1) : $partId . '.' . ($index + 1); $this->parts[] = (new MessagePart())->fromImap($subPart, $subPartId); } } return $this; } }