Skip to content

Mailcatcher Nginx Config

Nginx configs for Mailcatcher are out there, but if you’re like me you want something simple. I mean it’s Nginx, can’t be that hard right?

Chances are you’re here because you have a dedicated server, and want to setup mail catcher.  Well if you’re like me and spent HOURS looking for a ‘simple’ solution; I promise not to disappoint you.

Mailcatcher is something relatively new to me. It’s not something I’ve needed to use because my server’s IP is blocked by google already ( apparently my hosting has done something nefarious at one point ). Since I’m blocked anyhow I use a Gmail plugin for all my personal sites, so I know which sites can send email, while all the rest cannot.

If you’re not familiar with Mailcatcher you should be! it’s just as you’d expect; it catches all emails coming from your server and forwards them to a faux inbox for your viewing pleasure.

This helps tremendously if you’re working on plugins like WooCommerce which send automated emails, which is just what I needed this for. Plus it gives me the peace of mind to say “nope, no emails came from my server”. As a developer, we could all use some peace of mind.

The Setup

Your going to want to setup Mailcatcher, if you haven’t already. To do this, you need Ruby installed as well as SQLite.

I found this article from the most helpful in setting up mailcatcher on my Ubuntu server. Yes the article dates back to 2014, but the basic install is still the same.

Too be honest, I would go with the official installation, which is as follows:

  1. gem install mailcatcher
  2. mailcatcher
  3. Go to http://localhost:1080/
  4. Send mail through smtp://localhost:1025

IF that fails, give the article a go! The only problem I ran into with the default instructions was Ruby and SQLite wasn’t installed, which is where that article gave me a hand.

Okay so how about the Mailcatcher Nginx setup?

Looking at the official documentation you’ll see they provide you with the Apache config settings in order to run catchmail – pretty simple yea?

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

This got me thinking, “I need Mailcatcher configs for Nginx” so I went on a google-spree.

Chances are you’re here because you searched the same thing, if not similar; and you’ve probably seen a gist by raskhadafi.

But I’m here to tell you that, ITS NOT THIS HARD…

Its simpler than you think!

Now, I’ll be the first to admit, my development setup is different from most. I don’t use Vagrant, Mamp, or any of that fancy stuff. But what I DO use, as everyone else, is Nginx directives… DUH!

For my private sites I have a wp-common.conf file and for my public sites I have a wp-common-no-auth.conf file. The former having auth_basic directives so my sites aren’t view-able by anyone but me. Ensuring, at all times, my development sites are behind a password.

Both files are a snapshot of the default VVV ( yes I snagged it from vagrant ) config, which you can see on the github repository.

In the config you’ll see something like this:

# nginx-wp-common.conf


location ~ \.php$ {
    # ... other stuff ...


One little known fact, Nginx can be told to pass PHP variables to their location directives! YEP – and I had no idea so I started experimenting and came up with this nifty solution.

# nginx-wp-common.conf


location ~ \.php$ {
    # ... other stuff ...

    # Forward emails to mailcatcher
    fastcgi_param   PHP_VALUE         "sendmail_path=/usr/bin/env /usr/local/bin/catchmail -f";

    # ... more stuff ...

That’s it… literally in one line I’ve enabled mailcatcher for my dedicated server. Since I also have separate config files for my development environments versus my live sites, it’s that much easier.

Now, every email that’s sent out I’m able to catch them… I gotta say, this is way easier than setting up proxies, trying to override other configs… it’s just so much simpler.

If this article has helped you in some way or if you would just like to discuss, let me know in the comments below.

Published inBlog

Be First to Comment

Leave a Reply

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

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

%d bloggers like this: