3
votes

For a laravel project I want to view how the verify email, email looks. I did run php artisan vendor:publish --tag=laravel-mail and in the documentation I found:

Sometimes you may wish to capture the HTML content of a mailable without sending it. To accomplish this, you may call the render method of the mailable. This method will return the evaluated contents of the mailable as a string:

$invoice = App\Invoice::find(1);

return (new App\Mail\InvoicePaid($invoice))->render();

Is it possible to do something like this for the confirm email, email? I sarched for Mailable in my project but got no results. Is ther another way to view the email without sending it?

2
Do you have a mailable class setup yet?thefabdev
@Abiola No im using all default laravel settings.Sven van den Boogaart
I'd suggest you go through this article. It should help.thefabdev
Solving it with a route is quite dirty, if I were you, I would install Laravel Telescope and set your mail driver to log. You can then inspect all emails. And install Telescope using --dev.dbf

2 Answers

3
votes

Option 1 - Preview in Browser

Add a new route, preferably just in a test set up, that goes like this:

Route::get( '/verify-test', function () {
    // Get a user for demo purposes
    $user = App\User::find(1);
    return (new Illuminate\Auth\Notifications\VerifyEmail())->toMail($user);
}

In my tests with laravel 7 this was enough. I didn't have to publish any notifications. It just rendered nicely in the browser! Of course, if you want to also change the content of the notification it's a good idea to do:

php artisan vendor:publish --tag=laravel-notifications

The content of the notification is in the file resources/views/vendor/notifications

Laravel Notification Docs

Option 2 - Catch the Mails

What you can always do is either set a mailtrap.io account as smtp server and view the mail there or use the "Universal To" as described in the docs:

// config/mail.php
'to' => [
    'address' => '[email protected]',
    'name' => 'Example'
],
1
votes

To be honest? Yes you can add routes and make them accessible locally only, but why the hassle.

Laravel already built a tool called Laravel Telescope. Just install with composer, add --dev for development only and be done with it. Set your mail driver to log and inspect without the need to hack around with routes or whatever.

If you wish to test the mail without the hassle of registering all the time, create a factory to generate and mock emails and use Tinker, or better Tinkerwell to speedup your development process.