Using Mailhog For Email Testing

When we’re troubleshooting issues for clients we create a local copy of their site in order to prevent changes being made to their production site. When doing this it often times requires testing email being sent from their WordPress site.
For our local testing setup we make use of MailHog. MailHog is used to intercept email so nothing is ever actually sent out, this is great especially when testing WooCommerce orders being placed or contact forms.

MailHog which was initially inspired by MailCatcher is also a great alternative. We decided on MailHog due to the straightforward setup that it provides, especially while using Valet.

Installing MailHog on macOS is accomplished by using Homebrew, and can be done very simply using the following command.

brew install mailhog

Now that MailHog has been installed, you can access its web interface from the following URL in your browser of choice http://127.0.0.1:8025/
To fully utilize MailHog you also have to install the sendmail alternative from them which is called mhsendmail. This will make everything work great together.
Installing mhsendmail is more or less just as easy as MailHog but requires a few more steps. Once again if performing this on macOS you first need to install Go with the following Homebrew command

brew install go

Now that go is installed you can proceed with installing mhsendmail with the following command.

go get github.com/mailhog/mhsendmail

The previous command will grab a copy of mhsendmail and install it within the “go” folder inside your users home directory. On a side note, all other Go applications will be placed in this future during future installs as well.
The last part of the process for setting up mhsendmail within PHP on your system will be noting where mhsendmail has been installed. In my personal case, it was installed in the following directory.

/Users/shelby/go/bin/mhsendmail

Now that you know the path to mhsendmail you will need to edit your php.ini to have it make use of mhsendmail. If you do not already know the location of php.ini on your system you can use the following command within the terminal.

php -i | grep ‘php.ini’

You should see similar output as I had on my system below, but it could vary depending on how and where you have PHP installed on your machine.

Loaded Configuration File => /usr/local/etc/php/7.2/php.ini

Now you can edit your php.ini and search for “sendmail_path”, if you have not made changes to your php.ini in the past or are running a normal install it will likely be commented out. Once you have located the “sendmail_path” you will need to uncomment it and change it to the following in order to make sure that PHP is using mhsendmail when attempting to sendmail.

sendmail_path = “/Users/shelby/go/bin/mhsendmail”

Once you have saved your php.ini you will then need to restart PHP on your machine to make use of this change. In my case since I am using Valet, I had to issue the following command.

valet restart

After everything has been restarted you can now test your WordPress install with MailHog. A simple way to test this would be with the Lost your password? link on the WordPress login page, you should now see the Password Reset Request email within the MailHog web page.