Skip to content

Commit

Permalink
Fix setting mail header (#32272)
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints authored Apr 7, 2020
1 parent f0f977a commit bf4b6d1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Illuminate/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

Expand Down
5 changes: 5 additions & 0 deletions tests/Mail/MailMailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit bf4b6d1

Please sign in to comment.