Using MailHog as my default MTA
I'm using Linux on all my machines and that includes desktop systems, not only servers.
Those desktop machines have no reason to send mails to the internet directly. When I want to send an E-Mail I will use a remote SMTP or just log into the GMail interface. My laptop might not even have an Internet connection some of the time.
Nonetheless having a local MTA1) is useful when developing. Mailhog is a good solution for that. It's a single binary that implements an SMTP server and a simple web interface to see any mail that was “sent” through this server. Mails aren't actually sent anywhere of course.
So here's a quick guide on how I set up mailhog for my dev machines.
Before we start, here are my requirements:
- have mailhog running on port
25, so anything assuming SMTP on
localhost:25exists will work
- have the web interface on a different port than the default
8025, to not interfere with docker-compose setups that expose their own mailhog there
- make PHP work with it
Installing is super simple. Either download the binary or use your distro's package. I'm doing the latter:
$> yay -S mailhog
Next reconfigure the ports by adding some environment variables. You can extend the standard systemd service using
systemctl edit mailhog.service – this creates an override file.
[Service] Environment="MH_SMTP_BIND_ADDR=0.0.0.0:25" Environment="MH_UI_BIND_ADDR=0.0.0.0:8026" Environment="MH_API_BIND_ADDR=0.0.0.0:8026" User=root
Note: I'm running mailhog as root here, because I want to bind to a privileged port.
Finally, PHP needs a sendmail path. Thanks to the power of go modules, Mailhog includes the mhsendmail tool accessible via command line argument
sendmail_path = "/usr/local/bin/mailhog sendmail --smtp-addr=localhost:25"
That's it. After restarting the services, mailhog is running on port 25 and the mails can be seen at http://localhost:8026.