Ensuring that emails are sent, and are rendered exactly how I want them to be has always been a challenge. Sending an email is cumbersome, and requires a mail server being set up, and has to actually send an email to a recipient. Using MailCatcher with PHP/Laravel allows a developer to view sent mails and verify application mail functionality while excluding external factors such as mail servers. This setup assumes an IP of 192.168.10.10 is used to configure mail catcher, so replace this IP with the IP in your homestead.yaml. MailCatcher's Web site provides full documentation.
This process is intended for the latest Laravel Homestead box, and assumes your Homestead environment is already set up with the latest PHP7 Homestead box.
Before starting your Homestead box you'll want to update the Homestead config to forward MailCatcher's HTTP port. Add the following lines to your homestead.yaml file after the other forwarded ports, then start/restart your homestead box for update to take effect.
<pre> - send: 1080 to: 1080 </pre>
To start, you'll need to have Ruby installed, so type the following line within a terminal to your Homestead box.
<pre> sudo apt-get install -y ruby1.9.1-dev </pre>
The following lines are necessary to a dependency issue with an underlying dependency of MailCatcher, and you'll still get the above error, but mail catcher will run, so input into the Homestead terminal.
<pre> sudo gem install mime-types --version "< 3" sudo gem install --conservative mailcatcher </pre>
You may receive the following error, but because of the previous lines, MailCatcher will still function:
<pre> ERROR: Error installing mailcatcher: mime-types-data requires Ruby version >= 2.0" </pre>
For projects using PHP, or PHP frameworks and application platforms like Drupal, you can set PHP's mail configuration in your php.ini (/etc/nginx) to send via MailCatcher with by inserting the following line:
(You'll find php.ini in /etc/php7/fpm or /etc/php5/fpm if using PHP 5)
<pre> sendmail_path = /usr/bin/env catchmail -f email@example.com </pre>
Update Laravel project's configuration. If your mail settings are set using .env file, the following lines will need to replace the mail lines of your config.
<pre> MAIL_DRIVER=smtp MAIL_HOST=127.0.0.1 MAIL_PORT=1025 </pre>
Start MailCatcher with the IP as defined in the Homestead config with the following line.
<pre> mailcatcher --http-ip 192.168.10.10 </pre>
The command should output a success message:
<pre> Starting MailCatcher ==> smtp://127.0.0.1:1025 ==> http://192.168.10.10:1080 </pre>
MailCatcher can now be accessed using the URL 192.168.10.10:1080 in your browser. Simply send an email using PHP's mail function and it will be populated in MailCatcher's UI.
About the Author
Justin Hamm is a senior Web developer at Enola Labs, a custom Web and mobile app development company headquartered in Austin, Texas.