Very similar to Mailtrap but means you can host internally. It also boasts a full Swagger based API from which a client library can be generated for your language of choice.
Go, Mailhog acts as a local SMTP server and captures all emails without them reaching the true recipient (for testing purposes).
This means you can then use the API to perform test assertions around email sending in your application.
Command Line Installation¶
First we need to install
Go. Here is an example guide.
Go get MailHog¶
go get github.com/mailhog/MailHog to install MailHog.
Confirm installation by running the help
Go get mhsendmail¶
mhsendmail is a sendmail replacement to forward mail to an smtp server.
go get github.com/mailhog/mhsendmail
To send a message from the commandline you can use:
1 2 3 4 5 6 7
mhsendmail firstname.lastname@example.org <<EOF From: App <email@example.com> To: Test <firstname.lastname@example.org> Subject: Test message Some content! EOF
MailHog to run MailHog.
If using default config the SMTP server will be on
1025 and the dashboard will be on
Set php.ini to use mhsendmail¶
Set the following line in your php.ini file.
sendmail_path = /usr/local/bin/mhsendmail
PHP should now be using MailHog! To test this on a testing environment you can use the following commands:
MailHog Run - Keep in a separate tab/window¶
Interactive Php shell¶
You can use Docker to get an instance up and running very quickly with a one liner.
docker run -d -p 1025:1025 -p 8025:8025 mailhog/mailhog
1 2 3 4 5 6 7
version: "3" services: mailhog: image: mailhog/mailhog:latest ports: - 1025:1025 - 8025:8025
For emails to be sent via Mailhog, you need to configure your application (or PHP) to use it as an SMTP server.
By default, Mailhog listens on port
1025 for SMTP and port
8025 for the frontend dashboard and API.
When used with Docker/Compose, you can access both the SMTP and HTTP server by localhost with the specified port. E.g.
localhost:1025 #SMTP Server localhost:8025 #HTTP Frontend