Paulund
2017-08-31 #laravel

Using Mailhog With Homestead

Homestead is a package you can use in your Laravel development to create a local development environment for your application with all the services installed you can use to improve the workflow of your application.

Homestead will then create a new virtual machine with the following software installed.

  • Ubuntu 16.04
  • Git
  • PHP 7.01
  • Nginx
  • MySQL
  • MariaDB
  • Sqlite3
  • Postgres
  • Composer
  • Node (With Yarn, Bower, Grunt, and Gulp)
  • Redis
  • Memcached
  • Beanstalkd
  • Mailhog
  • ngrok

The service mailhog is very useful to use when testing email on your application. Normally when you need to test mail from your application you need to send the email to a client and check the content.

The problem comes when you do integration testing on your application with "real world" data you might come into the situation where an dev email can be sent to a real user. There are a few ways you can get round this one of the most common old fashion ways of doing this was inside the sending email class you check the environment and if local change the email to address to the site admin email address, making sure all outgoing email goes to the site admin in local.

This solution isn't ideal, if you have multiple developers they will all need access to this mailbox, if you use an smtp service these dev email could be costing your company money. Mailhog is the solution for your development emails, it will watch for outgoing traffic using SMTP and collect the email and store it locally instead of sending the email.

The email it's collected can be viewed in the mailhog UI so you can see all dev emails in a nice client UI. Once installed all you have to do is go to the IP of the machine on port 8025. 127.0.0.1:8025

Environment File

If you use Homestead for your Laravel development you already have mailhog installed and can use it straight away, all you have to do is change your mail environment settings in your .env to the following.


MAIL_DRIVER=smtp
MAIL_HOST=0.0.0.0
MAIL_PORT=1025
MAIL_USERNAME=testuser
MAIL_PASSWORD=password
MAIL_ENCRYPTION=null

Now when you send emails from Laravel you'll be able to see it straight away in mailhog.

Mailhog Github

To learn more about mailhog and keep up to date with any updates you can follow the package on github.

Mailhog