Bugfix: save/send message failed do to CloseMemoryResource() in MailSo\Mime\Part destructor

This commit is contained in:
the-djmaze 2022-01-28 12:24:43 +01:00
parent 7de00cd05c
commit 88e2c5f52d
3 changed files with 69 additions and 85 deletions

View file

@ -1068,7 +1068,7 @@ abstract class Utils
public static function MultipleStreamWriter($rRead, array $aWrite, int $iBufferLen = 8192, bool $bResetTimeLimit = true, bool $bFixCrLf = false, bool $bRewindOnComplete = false) : int public static function MultipleStreamWriter($rRead, array $aWrite, int $iBufferLen = 8192, bool $bResetTimeLimit = true, bool $bFixCrLf = false, bool $bRewindOnComplete = false) : int
{ {
$mResult = false; $mResult = false;
if ($rRead && \count($aWrite)) if (\is_resource($rRead) && \count($aWrite))
{ {
$mResult = 0; $mResult = 0;
while (!\feof($rRead)) while (!\feof($rRead))
@ -1080,7 +1080,7 @@ abstract class Utils
break; break;
} }
if (0 === $iBufferLen || '' === $sBuffer) if ('' === $sBuffer)
{ {
break; break;
} }

View file

@ -511,71 +511,6 @@ class Message
return $oAlternativePart; return $oAlternativePart;
} }
private function createNewMessageSimpleOrAlternativeBody() : Part
{
$oResultPart = null;
if (1 < \count($this->aAlternativeParts))
{
$oResultPart = new Part;
$oResultPart->Headers->append(
new Header(Enumerations\Header::CONTENT_TYPE,
Enumerations\MimeType::MULTIPART_ALTERNATIVE.'; '.
(new ParameterCollection)->Add(
new Parameter(
Enumerations\Parameter::BOUNDARY,
$this->generateNewBoundary())
)->ToString()
)
);
foreach ($this->aAlternativeParts as $aAlternativeData)
{
$oAlternativePart = $this->createNewMessageAlternativePartBody($aAlternativeData);
if ($oAlternativePart)
{
$oResultPart->SubParts->append($oAlternativePart);
}
unset($oAlternativePart);
}
}
else if (1 === \count($this->aAlternativeParts))
{
$oAlternativePart = $this->createNewMessageAlternativePartBody($this->aAlternativeParts[0]);
if ($oAlternativePart)
{
$oResultPart = $oAlternativePart;
}
}
if (!$oResultPart)
{
if ($this->bAddEmptyTextPart)
{
$oResultPart = $this->createNewMessageAlternativePartBody(array(
Enumerations\MimeType::TEXT_PLAIN, null
));
}
else
{
$aAttachments = $this->oAttachmentCollection->getArrayCopy();
if (1 === \count($aAttachments) && isset($aAttachments[0]))
{
$this->oAttachmentCollection->Clear();
$oResultPart = $this->createNewMessageAlternativePartBody(array(
$aAttachments[0]->ContentType(), $aAttachments[0]->Resource(),
'', $aAttachments[0]->CustomContentTypeParams()
));
}
}
}
return $oResultPart;
}
private function createNewMessageRelatedBody(Part $oIncPart) : Part private function createNewMessageRelatedBody(Part $oIncPart) : Part
{ {
$oResultPart = null; $oResultPart = null;
@ -672,26 +607,80 @@ class Message
return $oIncPart; return $oIncPart;
} }
public function ToPart(bool $bWithoutBcc = false) : Part
{
$oPart = $this->createNewMessageSimpleOrAlternativeBody();
$oPart = $this->createNewMessageRelatedBody($oPart);
$oPart = $this->createNewMessageMixedBody($oPart);
$oPart = $this->setDefaultHeaders($oPart, $bWithoutBcc);
return $oPart;
}
/** /**
* @return resource * @return resource
*/ */
public function ToStream(bool $bWithoutBcc = false) public function ToStream(bool $bWithoutBcc = false)
{ {
return $this->ToPart($bWithoutBcc)->ToStream(); $oPart = null;
} if (1 < \count($this->aAlternativeParts))
{
$oPart = new Part;
$oPart->Headers->append(
new Header(Enumerations\Header::CONTENT_TYPE,
Enumerations\MimeType::MULTIPART_ALTERNATIVE.'; '.
(new ParameterCollection)->Add(
new Parameter(
Enumerations\Parameter::BOUNDARY,
$this->generateNewBoundary())
)->ToString()
)
);
foreach ($this->aAlternativeParts as $aAlternativeData)
{
$oAlternativePart = $this->createNewMessageAlternativePartBody($aAlternativeData);
if ($oAlternativePart)
{
$oPart->SubParts->append($oAlternativePart);
}
unset($oAlternativePart);
}
}
else if (1 === \count($this->aAlternativeParts))
{
$oAlternativePart = $this->createNewMessageAlternativePartBody($this->aAlternativeParts[0]);
if ($oAlternativePart)
{
$oPart = $oAlternativePart;
}
}
if (!$oPart)
{
if ($this->bAddEmptyTextPart)
{
$oPart = $this->createNewMessageAlternativePartBody(array(
Enumerations\MimeType::TEXT_PLAIN, null
));
}
else
{
$aAttachments = $this->oAttachmentCollection->getArrayCopy();
if (1 === \count($aAttachments) && isset($aAttachments[0]))
{
$this->oAttachmentCollection->Clear();
$oPart = $this->createNewMessageAlternativePartBody(array(
$aAttachments[0]->ContentType(), $aAttachments[0]->Resource(),
'', $aAttachments[0]->CustomContentTypeParams()
));
}
}
}
$oPart = $this->createNewMessageRelatedBody($oPart);
$oPart = $this->createNewMessageMixedBody($oPart);
$oPart = $this->setDefaultHeaders($oPart, $bWithoutBcc);
return $oPart->ToStream();
}
/*
public function ToString(bool $bWithoutBcc = false) : string public function ToString(bool $bWithoutBcc = false) : string
{ {
return \stream_get_contents($this->ToStream($bWithoutBcc)); return \stream_get_contents($this->ToStream($bWithoutBcc));
} }
*/
} }

View file

@ -63,11 +63,6 @@ class Part
$this->SubParts = new PartCollection; $this->SubParts = new PartCollection;
} }
function __destruct()
{
\MailSo\Base\ResourceRegistry::CloseMemoryResource($this->Body);
}
public function Boundary() : string public function Boundary() : string
{ {
return $this->sBoundary; return $this->sBoundary;
@ -532,7 +527,7 @@ class Part
if ($this->Body && \is_resource($this->Body)) if ($this->Body && \is_resource($this->Body))
{ {
$aMeta = \stream_get_meta_data($this->Body); $aMeta = \stream_get_meta_data($this->Body);
if (isset($aMeta['seekable']) && $aMeta['seekable']) if (!empty($aMeta['seekable']))
{ {
\rewind($this->Body); \rewind($this->Body);
} }