From e295044255624245c1b6a0bdf46e16920ed318e6 Mon Sep 17 00:00:00 2001 From: Maik Schneider Date: Fri, 6 Sep 2024 12:39:31 +0200 Subject: [PATCH 1/3] feat: adjust test fixtures for line breaks Co-authored-by: Markus Hackel --- Tests/Fixtures/TestMailTemplate.html | 4 +++- Tests/Fixtures/TestMailTemplate.txt | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Tests/Fixtures/TestMailTemplate.html b/Tests/Fixtures/TestMailTemplate.html index bc9c364..5d1c750 100644 --- a/Tests/Fixtures/TestMailTemplate.html +++ b/Tests/Fixtures/TestMailTemplate.html @@ -1 +1,3 @@ -https://domain/sign-in/registration/confirmation?tx_getaccess_confirmation%5Baction%5D=user&tx_getaccess_confirmation%5Bcontroller%5D=Confirmation&tx_getaccess_confirmation%5Bhash%5D=2720ba93a3007066&cHash=3179e61306999063853b2247a1bd4d +

Hallo Welt!


+
+https://domain/sign-in/registration/confirmation?tx_getaccess_confirmation%5Baction%5D=user&tx_getaccess_confirmation%5Bcontroller%5D=Confirmation&tx_getaccess_confirmation%5Bhash%5D=2720ba93a3007066&cHash=3179e61306999063853b2247a1bd4d diff --git a/Tests/Fixtures/TestMailTemplate.txt b/Tests/Fixtures/TestMailTemplate.txt index bc9c364..898b333 100644 --- a/Tests/Fixtures/TestMailTemplate.txt +++ b/Tests/Fixtures/TestMailTemplate.txt @@ -1 +1,3 @@ +Hallo Welt! + https://domain/sign-in/registration/confirmation?tx_getaccess_confirmation%5Baction%5D=user&tx_getaccess_confirmation%5Bcontroller%5D=Confirmation&tx_getaccess_confirmation%5Bhash%5D=2720ba93a3007066&cHash=3179e61306999063853b2247a1bd4d From d9499ff641c27719ecaae46041ba4a1d1159c9be Mon Sep 17 00:00:00 2001 From: Maik Schneider Date: Fri, 6 Sep 2024 12:39:57 +0200 Subject: [PATCH 2/3] feat: add plain text-only and html-only mails to test Co-authored-by: Markus Hackel --- .../Utility/LogParserUtilityTest.php | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/Tests/Functional/Utility/LogParserUtilityTest.php b/Tests/Functional/Utility/LogParserUtilityTest.php index 841734e..37fbd9c 100644 --- a/Tests/Functional/Utility/LogParserUtilityTest.php +++ b/Tests/Functional/Utility/LogParserUtilityTest.php @@ -64,21 +64,37 @@ public static function mailDataProvider(): array 'format' => FluidEmail::FORMAT_BOTH, ]; + $htmlOnlyMail = $defaultMail; + $htmlOnlyMail['format'] = FluidEmail::FORMAT_HTML; + + $plainOnlyMail = $defaultMail; + $plainOnlyMail['format'] = FluidEmail::FORMAT_PLAIN; + return [ [ [$defaultMail], ], [ [ + $plainOnlyMail, + $plainOnlyMail, $defaultMail, - $defaultMail, + $plainOnlyMail, ], ], [ [ + $htmlOnlyMail, + $htmlOnlyMail, $defaultMail, + $htmlOnlyMail, ], ], + [ + [$defaultMail], + [$htmlOnlyMail], + [$plainOnlyMail], + ], ]; } @@ -89,7 +105,7 @@ public static function mailDataProvider(): array */ public function testEmailEncoding(array $exampleMails): void { - foreach ($exampleMails as $key => $exampleMail) { + foreach ($exampleMails as $exampleMail) { $this->createTestMail($exampleMail); } From eaea0d6a67b91c87e14052c0a50e9a922d308532 Mon Sep 17 00:00:00 2001 From: Maik Schneider Date: Fri, 6 Sep 2024 12:40:10 +0200 Subject: [PATCH 3/3] feat: replace email splitting by boundary with preg_split by header key words Co-authored-by: Markus Hackel --- Classes/Utility/LogParserUtility.php | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/Classes/Utility/LogParserUtility.php b/Classes/Utility/LogParserUtility.php index 97ab946..40e2db9 100644 --- a/Classes/Utility/LogParserUtility.php +++ b/Classes/Utility/LogParserUtility.php @@ -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); } } @@ -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);