Magento 2 – How to setup Mailhog services? (Test emails on local for PHP and Magento 2) PHP 8.1 and PHP 7.4

MailHog is an email testing tool for developers, Configuration and use are very easy, It will provide the UI to read the emails.
It will not send any email to anyone, it just keeps your all email local and you can easily read them.

Here are the steps to setup the MailHog in Ubuntu and WSL (Windows Linux)

$ sudo apt-get update

Get your current username

$ echo "$USER"
Saphal

$ sudo apt-get install golang-go
$ go get github.com/mailhog/MailHog

copy MailHog to the /usr/local/bin

$ sudo cp /home/USER/go/bin/MailHog /usr/local/bin/mailhog

NOTE: USER is your Linux username

$ go get github.com/mailhog/mhsendmail

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

$ sudo cp /home/USER/go/bin/mhsendmail /usr/local/bin/mhsendmail

NOTE: USER is your Linux username

After this installation, You need to configure the email service to your PHP, Follow the below steps:

$ sudo nano /etc/php/8.1/apache2/php.ini

NOTE: 8.1 is my PHP version you can choose yours

Find sendmail_path in your php.ini

remove semicolon (;) from starting of the line

From
;sendmail_path = /usr/local/bin/mhsendmail

To
;sendmail_path = /usr/local/bin/mhsendmail

Now you are done with all setup, now go to CLI and run the below command

$ mailhog

After running the above command you will get the below output:

~$ mailhog
2023/01/30 11:43:45 Using in-memory storage
2023/01/30 11:43:45 [SMTP] Binding to address: 0.0.0.0:1025
[HTTP] Binding to address: 0.0.0.0:8025
2023/01/30 11:43:45 Serving under http://0.0.0.0:8025/
Creating API v1 with WebPath:
Creating API v2 with WebPath:
[APIv1] KEEPALIVE /api/v1/events
[APIv2] GET /api/v2/jim
[APIv2] GET /api/v2/messages
[APIv2] GET /api/v2/websocket
[APIv2] GET /api/v2/messages
[APIv1] KEEPALIVE /api/v1/events

NOTE: FOR WSL users if the URL (http://0.0.0.0:8025/) is not working try with the local host

localhost:8025

Please comment if any query

Source – https://github.com/mailhog/MailHog