How to debug emails locally using MailHogApril 5th, 2020 by Rijad Husic
How do you test your emails when working locally or from a staging server? Usually with manually testing and sending emails to only specific email addresses, and try not to send emails to real customers/users. You really need to be careful with those so it ends up as a really slow and painful process. Wouldn’t it be awesome to have a way to intercept all outgoing emails in a testing/debugging SMTP server so only could see those? The solution to this problem is called MailHog.
What is MailHog
MailHog is an email tool with a debugging SMTP server. It intersects all outgoing emails sent from your server, in this case, your local machine. With Mailhog you get a web interface with the email inbox just like Gmail for example. MailHog is really portable and built with Golang so it’s really easy to install it and use it.
When to use it
MailHog is a tool made by developers for developers. You can use it every day while developing any app. You don’t need to set up a real SMTP and you avoid all of the configurations.
How to install MailHog
MailHog is written in Go, so you’ll need to check if Go is already installed by running `go` in your terminal. If it’s not installed you’ll need to run the following commands:
sudo apt-get install golang-go
echo "export GOPATH=$HOME/gocode" >> ~/.profile
Now we need to download MailHog from GitHub alongside with mhsendmail witch is the handler for forwarding PHP outgoing email to MailHog
go get github.com/mailhog/MailHog
go get github.com/mailhog/mhsendmail
Or if you are on windows you can download the corresponding release from https://github.com/mailhog/MailHog/releases
I’ll continue explaining how to install MailHog on Ubuntu system.
Next, we need to copy the binaries so they are available globally on the system. And connect PHP with MailHog.
sudo cp /home/rijadhusic/gocode/bin/MailHog /usr/local/bin/mailhog
sudo cp /home/rijadhusic/gocode/bin/mhsendmail /usr/local/bin/mhsendmail
As for the PHP we need to edit php.ini. Find where it says sendmail_path and edit the line so it matches the following
sendmail_path = /usr/local/bin/mhsendmail
And that’s it for the configuration. What we have left is to set it on bootup and test it out. Pretty easy right.
Starting MailHog after the system boot
In order to set MailHog run all the time and not worrying about it anymore you need to follow a few simple steps:
Create the following file /etc/systemd/system/mailhog.service and paste the following in it:
-api-bind-addr 127.0.0.1:8025 \
-ui-bind-addr 127.0.0.1:8025 \
Start the mailhog service and check if it works by
sudo systemctl start mailhog
Enable the service on bootup by:
sudo systemctl enable mailhog
Restart your PC and then verify that MailHog is running with the following command:
systemctl | grep mailhog
You should get a response that the service is loaded active and running MailHog.
In order to test MailHog open up the web interface in your browser at 127.0.0.1:8025.
Now run this simple PHP script from the CLI:
php -r "\$from = \$to = 'email@example.com'; \$x = mail(\$to, 'subject'.time(), 'Hello World', 'From: '. \$from); var_dump(\$x);"
And that’s it, now you don’t have to worry about sending emails to clients while working on your app. And you can use MailHog in your everyday work for testing emails.