Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent empty HTML email when sending raw message with Symfony Mail #55

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Listeners/SymfonyEmbedImages.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ private function attachImages()
{
// Get body
$body = $this->message->getHtmlBody();
if ($body === null) {
// Not an HTML message
return;
}

// Parse document
$parser = new HTML5();
Expand Down
20 changes: 20 additions & 0 deletions tests/MailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,24 @@ public function testDoesntTransformRawMessages()
($this->isLaravel9() ? $message->getTextBody() : $message->getBody())
);
}

/**
* @test
*/
public function testDoesNotCreateHtmlBodyForSymfonyRawMessage()
{
if (! $this->isLaravel9()) {
$this->assertTrue(true);

return;
}

$message = $this->handleBeforeSendPerformedEvent(
'raw-message.txt',
['enabled' => true, 'method' => 'attachment'],
true
);

$this->assertNull($message->getHtmlBody());
}
}
14 changes: 8 additions & 6 deletions tests/Traits/InteractsWithMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ private function isLaravel9()

/**
* @param string $htmlMessage
* @param bool $isRawMessage
*
* @return Email|Swift_Message
*/
protected function createMessage($htmlMessage)
protected function createMessage($htmlMessage, $isRawMessage = false)
{
if ($this->isLaravel9()) {
return (new Email())->to('[email protected]')->from('[email protected]')->subject('test')
->html($htmlMessage)
->text($htmlMessage);
->text($htmlMessage)
->html($isRawMessage ? null : $htmlMessage);
} else {
return new Swift_Message('test', $htmlMessage);
return new Swift_Message('test', $htmlMessage, $isRawMessage ? 'text/plain' : null);
}
}

Expand All @@ -53,12 +54,13 @@ protected function createSwiftEvent(Swift_Message $message)
/**
* @param string $libraryFile
* @param array $options
* @param bool $isRawMessage
* @return Swift_Message|Email
*/
protected function handleBeforeSendPerformedEvent($libraryFile, $options)
protected function handleBeforeSendPerformedEvent($libraryFile, $options, $isRawMessage = false)
{
$htmlMessage = $this->getLibraryFile($libraryFile);
$message = $this->createMessage($htmlMessage);
$message = $this->createMessage($htmlMessage, $isRawMessage);

if ($this->isLaravel9()) {
$event = new MessageSending($message);
Expand Down
Loading