DEV Community

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

Posted on

1

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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

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.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay