How to install and start Mailhog on Ubuntu 20.04
January 14th, 2022
When working on a local version of a website it can be difficult to test email content, especially if you are using a virtual enviromnent such as Docker or Vagrant. Solutions range from setting up a local MTA using Postfix to using a third-party service such as Mailtrap or tesmail.app. Wouldn't it be great if you could install something locally that does the same?
I've got some good news for you - you can!
Mailhog is an email testing tool is very easy to install. It sets up a local SMTP server and provides a web interface for you to see the emails. Configuring web applications like Laravel or Wordpress to send emails to Mailhog is easy.
I often use Vagrant for developing on older projects and often have a need to send emails, so I use Mailhog. Here's how I install Mailhog on Ubuntu 20.04 and configure it to start on boot.
To use Mailhog we need to install 'Go'.
sudo apt-get -y install golang-go go get github.com/mailhog/MailHog sudo mv ~/go/bin/MailHog /usr/local/bin/Mailhog
This will install 'Go', download the Mailog binary, and then moves it to
Starting Mailhog automatically on boot
To run Mailhog automatically when the Vagrant box is started we need to set up a Systemd service.
[Unit] Description=Mailhog After=network.target [Service] User=vagrant ExecStart=/usr/bin/env /usr/local/bin/Mailhog > /dev/null 2>&1 & [Install] WantedBy=multi-user.target
Note: this will run Mailhog as the
Check it all works ok:
sudo systemctl status mailhog
If good, start the service
sudo systemctl start mailhog
Now it should start on boot.
Check it by going to the vm IP on port
:8025, e.g. http://192.168.56.5:8025.
Configuring Laravel to use Mailhog
To send email from Laravel to Mailhog change the
MAIL_DRIVER=smtp MAIL_HOST=0.0.0.0 MAIL_PORT=1025 MAIL_USERNAME=null MAIL_PASSWORD=null MAIL_ENCRYPTION=null