MailHog as a service

Running MailHog on Debian 12

0 comment(s)

MailHog is an essential tool for intercepting emails sent by a development application. But I don't want to manage its startup and shutdown manually; it needs to be always available at system boot, especially in my dev environment. Here's how to make MailHog a proper service under Debian 12.

πŸ“¦ 1. Prerequisites


Check if you already have MailHog installed on your non-Dockerized Debian (Docker can also be a solution):

Bash
which mailhog

If not, refer to my article:

PHP / WEB DEV – Sending Emails Without SMTP

🎯 2. Objective


Launch MailHog automatically at each system boot without manual intervention, just like a good classic Debian service.

πŸ‘€ 3. Step 1: Create a Dedicated User


Avoid running MailHog as root. Let's create a system user with no shell or home directory:

Bash
sudo useradd --system --no-create-home --shell /usr/sbin/nologin mailhog

βš™οΈ 4. Step 2: Create the systemd Service File


Create a mailhog.service file in the dedicated services folder:

Bash
sudo nano /etc/systemd/system/mailhog.service

Add the following content for our service:

Bash
[Unit]
Description=MailHog service
After=network.target
[Service]
# or your path, e.g. the one from `go install`: ~/go/bin/mailhog
ExecStart=/usr/local/bin/mailhog
Restart=always
User=mailhog
Group=mailhog
Environment="MH_API_BIND_ADDR=0.0.0.0:8025" "MH_UI_BIND_ADDR=0.0.0.0:8025" "MH_SMTP_BIND_ADDR=0.0.0.0:1025"
[Install]
WantedBy=multi-user.target

πŸ’‘ Here, we expose:

MailHog's web/API interface on port 8025
The SMTP server on port 1025

πŸ” 5. Step 3: Enable and Start the Service


Reload systemd and enable the service to start at boot:

Bash
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl enable mailhog
sudo systemctl start mailhog

βœ… Step 4: Check That It’s Working


A quick check to verify if our service is running:

Bash
systemctl status mailhog

You should see an active status.

If needed, view the logs in real-time:

Bash
journalctl -u mailhog -f

πŸš€ Result

MailHog starts automatically at each reboot. You can access the web interface at:

http://localhost:8025

Next, configure your application to send emails via localhost:1025. πŸ›‘οΈ Security Bonus

If needed, you can restrict access to the web interface if your server is exposed. MailHog does not include built-in authentication, so using ufw, a password-protected NGINX proxy, or a firewall is recommended.

You may also like:

Comments

No approved comments yet.

Sign in with a commenter account to post a comment. Sign in.