Skip to content

Commit

Permalink
Allow mailable to be rendered directly to views.
Browse files Browse the repository at this point in the history
This also makes Mailables “Renderable” allowing you to return them
directly from a route for easier viewing, etc.
  • Loading branch information
taylorotwell committed Mar 15, 2017
1 parent ff563d2 commit d9a6dfa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Illuminate/Mail/Mailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
use Illuminate\Support\Str;
use Illuminate\Support\Collection;
use Illuminate\Container\Container;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Contracts\Queue\Factory as Queue;
use Illuminate\Contracts\Mail\Mailer as MailerContract;
use Illuminate\Contracts\Mail\Mailable as MailableContract;

class Mailable implements MailableContract
class Mailable implements MailableContract, Renderable
{
/**
* The person the message is from.
Expand Down Expand Up @@ -159,6 +160,20 @@ public function later($delay, Queue $queue)
);
}

/**
* Render the mailable into a view.
*
* @return \Illuminate\View\View
*/
public function render()
{
Container::getInstance()->call([$this, 'build']);

return Container::getInstance()->make('mailer')->render(
$this->buildView(), $this->buildViewData()
);
}

/**
* Build the view for the message.
*
Expand Down
19 changes: 19 additions & 0 deletions src/Illuminate/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,25 @@ public function plain($view, array $data, $callback)
return $this->send(['text' => $view], $data, $callback);
}

/**
* Render the given message as a view.
*
* @param string|array $view
* @param array $data
* @return \Illuminate\View\View
*/
public function render($view, array $data = [])
{
// First we need to parse the view, which could either be a string or an array
// containing both an HTML and plain text versions of the view which should
// be used when sending an e-mail. We will extract both of them out here.
list($view, $plain, $raw) = $this->parseView($view);

$data['message'] = $this->createMessage();

return $this->renderView($view, $data);
}

/**
* Send a new message using a view.
*
Expand Down

0 comments on commit d9a6dfa

Please sign in to comment.