Preview mails, notifications in browser
Hello, been troubled with debugging the look and feel of your emails or notifications in Laravel. There is a way to render/preview you mailable blades right from the comfort of your broswer, without first sending out the actual email.
Let’s create a sales receipt mailable.
php artisan make:mail SalesReceipt --markdown=mails.sales.receipt
The artisan command will create a Mailable class App\Mail\SalesReceipt
and a markdown mail template resources/views/mails/sales/receipt.blade.php
.
Mailable:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; use Illuminate\Contracts\Queue\ShouldQueue; class SalesReceipt extends Mailable { use Queueable, SerializesModels; /** * Create a new message instance. * * @return void */ public function __construct() { // } /** * Build the message. * * @return $this */ public function build() { return $this->markdown('mails.sales.receipt'); } } |
Markdown:
1 2 3 4 5 6 7 8 9 10 11 12 |
@component('mail::message') # Introduction The body of your message. @component('mail::button', ['url' => '']) Button Text @endcomponent Thanks,<br> {{ config('app.name') }} @endcomponent |
Routes:
Laravel 5.5+
1 2 3 |
Route::get('/receipt-mail', function () { return new \App\Mail\SalesReceipt(); }); |
Laravel 5.4
1 2 3 4 |
Route::get('/receipt-mail', function () { $markdown = new \Illuminate\Mail\Markdown(view(), config('mail.markdown')); return $markdown->render('mails.sales.receipt'); }); |
Passing data to markdown blade
You may pass data the blade in the render()
. Let take this example…
Markdown:
1 2 3 4 5 6 7 8 9 10 11 12 |
@component('mail::message') # Introduction The body of your message. @component('mail::button', ['url' => $url]) Button Text @endcomponent Thanks,<br> {{ config('app.name') }} @endcomponent |
Laravel 5.4
1 2 3 4 5 6 |
Route::get('/receipt-mail', function () { $markdown = new \Illuminate\Mail\Markdown(view(), config('mail.markdown')); return $markdown->render('mails.sales.receipt', [ 'url' => 'http://localhost:8000/sales/receipt/123456', ]); }); |
Laravel 5.5+
1 2 3 4 5 6 7 |
Route::get('/receipt-mail', function () { $receipt = new \App\Mail\SalesReceipt(); return $receipt->markdown('mails.sales.receipt', [ 'url' => 'http://localhost:8000/sales/receipt/123456', ]); }); |
Note:
Don’t indent your markdown blades in the IDE, it may be rendered wrongly.
That’s all. Stay tuned for more…