Skip to content

Commit

Permalink
Fixed embedding images (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar authored Dec 19, 2024
1 parent 6edef05 commit a1ddb2c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/Gateway/MailerGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,18 @@ 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;
}

$stamp = $stamp->withText($text);

if ($html) {
$html = $this->embedImages($html, $stamp);
$stamp = $stamp->withHtml($html);
}

Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Gateway/MailerGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a1ddb2c

Please sign in to comment.