From bf4b6d124e4187751ba7d9c10add8801edd5491d Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Tue, 7 Apr 2020 16:38:28 +0200 Subject: [PATCH] Fix setting mail header (#32272) --- src/Illuminate/Mail/Mailer.php | 11 ++++++++--- tests/Mail/MailMailerTest.php | 5 +++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Mail/Mailer.php b/src/Illuminate/Mail/Mailer.php index 01a32bded0a8..34250f3ca6b0 100755 --- a/src/Illuminate/Mail/Mailer.php +++ b/src/Illuminate/Mail/Mailer.php @@ -324,20 +324,25 @@ protected function parseView($view) */ protected function addContent($message, $view, $plain, $raw, $data) { + $header = $message->getContentType(); + if (isset($view)) { - $message->setBody($this->renderView($view, $data), 'text/html'); + $message->setBody( + $this->renderView($view, $data), + $header && $header !== 'text/plain' ? $header : 'text/html' + ); } if (isset($plain)) { $method = isset($view) ? 'addPart' : 'setBody'; - $message->$method($this->renderView($plain, $data), 'text/plain'); + $message->$method($this->renderView($plain, $data), $header ?: 'text/plain'); } if (isset($raw)) { $method = (isset($view) || isset($plain)) ? 'addPart' : 'setBody'; - $message->$method($raw, 'text/plain'); + $message->$method($raw, $header ?: 'text/plain'); } } diff --git a/tests/Mail/MailMailerTest.php b/tests/Mail/MailMailerTest.php index 3a3ef125f1c4..74ec658fd2a5 100755 --- a/tests/Mail/MailMailerTest.php +++ b/tests/Mail/MailMailerTest.php @@ -36,6 +36,7 @@ public function testMailerSendSendsMessageWithProperViewContent() $message->shouldReceive('setFrom')->never(); $this->setSwiftMailer($mailer); $message->shouldReceive('getSwiftMessage')->once()->andReturn($message); + $message->shouldReceive('getContentType')->once()->andReturn(''); $mailer->getSwiftMailer()->shouldReceive('send')->once()->with($message, []); $mailer->send('foo', ['data'], function ($m) { $_SERVER['__mailer.test'] = $m; @@ -57,6 +58,7 @@ public function testMailerSendSendsMessageWithProperViewContentUsingHtmlStrings( $message->shouldReceive('setFrom')->never(); $this->setSwiftMailer($mailer); $message->shouldReceive('getSwiftMessage')->once()->andReturn($message); + $message->shouldReceive('getContentType')->once()->andReturn(''); $mailer->getSwiftMailer()->shouldReceive('send')->once()->with($message, []); $mailer->send(['html' => new HtmlString('rendered.view'), 'text' => new HtmlString('rendered.text')], ['data'], function ($m) { $_SERVER['__mailer.test'] = $m; @@ -77,6 +79,7 @@ public function testMailerSendSendsMessageWithProperViewContentUsingHtmlMethod() $message->shouldReceive('setFrom')->never(); $this->setSwiftMailer($mailer); $message->shouldReceive('getSwiftMessage')->once()->andReturn($message); + $message->shouldReceive('getContentType')->once()->andReturn(''); $mailer->getSwiftMailer()->shouldReceive('send')->once()->with($message, []); $mailer->html('rendered.view', function ($m) { $_SERVER['__mailer.test'] = $m; @@ -99,6 +102,7 @@ public function testMailerSendSendsMessageWithProperPlainViewContent() $message->shouldReceive('setFrom')->never(); $this->setSwiftMailer($mailer); $message->shouldReceive('getSwiftMessage')->once()->andReturn($message); + $message->shouldReceive('getContentType')->once()->andReturn(''); $mailer->getSwiftMailer()->shouldReceive('send')->once()->with($message, []); $mailer->send(['foo', 'bar'], ['data'], function ($m) { $_SERVER['__mailer.test'] = $m; @@ -121,6 +125,7 @@ public function testMailerSendSendsMessageWithProperPlainViewContentWhenExplicit $message->shouldReceive('setFrom')->never(); $this->setSwiftMailer($mailer); $message->shouldReceive('getSwiftMessage')->once()->andReturn($message); + $message->shouldReceive('getContentType')->once()->andReturn(''); $mailer->getSwiftMailer()->shouldReceive('send')->once()->with($message, []); $mailer->send(['html' => 'foo', 'text' => 'bar'], ['data'], function ($m) { $_SERVER['__mailer.test'] = $m;