Setup MailHog with Laravel Valet

tooling
Table of Contents

Before we begin, you need to have Homebrew installed. If you don't, visit https://brew.sh/ for instructions on how to get it installed.

If you're not using Laravel Valet, you can skip the last section of this tutorial and use the localhost / 127.0.0.1 domain instead.

To install MailHog, run the follow commands in your terminal:

brew install mailhog

This command will install MailHog on your system, but won't enable the service.

To enable the service, run the command below:

brew services start mailhog

This will instruct Homebrew to setup a background service so that MailHog is always running on your machine. You won't need to manually start anything, as long as Homebrew is running.

Now, you can visit 127.0.0.1:8025 in a browser and you should be greeted with the MailHog application:

MailHog image

To get MailHog working with your Laravel application, update the following keys in your .env file:

MAIL_DRIVER=smtp
MAIL_HOST=127.0.0.1
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

The reason the ports are different to the MailHog UI is that the SMTP server is running on port 1025, where as the HTTP server is running on 8025.

If you send an email from your Laravel application now, you will see it pop up in the MailHog interface. You can use the code snippet below inside of Laravel Tinker to test:

Mail::raw('MailHog', fn ($message) => $message->to('[email protected]')->from('[email protected]'));

Laravel Valet makes this process super easy. All you need to do is run the following command in your terminal:

valet proxy mailhog.test http://127.0.0.1:8025

This command will create an Nginx configuration file for the domain mailhog.test, proxying all requests to that domain through to the MailHog HTTP server.

It will also setup an SSL certificate, just like valet secure does for your normal Laravel sites.

Enjoyed this post or found it useful? Please consider sharing it on Twitter.