Installing MailCatcher in Laravel Homestead

MailCatcher is a Ruby gem that acts as a sendmail/postfix replacement and displays outgoing emails in a friendly web GUI. It is useful if you want to run someone else’s code and be sure of what emails are being sent.

Here’s how to use it with PHP and Laravel Homestead.

1. We need to access the website created by MailCatcher which is available on a custom port. ON your host machine, edit ~/Homestead/scripts/homestead.rb, add 1080 => 1080 to the default_ports section.
You’ll need to re-provision the Homestead box with: homestead reload --provision (this will re-read the settings and apply the additional port mapping).
You will also need to add a line to your hosts file to map the URL to IP Address. Edit /etc/hosts and add something like this 192.168.10.10 mailcatcher.app.

2. Access the guest machine via SSH and install MailCatcher:

sudo apt-get update
sudo apt-get install ruby-dev
sudo gem install mailcatcher

3. Edit the PHP configuration /etc/php/7.0/fpm/php.ini to change the sendmail path.

sendmail_path = "/usr/bin/env /usr/local/bin/catchmail -f some@from.address"

Note that the -f must be included for it to send if you aren’t specifying a from address yourself.
Restart PHP FPM with sudo service php7.0-fpm restart.

4. Run MailCatcher with: mailcatcher --foreground --http-ip=0.0.0.0

Now any emails your PHP code tries to send will be caught by MailCatcher and displayed at: http://mailcatcher.app

This article uses Homestead 0.5 which runs PHP 7. I’m not sure if more recent versions will work but give it a try and let me know in the comments.

Update 28/09/17 this also works with Homestead 3 running PHP 7.1.

Homestead 3 comes with MailHog preinstalled but this can’t render embedded images in emails whereas MailCatcher can. Follow the instructions above but you’ll need to run sudo service mailhog stop before you can start MailCatcher.

Running on boot

nano /etc/systemd/system/mailcatcher.service

[Unit]
Description=Ruby MailCatcher
Documentation=http://mailcatcher.me/

[Service]
# Ubuntu/Debian convention:
EnvironmentFile=-/etc/default/mailcatcher
Type=simple
ExecStart=/usr/local/bin/mailcatcher --foreground --ip 1.2.3.4 
# (use whatever your server ip is)

[Install]
WantedBy=multi-user.target

then
systemctl enable mailcatcher.service to set the service to be auto-started at boot
systemctl start mailcatcher.service to start the service manually
systemctl status mailcatcher.service to see the service status

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA - answer the question below to prove you are human *

This site uses Akismet to reduce spam. Learn how your comment data is processed.