MailHog is an email testing tool for developers. It almost like Mailtrap but this one is hosted in your own server. This give more privacy and freedom when you are testing your mail notifications.
Caution : This project has been abandoned but it's okay, we have alternative, MailPit which are forked from MailHog and the installation should be almost similar.
This installation is based on following:
- Linux ubuntu server to host your mailserver
- Laravel to test send your mail
- Nginx to create webproxy to MailHog port
Okay, lets go !
1. Installing MailHog into your ubuntu server.
# Golang is require to install MailHog
sudo apt-get -y install golang-go
# Downloading MailHog binary to current directory
go install github.com/mailhog/MailHog
# moves downloaded binary to '/usr/local/bin/mailhog' directory
sudo mv ~/go/bin/MailHog /usr/local/bin/mailhog/MailHog
2. Configure MailHog using JSON file
# we will store related mailhog file here
cd /usr/local/bin/mailhog
# create a json file 'mailhog-outgoing.json'
sudo nano mailhog-outgoing.json
and paste the following json :
{
"server name": {
"name": "development mail",
"host": "...",
"port": "1025",
"email": "...",
"username": "test",
"password": "test123",
"mechanism": "PLAIN"
}
}
here you can set up your password, username, etc. for access by laravel application which you can refer here.
3. Running MailHog
You can running mailhog manually by CLI provided but since we want to run it 'forever', we can create a linux service for that utilising linux 'systemctl'
# create a service file in this case, i named it as MailHog.service (capital 'M, H' here)
sudo nano /etc/systemd/system/MailHog.service
and paste the following:
[Unit]
Description=Mailhog Local Mail SMTP
After=network.target
[Service]
Type=simple
User=ubuntu
ExecStart=/usr/bin/env /usr/local/bin/mailhog/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 -storage maildir -maildir-path /usr/local/bin/mailhog/mails/ -outgoing-smtp /usr/local/bin/mailhog/mailhog-outgoing.json > /dev/null 2>&1 &
[Install]
WantedBy=multi-user.target
the important part here is which user
you want to run this service and the command execStart
which calling Mailhog CLI with the mailhog-outgoing.json configuration. Other flag are kept default and can be refer here
Start the service by:
# the name of service mail is sensitive
# check does systemctl detect the new service file
sudo systemctl status MailHog
# if yes, run the new service
sudo systemctl start MailHog
4. Sending a Mail
By now, the MailHog should be running and can be test. Since I'm already has a Laravel project hosted using mailtrap beforehand, I just need to update my .env to use the local mailhog mail server like this:
MAIL_DRIVER=smtp
# localhost
MAIL_HOST=0.0.0.0
# depending on value you set up json
MAIL_PORT=1025
MAIL_USERNAME=test
MAIL_PASSWORD=test123
MAIL_ENCRYPTION=null
Then try to send an email.
5. Opening MailHog Inbox
MailHog inbox is hosted on port 8025 (default). To open the inbox by browser, we can directly go to this port using server-ip:8025. But since my server is behind a domain, I need to proxy through port 80 to port 8025. To do this, use this nginx server block template file:
server {
server_name mailtrap.mydomain.com;
listen 80;
listen [::]:80;
if ($host != "mailtrap.mydomain.com") {
return 404;
}
location / {
proxy_pass <http://localhost:8025>;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Client-Verify SUCCESS;
proxy_set_header X-Client-DN $ssl_client_s_dn;
proxy_set_header X-SSL-Subject $ssl_client_s_dn;
proxy_set_header X-SSL-Issuer $ssl_client_i_dn;
proxy_read_timeout 1800;
proxy_connect_timeout 1800;
chunked_transfer_encoding on;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_redirect off;
proxy_buffering off;
}
}
where mailtrap.mydomain.com
is the subdomain you set for mailhog inbox.
Top comments (0)