How to Preview Laravel Notification Emails

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.

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 AppNotificationsTemplateNotification())->toMail((object) [])->render();
});

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 = AppModelsUser::inRandomOrder()->first();
return (new AppNotificationsUserRegisteredNotification($user))->toMail((object) [])->render();

});

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

Leave a Reply

Your email address will not be published. Required fields are marked *