At times you may want to stop your WordPress installation from sending emails.
Maybe you’re working on a local copy of a website with a database pulled from production and want to keep your local copy of your website from sending emails to your users. The same thing could be true on a staging site as well.
Or perhaps you’re working on building out your WooCommerce store and have some test products on your website and are still getting things ironed out, but you don’t want customers to actually be able to place orders or receive emails from your website. (Note: stopping orders from being placed is out of the scope of this post.)
Whatever your reason for not wanting to send emails from your WordPress site, there are a couple of ways to go about disabling all email sending from WordPress.
Override the wp_mail() Function
The simplest and most direct way to keep your site from sending emails is to add a function to your wp-config.php file. The PHP function that WordPress uses to send emails is written in a way that can be overwritten with our own wp_mail() function.
Allowing the wp_mail function to be overwritten allows different plugins to use different methods for sending emails (Mailgun, SendGrid, Amazon SES, etc) instead of using your server’s built-in email functionality. Using one of these other services to send your emails requires some setup, but will ultimately allow emails sent from your WordPress site to be much more likely to show up in your users/customers’ email inbox instead of going to the spam folder.
For our use case, there is the added benefit of being able to overwrite the wp_mail function ourselves and use it to stop all emails from being sent from our site.
So, if you edit your wp-config.php file you can add the following function as a short circuit for WordPress attempting to send emails. This should be added just above the /* That’s all, stop editing! Happy publishing. */ line.
<?php // Do NOT include the opening php tag. // Place in your site's wp-config.php file function wp_mail() { // Stops all emails from being sent by WordPress } /* That's all, stop editing! Happy publishing. */
Using WP Sent Mail’s Airplane Mode
If you’re not comfortable editing your site’s wp-config.php file, or if you would like your emails to be processed normally and logged but not actually sent, WP Sent Mail gives you a couple of ways to turn “Airplane Mode” on for your WordPress website.
Once WPSentMail is installed and activated you’ve got two ways to enable “Airplane Mode.” The first is via the plugin settings if you visit Your Site > WP Admin > WP Sent Mail > Settings you’ll find a setting to “Log and silently block outbound emails for testing.” Checking this box and saving your settings turns on Airplane Mode.
If you’ve got your wp-config.php file in source control, are frequently pulling a production database to your local site and don’t want to override the setting, or would just rather have this set within your config file… you can add the WPSM_AIRPLANE_MODE constant in your config file and set it to true.
<?php // Do NOT include the opening php tag. // Place in your site's wp-config.php file define( 'WPSM_AIRPLANE_MODE', true ); /* That's all, stop editing! Happy publishing. */
Using WP Sent Mail to do this has the added benefit of allowing you to log the email and review the content and design of the emails being sent. This can be extremely helpful if you’re building custom WooCommerce email templates for example.
Hopefully, that helps you when you need to stop WordPress from sending emails!