Skip to content

Instantly share code, notes, and snippets.

@dipenparmar12
Last active August 9, 2024 10:13
Show Gist options
  • Save dipenparmar12/4e6cd50d8d1303d5e914742f62659116 to your computer and use it in GitHub Desktop.
Save dipenparmar12/4e6cd50d8d1303d5e914742f62659116 to your computer and use it in GitHub Desktop.
Mailhog installation guide (Linux)

Mailhog

Video Tutorial

1. Install GoLang

Install

Mailhog Requires Go 1.4+ to run so we will install GO language in system.

sudo apt install golang-go -y

Cross verify Go language is successfully installed.

Verify installation

go version

we will see output something like this

go version go1.13.8 linux/amd64

If you found any installation related issues please visit: https://golang.org/doc/install

2. Install Mailhog

Install Mailhog

Downloading & installation

go install github.com/mailhog/MailHog@latest

go get github.com/mailhog/MailHog // this command is deprecated as of now

To start the MailHog run:

~/go/bin/MailHog

Copy Mailhog to bin directory

sudo cp ~/go/bin/MailHog /usr/local/bin/Mailhog

Open terminal and start mailhog server

Mailhog

we will see something lke below

[CurrentDate] [TIME] Using in-memory storage
[CurrentDate] [TIME] [SMTP] Binding to address: 0.0.0.0:1025
[HTTP] Binding to address: 0.0.0.0:8025
[CurrentDate] [TIME] Serving under http://0.0.0.0:8025/
Creating API v1 with WebPath: 
Creating API v2 with WebPath: 

Go to http://0.0.0.0:8025/ where we will receive all Emails. and keep open terminal until want to stop it.

3. Setup PHP with Mailhog

Get php.ini directory

php --ini

We will see below output.

Configuration File (php.ini) Path: /<direcotry>/etc
Loaded Configuration File:        /<direcotry>/php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

Modify php.ini by following command.

sudo sed -i "s/;sendmail_path.*/sendmail_path='\/usr\/local\/bin\/Mailhog sendmail [email protected]'/" /<direcotry>/php.ini

Or we may modify php.ini manually, Set sendmail_path to '/usr/local/bin/Mailhog sendmail [email protected]'

sendmail_path='/usr/local/bin/Mailhog sendmail [email protected]'

4. Testing Email

Create index.php file, with following content to fire mail

<?php
    $from = '[email protected]';
    $to = '[email protected]';
    $x = mail($to, 'subject' . time(), 'Test email working', 'From: ' . $from);
    var_dump($x); // true means successfull.
?>

If we see something like following means everyting is working fine and ready to go. Visit http://0.0.0.0:8025/ where we receive all mails.

/<Direcotry>/index.php:6:boolean true

5. Create Mailhog service [optional]

Now we are going make MailHog service, So we dont have to start every time Mailhog after rebooting system.

open terminal and copy paste below content.

sudo tee /etc/systemd/system/mailhog.service <<EOL
[Unit]
Description=Mailhog
After=network.target
[Service]
User="$USER" 
ExecStart=/usr/bin/env /usr/local/bin/mailhog > /dev/null 2>&1 &
[Install]
WantedBy=multi-user.target
EOL

To check status service is loaded successfully.

sudo systemctl status mailhog

● mailhog.service - Mailhog
     Loaded: loaded (/etc/systemd/system/mailhog.service; disabled; vendor preset: enabled)
     Active: inactive (dead)

To start service

sudo systemctl enable mailhog

Reboot system and visit http://0.0.0.0:8025/

We done, That's it. Thanks

@openmindculture
Copy link

openmindculture commented May 26, 2021

This will not set up the service correctly.

How it works for me:

sudo tee /etc/systemd/system/mailhog.service <<EOL
[Unit]
Description=Mailhog
After=network.target
[Service]
User="$USER" 
ExecStart=/usr/bin/env /usr/local/bin/mailhog > /dev/null 2>&1 &
[Install]
WantedBy=multi-user.target
EOL

@dipenparmar12
Copy link
Author

@openmindculture Thanks for your attention.

@ksamel
Copy link

ksamel commented Sep 17, 2021

How we can use the API ? I'm looking for a automated solution where I can test the emails received in the inbox through mailhog API.

@Papoel
Copy link

Papoel commented Dec 16, 2021

Hello, the command go install github.com/mailhog/MailHog is deprecated, use the command go install github.com/mailhog/MailHog@latest instead.

My Linux version (with uname -a) :
Linux papoel 5.13.0-22-generic #22-Ubuntu SMP Fri Nov 5 13:21:36 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

I hope that will be help.

@SebastianOpiyo
Copy link

@Papoel Thanks, that was really helpful.

@dipenparmar12
Copy link
Author

Hello, the command go install github.com/mailhog/MailHog is deprecated, use the command go install github.com/mailhog/MailHog@latest instead.

My Linux version (with uname -a) : Linux papoel 5.13.0-22-generic #22-Ubuntu SMP Fri Nov 5 13:21:36 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

I hope that will be help.

Thank you Papoel

@Papoel
Copy link

Papoel commented Feb 17, 2023

I'm happy it's still useful and relevant.

@com2
Copy link

com2 commented Feb 27, 2023

In Debian 11 (Bullseye) I get:

$ go install github.com/mailhog/MailHog@latest
package github.com/mailhog/MailHog@latest: cannot use path@version syntax in GOPATH mode

It only worked with $ go get github.com/mailhog/MailHog

The systemd script would not start and needed to be changed to:

tee /etc/systemd/system/mailhog.service <<EOL 
[Unit]
Description=Mailhog
After=network.target
[Service]
ExecStart=/usr/local/bin/MailHog \
  -api-bind-addr 127.0.0.1:8025 \
  -ui-bind-addr 127.0.0.1:8025 \
  -smtp-bind-addr 127.0.0.1:1025

[Install]
WantedBy=multi-user.target
EOL

Please note I removed user and I changed command name (with two capitals).

@renzocastillo
Copy link

In case someone needs to apply the fix to several PHP versions you could use this script:

#!/bin/bash

# Get the list of php.ini files
php_ini_files=$(find /etc/php/ -name php.ini)

# Loop through each php.ini file and apply the fix
for ini_file in $php_ini_files; do
    sudo sed -i "s|^;\?sendmail_path.*|sendmail_path = '/usr/local/bin/Mailhog sendmail [email protected]'|" "$ini_file"
    
    # Check if the sed command was successful
    if [ $? -eq 0 ]; then
        echo "Fix applied to $ini_file"
    else
        echo "Error applying fix to $ini_file"
    fi
done

Then you can do ./fix_php_ini_mailhog.sh and get that fix applied.

In case you are using a php-fpm service, you would also need to restart the PHP service like sudo service php7.4-fpm restart to get the change reflected properly.

@neoporcupine
Copy link

neoporcupine commented Apr 25, 2024

Using an Oracle 8 VM (a redhat variant, it's my institution's SOE)
Note: there are alternate go install options for other versions of the go-toolset

yum install go-toolset
go install github.com/mailhog/MailHog@latest
go install github.com/mailhog/mhsendmail@latest
cp ~/go/bin/MailHog /usr/local/bin/
cp ~/go/bin/mhsendmail /usr/local/bin/
firewall-cmd --add-port=8025/tcp --zone=public --permanent
firewall-cmd --add-port=1025/tcp --zone=public --permanent

I altered IP below to use my fixed locally assigned IP for the VM
however, recommended is 0.0.0.0 ... or 127.0.0.1 although I could not get 127.0.0.1 to operate correctly
if you are using within a docker then you will need -p 8025:8025 in your docker config
nano /etc/systemd/system/mailhog.service
#-- FILE: /etc/systemd/system/mailhog.service
[Unit]
Description=Mailhog
After=network.target
[Service]
User=root
ExecStart=/usr/bin/env /usr/local/bin/MailHog \
-api-bind-addr 0.0.0.0:8025 \
-ui-bind-addr 0.0.0.0:8025 \
-smtp-bind-addr 0.0.0.0:1025 > /dev/null 2>&1 &
[Install]
WantedBy=multi-user.target
#-- END FILE

systemctl status mailhog
systemctl restart mailhog
systemctl enable mailhog

After changing the above service:
systemctl daemon-reload

nano /etc/php.ini
#-- FILE: /etc/php.ini
sendmail_path = '/usr/local/bin/mhsendmail --smtp-addr="0.0.0.0:1025"'
#-- END FILE

systemctl restart httpd
systemctl restart php-fpm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment