Setup Mailcatcher in Vagrant box20 Nov 2015
I am currently refactoring some mailings in an application and as you probably know, debugging mails is a PITA. Luckily I stumbled across Mailcatcher, which runs a simple mail server and can catch mails you send to it and display them in a web gui.
Since it runs on localhost, it’s a bit tricky to set it up in a Vagrant box, where your box probably has it’s own hostname (e.g. myapp.dev). This blog post is written for Ubuntu 14.04 with PHP and apache.
Here’s how to set up Mailcatcher in your Vagrant box.
First you need to provision your Vagrant box to install Mailcatcher. If you don’t already have a provisioning script, you can add one to your Vagrantfile like this:
Then create vagrant/provision.sh in your project directory:
Note that I replace the default sendmail path in the php.ini* for apache with Mailcatcher’s own catchmail, which causes all mails to be sent there. If you also send mails from the command line (e.g. cronjobs), you’ll also need to replace sendmail in /etc/php5/cli/php.ini.
If you’re sending your mails with SMTP, you have to configure smtp_port instead.
This script is only run automatically when your box is first setup. You can run it manually by calling:
*you could also just configure this in your apache config like:
But I feel more comfortable knowing that it’s in my php.ini and won’t suddenly start sending mails if I change around my apache config and suddenly that rule is in the wrong Directory or something.
Starting Mailcatcher on ‘vagrant up’
Just add this to your Vagrantfile to start Mailcatcher when you run vagrant up:
Accessing Mailcatcher web gui
Since the Mailcatcher web gui, where you can view all captured mails, runs on localhost, it’s not accessible via your app or website frontend by default. This is how I configured my Apache virtualhost conf to enable this (only relevant parts):
Then restart your apache and you should be able to access myapp.dev/mailcatcher in the browser. Now any mails your app sends should be instantly displayed there instead of actually being sent.