DEV Community

Cover image for How to Preview Laravel Notification Emails
Daniel Mabadeje
Daniel Mabadeje

Posted on

How to Preview Laravel Notification Emails

It’s a hassle trying to test your the HTML of your notification class by sending multiple requests to trigger the Notification to your mail. Do you want to preview your emails in the browser and see what they would look like in the recepients mailbox without having to go through the hassles of sending multiple requests to trigger the notifications? Here are a few lines of codes that can help you with that.

Image of laravel notifciation

The Snippet below shows how you can preview your notifications with your web routes. I am assuming you have created a Notification called TemplateNotification (for this example) or you replaced TemplateNotification with your custom class name. In the example below, my notification class did not require any parameters.

Route::get(‘mailable’, function () {
  return (new App\Notifications\TemplateNotification())->toMail((object) [])->render();
});
Enter fullscreen mode Exit fullscreen mode

Let’s try this with a class that requires a parameter let’s say the UserRegisteredNotification class that requires the User Object. The code would look like this.

Route::get('mailable', function () {
    $user = App\Models\User::inRandomOrder()->first();
    return (new App\Notifications\UserRegisteredNotification($user))->toMail((object) [])->render();    

});
Enter fullscreen mode Exit fullscreen mode

If you observed in the examples above, you would see that I typecasted an empty array as an object to the toMail method. This is because the toMail method is expecting an object as a parameter.
I hope this helps

Follow me on twitter : https://twitter.com/mabadejedanphp

Top comments (1)

Collapse
 
snehalkadwe profile image
Snehal Rajeev Moon

It is good to know a straightforward method to visualise and fine-tune email templates without the hassle of multiple requests. Thank you for sharing.