Why disabling emails is harder in WordPress than you thought?

Aww yeah, my first clickbaity title!

Anyways, let’s get down to business. Here’s the initial setup so you know why the following things will be annoying:

You have an eCommerce website. That eCommerce website sends emails for new orders, renewal orders, etc. As it should. You need to do some work, so you create a staging copy of your site. The staging copy won’t take payments, but it will still generate renewal orders. Obviously you don’t want your customers to receive emails generated by the staging site, so you install helper plugins to do that for you. There are two that are pretty popular: Stop Emails and Disable Emails. Which one to use? They both do the same thing. You install one of them: Stop Emails. Then you wake up to emails from customers asking why they are receiving emails that don’t make sense. Turns out the staging site still sent emails.

Why?!

Here’s how emailing works in WordPress: there’s a function called wp_mail. You pass it email details, and it sends the email. Out of the box it will use your server to do that.

That function uses a class called phpmailer, which is included with WordPress. It’s pretty neat.

The plugin you installed (Stop Emails) will step on WordPress, and replace the class wp_mail is using with its own one. That way when you call wp_mail, you’re still calling WordPress’s version, but the phpmailer class is now different, and it won’t send emails. Works, right?

Well, not quite. Becuase you’re ALSO using Mandrill to send emails, so you have the wpMandrill plugin installed. That particular plugin replaces the wp_mail function, and since that’s pluggable, all is well. Except now when you call wp_mail, it uses wpMandrill’s version, which happily pipes the data to their API, completely bypassing phpmailer and ignoring the fact that Stop Emails replaced that class. The email will still be sent.

Okay, but this is a bug, right?

Not really. Every plugin works as expected, so technically it isn’t. Stop Emails has this bit in their documentation:

In the case where an author does not use the recommended wp_mail() function and instead sends the email using another mechanism, an email will still be sent.

And since wpMandrill replaces wp_mail, it’s actually expected for the emails to be sent.