Skip to content

Commit

Permalink
feat: replace email splitting by boundary with preg_split by header k…
Browse files Browse the repository at this point in the history
…ey words

Co-authored-by: Markus Hackel <[email protected]>
  • Loading branch information
maikschneider and pipaltree committed Sep 6, 2024
1 parent d9499ff commit eaea0d6
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions Classes/Utility/LogParserUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,11 @@ public function extractMessages(): void
return;
}

preg_match_all(
'/(?:; boundary=)(.+)(?:\r\n)/Ums',
$this->fileContent,
$boundaries
);
$mails = preg_split('/(?=(To:\s(.+)\nFrom:\s(.+)\nSubject:\s))/Ums', $this->fileContent);
$mails = array_filter($mails);

if (!isset($boundaries[1])) {
return;
}

foreach ($boundaries[1] as $boundary) {
$separator = '--' . $boundary . '--';
$messageParts = explode($separator, $this->fileContent);

if (!str_contains($messageParts[0], 'boundary=')) {
continue;
}

$messageString = trim($messageParts[0]);
$this->fileContent = $messageParts[1] ?: '';
foreach ($mails as $mailContent) {
$messageString = trim($mailContent);
$this->messages[] = self::convertToDto((string)$messageString);
}
}
Expand Down Expand Up @@ -131,6 +116,10 @@ protected static function convertToDto(string $msg): MailMessage
$dto->bodyPlain = @mb_convert_encoding($message->getTextContent() ?? '', 'UTF-8', 'auto');
$dto->bodyHtml = @mb_convert_encoding($message->getHtmlContent() ?? '', 'UTF-8', 'auto');

// remove "\r\n" from content, replace double line breaks with single line break and remove trailing whitespaces
$dto->bodyPlain = preg_replace(['/\r\n\r\n/', '/\s$/'], [' ', ''], $dto->bodyPlain);
$dto->bodyHtml = preg_replace(['/\r\n/', '/\s$/'], [' ', ''], $dto->bodyHtml);

$folder = self::getTempPath() . $dto->messageId;
if (!file_exists($folder)) {
GeneralUtility::mkdir($folder);
Expand Down

0 comments on commit eaea0d6

Please sign in to comment.