Skip to content

Commit

Permalink
make it possible to directly mail html strings
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Dec 9, 2016
1 parent 8f90447 commit 882ea28
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Illuminate/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Swift_Message;
use Illuminate\Support\Arr;
use InvalidArgumentException;
use Illuminate\Support\HtmlString;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Container\Container;
Expand Down Expand Up @@ -427,7 +428,9 @@ protected function createMessage()
*/
protected function getView($view, $data)
{
return $this->views->make($view, $data)->render();
return $view instanceof HtmlString
? $view->toHtml()
: $this->views->make($view, $data)->render();
}

/**
Expand Down
22 changes: 22 additions & 0 deletions tests/Mail/MailMailerTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Mockery as m;
use Illuminate\Support\HtmlString;

class MailMailerTest extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -29,6 +30,27 @@ public function testMailerSendSendsMessageWithProperViewContent()
unset($_SERVER['__mailer.test']);
}

public function testMailerSendSendsMessageWithProperViewContentUsingHtmlStrings()
{
unset($_SERVER['__mailer.test']);
$mailer = $this->getMockBuilder('Illuminate\Mail\Mailer')->setMethods(['createMessage'])->setConstructorArgs($this->getMocks())->getMock();
$message = m::mock('Swift_Mime_Message');
$mailer->expects($this->once())->method('createMessage')->will($this->returnValue($message));
$view = m::mock('StdClass');
$mailer->getViewFactory()->shouldReceive('make')->never();
$view->shouldReceive('render')->never();
$message->shouldReceive('setBody')->once()->with('rendered.view', 'text/html');
$message->shouldReceive('addPart')->once()->with('rendered.text', 'text/plain');
$message->shouldReceive('setFrom')->never();
$this->setSwiftMailer($mailer);
$message->shouldReceive('getSwiftMessage')->once()->andReturn($message);
$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;
});
unset($_SERVER['__mailer.test']);
}

public function testMailerSendSendsMessageWithProperPlainViewContent()
{
unset($_SERVER['__mailer.test']);
Expand Down

0 comments on commit 882ea28

Please sign in to comment.