Skip to content

Commit

Permalink
Merge pull request #36 from xima-media/single-format
Browse files Browse the repository at this point in the history
feat: parse single format mails
  • Loading branch information
maikschneider authored Sep 6, 2024
2 parents aac0c51 + eaea0d6 commit b273e79
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 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
4 changes: 3 additions & 1 deletion Tests/Fixtures/TestMailTemplate.html
Original file line number Diff line number Diff line change
@@ -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
<h1>Hallo Welt!</h1><br />
<br />
<a href="#">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</a>
2 changes: 2 additions & 0 deletions Tests/Fixtures/TestMailTemplate.txt
Original file line number Diff line number Diff line change
@@ -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
20 changes: 18 additions & 2 deletions Tests/Functional/Utility/LogParserUtilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
],
];
}

Expand All @@ -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);
}

Expand Down

0 comments on commit b273e79

Please sign in to comment.