4

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?

wschopohl
  • 1,567
  • 1
  • 11
  • 18
Sven van den Boogaart
  • 11,833
  • 21
  • 86
  • 169
  • Do you have a mailable class setup yet? – thefabdev Jul 18 '20 at 19:32
  • @Abiola No im using all default laravel settings. – Sven van den Boogaart Jul 18 '20 at 19:34
  • I'd suggest you go through this [article](https://johnbeales.com/2018/browser-preview-laravel-password-reset-and-verification-emails/). It should help. – thefabdev Jul 18 '20 at 19:38
  • Solving it with a route is quite dirty, if I were you, I would install [Laravel Telescope](https://laravel.com/docs/7.x/telescope) and set your mail driver to `log`. You can then inspect all emails. And install Telescope using `--dev`. – dbf Jul 18 '20 at 19:39

2 Answers2

4

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' => 'example@example.com',
    'name' => 'Example'
],
wschopohl
  • 1,567
  • 1
  • 11
  • 18
  • funny how hard you're trying – thefabdev Jul 18 '20 at 21:08
  • I know, SO is like a current addiction :D But I'm also genuinly interested and came up with my own solution by studying the source code, reading the docs and testing ;) – wschopohl Jul 18 '20 at 21:11
  • @wschopohl Very nice thats exactly what I was looking for. The files in notifications seem to be templates for the notifications message but as far as I can tell I can not change the content of the email verifcation email in the file. – Sven van den Boogaart Jul 18 '20 at 21:29
  • Interesting. After publishing I could change that notification and instantly saw the changes in the test view route.. – wschopohl Jul 18 '20 at 21:30
  • @wschopohl which file do you change? Im only see "resources/views/vendor/notifications/email.blade.php" – Sven van den Boogaart Jul 18 '20 at 21:38
  • that's exactly the only one I'm seeing, too. I was able to alter the text down by "Subcopy". I think the rest of the message is defined directly in "vendor/laravel/framework/src/Illuminate/Auth/Notifications/VerifyEmail.php" which in turn seems to utilize the Lang Facade.. Can't dig deeper now, because the bed is calling ;) – wschopohl Jul 18 '20 at 21:46
  • I think the rabbit hole goes something like "VerifyEmail" uses a new "MailMessage" which extends "SimpleMessage" which has functions like line(...) to throw text into.. – wschopohl Jul 18 '20 at 21:50
  • This seems promising: https://stackoverflow.com/questions/52416804/how-to-customize-the-email-verification-email-from-laravel-5-7 or google laravel custom verification email – wschopohl Jul 18 '20 at 21:57
1

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.

dbf
  • 3,278
  • 1
  • 24
  • 34