Disable Emails Sending from WordPress in Development

May 3, 2018

Inevitably, there will be a time that you need to copy a live database to a local installation of WordPress to work on a new feature or fix a couple of bugs. Occasionally, these sites have user activity and require sending emails – like e-commerce transactional emails – to site users.

This is where copying a database to your computer gets tricky. You don’t want to send emails from your site to users twice – once from the live version and once from the development version. What you need to do is stop your local site from sending emails!

There are a couple of plugins that do this well (Stop Emails and Disable Emails), but you would have to then remember to enable these plugins whenever you copy your database locally. Not ideal.

The Code

Though this is a drastic approach, you can add the following snippet to your wp-config.php file to disable all wp_mail() based emails:

// Disable Outgoing WordPress Emails
function wp_mail() {
	//
}

Using a Plugin Still…

If you still really want to use a plugin for some reason, you can also add the following line to your wp-config.php to do the same by automatically activating the Stop Emails plugin (or your plugin of choice):

activate_plugin( 'stop-emails/stop-emails.php' );

Both methods guarantee that wp_email() triggered emails never leave your development site, though one doesn’t require an additional plugin to be installed.

NOTE: Make sure you don’t allow this to be enabled on your live site! This will keep people from even being able to request a password reset!

BONUS

Want to make sure that you know that wp_mail() is being disabled locally? Add this snippet to your wp-config.php as well to give yourself a notice in the backend that it’s enabled.

add_action( 'admin_notices', 'nomail_notice' );
function nomail_notice() {
    ?>
    <div class="error notice">
        <p><?php _e( '<strong>NO MAIL</strong> Mail from wp_mail() has been disabled for this site.', '' ); ?></p>
    </div>
    <?php
}

Wanna chat about this article or any others? Feel free to DM me or mention me on Twitter @marcusdburnette to start a conversation!

Leave a Reply

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