Skip to content

Commit

Permalink
mail parsing attachment fix: fetch binary content
Browse files Browse the repository at this point in the history
  • Loading branch information
yurikuzn committed Apr 13, 2020
1 parent dcc97f4 commit 5f9db64
Showing 1 changed file with 4 additions and 37 deletions.
41 changes: 4 additions & 37 deletions application/Espo/Core/Mail/Parsers/MailMimeParser.php
Expand Up @@ -198,8 +198,6 @@ public function fetchContentParts(\Espo\Entities\Email $email, $message, &$inlin
foreach ($attachmentObjList as $attachmentObj) {
$attachment = $this->getEntityManager()->getEntity('Attachment');

$content = $attachmentObj->getContent();

$disposition = $attachmentObj->getHeaderValue('Content-Disposition');

$attachment = $this->getEntityManager()->getEntity('Attachment');
Expand All @@ -213,17 +211,10 @@ public function fetchContentParts(\Espo\Entities\Email $email, $message, &$inlin
$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');
Expand Down Expand Up @@ -271,28 +262,4 @@ public function fetchContentParts(\Espo\Entities\Email $email, $message, &$inlin
$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;
}
}

0 comments on commit 5f9db64

Please sign in to comment.