Skip to content

Commit d9a6dfa

Browse files
committed
Allow mailable to be rendered directly to views.
This also makes Mailables “Renderable” allowing you to return them directly from a route for easier viewing, etc.
1 parent ff563d2 commit d9a6dfa

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/Illuminate/Mail/Mailable.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
use Illuminate\Support\Str;
99
use Illuminate\Support\Collection;
1010
use Illuminate\Container\Container;
11+
use Illuminate\Contracts\Support\Renderable;
1112
use Illuminate\Contracts\Queue\Factory as Queue;
1213
use Illuminate\Contracts\Mail\Mailer as MailerContract;
1314
use Illuminate\Contracts\Mail\Mailable as MailableContract;
1415

15-
class Mailable implements MailableContract
16+
class Mailable implements MailableContract, Renderable
1617
{
1718
/**
1819
* The person the message is from.
@@ -159,6 +160,20 @@ public function later($delay, Queue $queue)
159160
);
160161
}
161162

163+
/**
164+
* Render the mailable into a view.
165+
*
166+
* @return \Illuminate\View\View
167+
*/
168+
public function render()
169+
{
170+
Container::getInstance()->call([$this, 'build']);
171+
172+
return Container::getInstance()->make('mailer')->render(
173+
$this->buildView(), $this->buildViewData()
174+
);
175+
}
176+
162177
/**
163178
* Build the view for the message.
164179
*

src/Illuminate/Mail/Mailer.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,25 @@ public function plain($view, array $data, $callback)
171171
return $this->send(['text' => $view], $data, $callback);
172172
}
173173

174+
/**
175+
* Render the given message as a view.
176+
*
177+
* @param string|array $view
178+
* @param array $data
179+
* @return \Illuminate\View\View
180+
*/
181+
public function render($view, array $data = [])
182+
{
183+
// First we need to parse the view, which could either be a string or an array
184+
// containing both an HTML and plain text versions of the view which should
185+
// be used when sending an e-mail. We will extract both of them out here.
186+
list($view, $plain, $raw) = $this->parseView($view);
187+
188+
$data['message'] = $this->createMessage();
189+
190+
return $this->renderView($view, $data);
191+
}
192+
174193
/**
175194
* Send a new message using a view.
176195
*

0 commit comments

Comments
 (0)