add-mailcatcher-to-your-scotchbox
November 13, 2018 21:22ScotchBox is a brilliant starter Vagrant for web development work. I have gone away from using a single dev server (Ubuntu Server in a VirtualBox VM) after I found out about Vagrant boxes, they are really cool isolated virtual development environments that mean if you are working on different projects you don't need to install all the things on one server, you can have an environment per project which includes the usual LAMP stuff (or whatever it is for the project you are working on). It can be shared by any one else working on the same project as you, so they don't need to spend time setting up their own environment they can just grab the box and run vagrant up
and it'll automagically set it up for them so they can begin contributing to your project.
Aside from what's already included in the ScotchBox I found that I was missing something to help setup my environment, a mail server. In the past, I've always been very nervous when trying to reproduce an issue locally or test something that involved emails being sent out, just in case I made a horrible mistake and ended up spamming lots of people with test emails. I remember having to do silly things like find and replace the To field in any emails being sent out and make sure it went to my personal account. However, I then came across Mailcatcher which is a really simple SMTP server which you can point your application to, and instead of emails being sent anywhere they are displayed in the mail catcher web page which you can access through http://server-ip:1080
.
How to install
I found a guide called Setting up mailcatcher on serversforhackers.com however it seemed to be a little dated so thought I'd document this process to help those who come across this situation going forward. In order to add this into my ScotchBox, I had to do the following (probably could/should be done via Puppet or something similar so it's not a manual task the next time around I setup my environment for another project).
First we install the dependencies for Mailcatcher which are sqllite and ruby
sudo apt-get install -y libsqlite3-dev ruby1.9.1-dev
Once that is done, we can actually install the Mailcatcher gem
sudo gem install mailcatcher
In order to test it now works, we can start Mailcatcher in the foreground and bind it to all IP addresses
mailcatcher --foreground --http-ip=0.0.0.0
You should now be able to go to http://server-ip:1080
and see the web page for Mailcatcher
Getting Mailcatcher to start on startup
In order to get Mailcatcher to start on system startup, I had to add it to upstart and so I created a file at '/etc/init/mailcatcher.conf' with the following content
description "Mailcatcher"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
exec /usr/local/bin/mailcatcher --foreground --http-ip=0.0.0.0
This relies on there being a symlink to the actual Mailcatcher executable, so to create that I ran sudo ln -s /home/vagrant/.rbenv/shims/mailcatcher /usr/local/bin/mailcatcher
after running which mailcatcher
as the vagrant
user to find the path to the Mailcatcher executable.
Once this is setup, you can manage Mailcatcher just like any other system service using sudo service mailcatcher start
and sudo service mailcatcher stop
etc to interact with it. Based on the run level defined in the script above (2), Mailcatcher will now start when the server starts up so there's no need to manually start it when you power up your vagrant box!