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

@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