diff --git a/src/Gateway/MailerGateway.php b/src/Gateway/MailerGateway.php index 0d971a3..ae5b935 100644 --- a/src/Gateway/MailerGateway.php +++ b/src/Gateway/MailerGateway.php @@ -131,11 +131,11 @@ private function createEmailStamp(Parcel $parcel): EmailStamp $text = $this->replaceTokensAndInsertTags($parcel, $languageConfig->getString('email_text')); break; case 'htmlAndAutoText': - $html = $this->renderEmailTemplate($parcel); + $html = $this->renderEmailTemplate($parcel, $stamp); $text = Html2Text::convert($html); break; case 'textAndHtml': - $html = $this->renderEmailTemplate($parcel); + $html = $this->renderEmailTemplate($parcel, $stamp); $text = $this->replaceTokensAndInsertTags($parcel, $languageConfig->getString('email_text')); break; } @@ -143,7 +143,6 @@ private function createEmailStamp(Parcel $parcel): EmailStamp $stamp = $stamp->withText($text); if ($html) { - $html = $this->embedImages($html, $stamp); $stamp = $stamp->withHtml($html); } @@ -199,7 +198,7 @@ private function createEmail(Parcel $parcel): Email return $email; } - private function renderEmailTemplate(Parcel $parcel): string + private function renderEmailTemplate(Parcel $parcel, EmailStamp &$stamp): string { $languageConfig = $parcel->getStamp(LanguageConfigStamp::class)->languageConfig; $tokenCollection = $parcel->getStamp(TokenCollectionStamp::class)?->tokenCollection; @@ -215,7 +214,12 @@ private function renderEmailTemplate(Parcel $parcel): string $template->parsedTokens = null === $tokenCollection ? [] : $tokenCollection->forSimpleTokenParser(); $template->rawTokens = $tokenCollection; - return $this->contaoFramework->getAdapter(Controller::class)->convertRelativeUrls($this->replaceInsertTags($template->parse())); + $html = $this->replaceInsertTags($template->parse()); + + // Embed images before making URLs absolute + $html = $this->embedImages($html, $stamp); + + return $this->contaoFramework->getAdapter(Controller::class)->convertRelativeUrls($html); } private function addAttachmentsFromTokens(LanguageConfig $languageConfig, Parcel $parcel, EmailStamp $emailStamp): EmailStamp diff --git a/tests/Gateway/MailerGatewayTest.php b/tests/Gateway/MailerGatewayTest.php index 7eb6e40..62c4564 100644 --- a/tests/Gateway/MailerGatewayTest.php +++ b/tests/Gateway/MailerGatewayTest.php @@ -170,7 +170,7 @@ private function createFrameWorkWithTemplate(string $parsedTemplateHtml): Contao $controllerAdapter = $this->mockAdapter(['convertRelativeUrls']); $controllerAdapter ->method('convertRelativeUrls') - ->willReturnCallback(static fn (string $template): string => $template) + ->willReturnCallback(static fn (string $template): string => Controller::convertRelativeUrls($template, 'https://example.com')) ; $templateInstance = $this->createMock(FrontendTemplate::class);