I'm making a course on Laravel Sanctum: MasteringAuth.com

Testing emails with Mailhog

Constantin Druc ยท 13 Jun, 2021

[00:00] If you've ever tested emails in Laravel, there's a good chance you might have used something like mailtrap.io. Laravel Sail ships with a similar tool called Mailhog. If you open your environment file and scroll down to the mail configuration, you'll see that the mail host is set to Mailhog.

[00:23] If you also open docker-compose.yml and look for Mailhog, you'lll see that there are two ports configured. The first one is the smtp port, and the second one points to the Mailhog dashboard. We can access that dashboard by opening the browser and going to localhost:8025. And here it is.

[00:53] Let's create an email, send it, and see if this thing works. Open your terminal and type in sail artisan make:mail HelloMail --markdown=emails.hello, the name of the mail, and to make it markdown based, we can do --markdown=emails.hello and this will create an email directory with a hello view.

[01:23] Let's open the hello view; resources, views, emails, hello, and let's just say "Hello, nice to meet you!" and save. Now to send it, let's open our web routes, and here inside the root url we can do Mail::to('druc@pinsmile.com')->send(new HelloMail()) - and let's make sure we import the mail facade.

[02:00] Now to test it, let's visit the root url. So I'll go to localhost, and we receive this error becase we forgot to set a sender address. To do that, go to your environment file, and here mail from address, set your e-mail.

[01:19] Now let's give it another go. And if we check Mailhog, here is the email. We can click it, view the html, the plai text, source, mime type, etc.