mail parsing attachment fix: fetch binary content

This commit is contained in:
Yuri Kuznetsov
2020-04-13 10:57:41 +03:00
parent dcc97f4f26
commit 5f9db6489a
@@ -198,8 +198,6 @@ class MailMimeParser
foreach ($attachmentObjList as $attachmentObj) {
$attachment = $this->getEntityManager()->getEntity('Attachment');
$content = $attachmentObj->getContent();
$disposition = $attachmentObj->getHeaderValue('Content-Disposition');
$attachment = $this->getEntityManager()->getEntity('Attachment');
@@ -213,17 +211,10 @@ class MailMimeParser
$attachment->set('name', $filename);
$attachment->set('type', $contentType);
$charset = $attachmentObj->getHeaderParameter('Content-Type', 'charset');
if (!$charset && $content && in_array($contentType, ['text/plain', 'text/html'])) {
$binaryContentStream = $attachmentObj->getBinaryContentStream();
if ($binaryContentStream) {
$rawContent = $binaryContentStream->getContents();
$encoding = $this->detectEncodingByBom($rawContent);
if ($encoding) {
$content = $rawContent;
}
}
$content = '';
$binaryContentStream = $attachmentObj->getBinaryContentStream();
if ($binaryContentStream) {
$content = $binaryContentStream->getContents();
}
$contentId = $attachmentObj->getHeaderValue('Content-ID');
@@ -271,28 +262,4 @@ class MailMimeParser
$email->set('body', $body);
}
}
protected function detectEncodingByBom(string $contents) : ?string
{
$first2 = substr($contents, 0, 2);
$first3 = substr($contents, 0, 3);
$first4 = substr($contents, 0, 4);
if ($first3 == chr(0xEF) . chr(0xBB) . chr(0xBF))
return 'UTF-8';
else
if ($first4 == chr(0x00) . chr(0x00) . chr(0xFE) . chr(0xFF))
return 'UTF-32BE';
else
if ($first4 == chr(0xFF) . chr(0xFE) . chr(0x00) . chr(0x00))
return 'UTF-32LE';
else
if ($first2 == chr(0xFE) . chr(0xFF))
return 'UTF-16BE';
else
if ($first2 == chr(0xFF) . chr(0xFE))
return 'UTF-16LE';
return null;
}
}