How to setup Mailhog in Ubuntu PHP Magento 2?

MailHog tools are used to test mail functionality from your local development environment.

If you are using Linux/Ubuntu OS with PHP language and want to set up MailHog for your local setup. MailHog tool is the best suited for your system to test web application mails.

First You need to install MailHog using the Command Line Interface (CLI),

sudo apt-get update
sudo apt-get install golang-go

Install MailHog,  go get github.com/mailhog/MailHog

Mhsendmail is a Send mail utility created to send emails via MailHog.

Install mhsendmail,  go get github.com/mailhog/mhsendmail

copy MailHog and mhsendmail to the /usr/local/bin path,

sudo cp /home/{system_user}/go/bin/MailHog /usr/local/bin/mailhog
sudo cp /home/{system_user}/go/bin/mhsendmail /usr/local/bin/mhsendmail

Just replace system_user with your system username.

You can get the current username of the system in Linux by command,

echo "$USER"
rakesh

Now Modify the php.ini to setup MailHog path,
I am working with PHP 7.4 version, php.ini path location would be /etc/php/7.4/apache2/

From Command Line, Run the command to modify the file,
sudo gedit /etc/php/7.4/apache2/php.ini

Now update ;sendmail_path = with the given line. Remove the semicolon from the starting and update the path,
sendmail_path = /usr/local/bin/mhsendmail
Save the file and restart the PHP to get it to work.

Final Step:  Go To CLI, run the command,

rakesh@rakesh-ThinkPad-E14-Gen-2:/var$ mailhog
2021/11/25 18:55:17 Using in-memory storage
2021/11/25 18:55:17 [SMTP] Binding to address: 0.0.0.0:1025
[HTTP] Binding to address: 0.0.0.0:8025
2021/11/25 18:55:17 Serving under http://0.0.0.0:8025/
Creating API v1 with WebPath: 
Creating API v2 with WebPath: 
[APIv1] KEEPALIVE /api/v1/events

You will see URLs like http://0.0.0.0:8025/
Open this URL in the browser and you will be able to see the web interface of  Mailhog.

You can see more details about MailHog by MailHog README.md link.