Skip to content

Commit

Permalink
fixes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Sazanov committed Aug 16, 2023
1 parent a1d61c4 commit b61b7bc
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/Models/BodyStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ protected function analizeBodyParts(string $string, MultiPart $parentMultipart =
$parentMultipart->setAttachmentsExists(true);
}
}

$parentMultipart->getParts()->add($attachment);
$section[array_key_last($section)]++;
$level = implode('.', $section);
Expand Down
61 changes: 60 additions & 1 deletion src/Models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Sazanof\PhpImapSockets\Collections\AddressesCollection;
use Sazanof\PhpImapSockets\Collections\MessageHeadersCollection;
use Sazanof\PhpImapSockets\Parts\AttachmentPart;
use Sazanof\PhpImapSockets\Parts\TextPart;
use Sazanof\PhpImapSockets\Query\FetchQuery;
use Sazanof\PhpImapSockets\Response\AttachmentBodyResponse;
Expand Down Expand Up @@ -151,6 +152,22 @@ public function getBody(TextPart $part): bool|string|null
return null;
}

/**
* @param AttachmentPart $part
* @return string
* @throws \ReflectionException
*/
public function getInlineImage(AttachmentPart $part)
{
if (!is_null($this->mailbox)) {
$q = new FetchQuery();
$body = $this->mailbox->fetch([$this->num], $q->body($part->getSection()))->asCollection(BodyResponse::class);
$body = $body->getContent();
return 'data:' . $part->getMimeType() . ';' . $part->getEncoding() . ',' . str_replace(["\r\n", "\r", "\n"], '', $body);
}
return '';
}

/**
* @return bool|string|null
* @throws \ReflectionException
Expand All @@ -159,12 +176,54 @@ public function getHtmlText(): bool|string|null
{
foreach ($this->getBodyStructure()->getTextParts() as $textPart) {
if ($textPart->getMimeType() === 'text/html') {
return $this->getBody($textPart);
$body = $this->getBody($textPart);
$contentIds = $this->findContentIds($body);
if (!empty($contentIds)) {
// html part contains inline images
foreach ($contentIds as $cid) {
$inlinePart = $this->getPartByContentId($cid);
$inlineContent = $this->getInlineImage($inlinePart);
$body = str_replace("cid:$cid", $inlineContent, $body);
}
}
return $body;
}
}
return null;
}

/**
* @param string $cid
* @param MultiPart|null $_part
* @return AttachmentPart|null
*/
public function getPartByContentId(string $cid, MultiPart $_part = null)
{
$parts = is_null($_part) ? $this->getBodyStructure()->getParts() : $_part->getParts();
/** @var AttachmentPart $part */
foreach ($parts->items() as $part) {
if ($part instanceof AttachmentPart) {
if (strtolower($part->getDisposition()) === 'inline' && $part->getContentId() === "<$cid>") {
return $part;
}
} elseif ($part instanceof MultiPart) {
return $this->getPartByContentId($cid, $part);
}
}
return null;
}

/**
* @param string $html
* @return mixed|void
*/
protected function findContentIds(string $html)
{
if (preg_match_all('/src="cid:(.*?)"/', $html, $matches)) {
return $matches[1];
}
}

/**
* @param int|null $length
* @return bool|string|null
Expand Down

0 comments on commit b61b7bc

Please sign in to comment.