Catching Mail with MailCatcher

MailCatcher

MailCatcher is a powerful and easy to use Ruby gem that acts as an SMTP server that allows you to inspect emails that your application is sending out in your dev environment.

Installation

Installation is pretty simple, these are the commands that I used on my Fedora 29 install to get it up and running. If Ruby is already installed on your machine, or if you've already installed other Ruby gems, you may not need the first few lines.

sudo dnf install ruby
sudo dnf group install "C Development Tools and Libraries"
sudo dnf install ruby-devel zlib-devel

# Redhat-rpm-config and libsqlite3x-devel are the mailcatcher specific requirements
sudo dnf install redhat-rpm-config libsqlite3x-devel
gem install mailcatcher

After everything installs properly, from a terminal run MailCatcher.

mailcatcher

MailCatcher has options that you can pass it if you need to change the SMTP port or the port the web front end uses, these can be viewed by running mailcatcher --help.

Now that MailCatcher is up and running, we're ready to send it some e-mail.

Symfony

The default MAILER_URL is NULL in a Symfony project, which is rational setting that ensures that you aren't sending out emails while running a project in the dev environment (which I've done, on more than one occasion). However, there are occasions when you want to verify that an e-mail is being sent or you want to inspect the contents of that email. Symfony, of course, does provide ways through the Web Debug Toolbar to view the contents of sent e-mail and to override the intended recipient, so you aren't spamming users with dev emails (but you have to check the header if you want to see who the intended recipient was). However, I find that using MailCatcher has a more developer friendly UI and requires less configuration.

In your Symfony .env file change the MAILER_URL to point to the instance of MailCatcher.

# .env file
MAILER_URL=smtp://localhost:1025

Now all that is left to do is to point a browser to http://127.0.0.1:1080/ to see any emails that are sent from your Symfony project.

Of course, one other benefit of using MailCatcher over the built-in Symfony tools for catching dev emails is that you aren't limited to using this for just Symfony projects. You can drop this in anywhere that has a configurable SMTP url setting.

If you liked this post, you can subscribe to the rss feed or follow me @ToddEidson on Twitter to be notified of future blog posts.

Date Published: 31 March, 2019

Tags: symfony general programming

About Todd

Full stack application developer. Life-long learner. Pragmatic programmer. Believer in clean coding. Proponent for extensible and reusable code. Hobbies include (very) amateur photography, collecting old jazz records and going to live music performances.

North Central Ohio, US
toddeidson[dot]info

Obligatory Disclaimer

All opinions are my own, probably wrong, and subject to change without notice.

© 2017-2019 Todd Eidson. All content is licensed under the Creative Commons Attribution-ShareAlike 4.0 International License.

Hosted on linode