Make WordPress Stop Sending E-Mails When Working Locally

Let’s suppose you have a website with an e-commerce plugin.  A user comes in, buys a product and goes fine through the checkout process, but when the site tries to redirect the user to the third-party gateway where he wants to pay, something goes wrong. The redirect goes to a 404 page or never gets done. And that only happens with that single costumer. All the other costumers who bought that same product from your site did it without an itch. Whatever the problem is, you need to debug locally to find the source of it.

So now you need to replicate the process in your local mirror of the production site. You click the “buy now” button in the product page, then go to the checkout page and fill the form with the very same values that the user has previously entered. You click “confirm purchase”, and then notice the error through your favourite Javascript profiler: the email of the user customer has a character that your validator doesn’t like, so you need to improve it, but somehow the form was sent either way instead of prompting the user to re-enter his email, and you need to fix that too.

A couple of minutes later, while you’re fixing your code, you receive a phone call from the customer, who says that he received a second purchase confirmation email for an operation he did not perform. Then you have to explain to him that you were working on the issue that he reported earlier, and he understands but insists on the fact that he was bothered for a second time (the first one being his impossibility to pay) and that you should be a little bit more professional. And he is right. Now let’s suppose that this email thing happens often, and that you even lost sales because of this. The same can be applied to other scenarios regarding emails being sent, such as creation of testing users, or posting test contents to a blog with a lot of suscribers that should be notified of new posts by email. I’m sure you don’t want this. I really don’t for sure.

But there’s a solution. When working with WordPress you’re gonna find something that Codex calls Pluggable Functions. These are some important core functions that you can override to fit your own needs. This is something good for our current problem, since wp_mail(), the function that takes care of sending all the internal emails , is one of these pluggable functions. Now we need to write some code, so let’s work it out.

Use Different Configuration Files for Different Stages

I talked about this earlier, and here we need this practice again, so I totally recommend you to have something like this in your workflow.

Long story short, having a slightly modified wp-config.php file and some additional configuration files you can perform different actions based on the stage you’re in. Take the following code as an example:

Create a MU Plugin

If you’re not using MU plugins, you should do it now. These are plugins that go in a different folder (the default one is called mu-plugins, and you need to create it next to your usual plugins directory) and do not need to be activated. Using this one solves our problem:

So, having that code in a MU plugin will stop WordPress from sending emails when you’re working locally. You just have to make sure that your local-config.php file never gets uploaded to your live site. In fact, you don’t even need to upload this file to production. If it just keeps living in your local environment, then you’re all done. If you prefer this method, you can even get rid of the local configuration file and the WP_STAGE constant, but I don’t really recommend doing that because is easy to forget that you should not upload the file, and if you do it you’ll prevent all emails to be sent from the live website too.

Also, speaking of constants, take a look at WP_MAIL_LOCAL. That’s there in case you do have to send an email locally, for example when you need to send something to yourself. All you have to do is set WP_MAIL_LOCAL to true every time you need it, so you can have your sending processes being performed in a more controlled environment.

Have Any Question or Suggestion?

Please use the comments section. It would be awesome to have some feedback from you and know what other methods you use to work around this issue.

2 Comments

Leave a Reply

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