Background
MailHog is a fantastic utility for intercepting email on a hosted server and popular with the Laravel community. Alternatives for MailHog exists, e.g.:
Our recommendation is if you’re developing locally to use HELO due to it’s superior Laravel integration, for example it’s ability to show the view blades in the debug tab.
But what do you do when you want to catch mail on a Forge server? This guide is designed to give you a quick path for doing that. The guide consists of installing the software, creating a new Linux systemd service, and then adjusting the Forge firewall to make it work.
Installation
This must be performed as root
sudo apt-get -y install golang-go go get github.com/mailhog/MailHog
The default installation needs root and will install the MailHog binary in ~/go/bin/MailHog
To get it running on every startup, do the following two things:
1. Create a new systemd service
cat >/etc/systemd/system/mailhog.service <<EOL [Unit] Description=MailHog service [Service] ExecStart=/root/go/bin/MailHog [Install] WantedBy=multi-user.target EOL
2. Enable automatic startup for systemd service
systemctl enable mailhog
Starting Mailhog
You can now use systemctl start mailhog
to start MailHog or systemctl status mailhog
to see if it worked.
What Ports Do MailHog Use?
MailHog uses port 1025
for SMTP
and port 8025
for it’s HTTP
interface. These must be allowed on the Forge firewall before it will work.
Forge Firewall Configuration
Below is a screenshot of the Laravel Forge firewall configuration. Be sure to add both ports 1025
and 8025
to the Forge configuration.
2 thoughts on “How to install MailHog on Laravel Forge (Abbreviated Guide)”
Thanks for this really helpful article – it just worked first time for me.
However can I add that it’s adviseable to enable authentication as opening MailHog ports as above will make it open to anybody on the internet. In my case I’m testing a live site migration so I absolutely do not want to leak any emails.
See: https://github.com/mailhog/MailHog/blob/master/docs/Auth.md (The -auth-file= option can be added to the ExecFile= part of the systemd service file)
I’m also not certain that opening port 1025 is necessary unless you really want it to be available for SMTP from external sources. I’ll try with out first because my MailHog and my website are both hosted on the same Forge Server in this case.
Just to follow up on my previous comment….
You can definitely access SMTP via 127.0.0.1:1025 so long as the MailHog service is running on the same system as your site.
Furthermore, if you’re using Forge’s basic authentication to protect your site (N.B. using MailHog’s Auth as in my previous comment gets messy with Nginx reverse proxy!) then you can add it under a path in your Nginx config (example shamelessly stolen from: https://github.com/mailhog/MailHog/issues/117#issuecomment-1210001450 )
—
location ^~ /mailhog/ {
chunked_transfer_encoding on;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:8025/;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection “upgrade”;
proxy_http_version 1.1;
proxy_redirect off;
proxy_buffering off;
}
—