Skip to content
Closed
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
11 changes: 8 additions & 3 deletions src/Illuminate/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,20 +351,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