WordPress has its own email function, wp_mail, that it uses for sending email. There are many plugins, my WP-Members plugin included, that rely on this function to be working. WordPress does not provide email settings, so how do you know if email is working?
Suppose you have some plugins or something in your site that should be sending emails, yet you are not receiving any messages. Is something broken in the plugin? Or is it a case of wp_mail not functioning properly? You can’t begin to track your issue until you know where to be looking.
This article will explain how you can run a test script on your site to test your WordPress email settings. This will tell you if the wp_mail function is both working and sending email.
The wp_mail Function Explained
The function accepts the following arguments:
- $to (required) – This is the email address you wish to send to
- $subject (required) – The email subject line
- $message (required) – The email message body content.
- $headers (optional) – This is an optional argument. It could contain things like defining plain text or html email, or the “from” name of an email
- $attachments (optional) – Another optional argument. This one allows for an email attachment.
Focus on the first three required arguments. If email sends with those then wp_mail is functioning. If not, further testing may require looking into the $headers. Some hosts require that valid “from” headers be used, so that is something that should be checked.
Simple wp_mail Testing Snippet
The following script that allows you to test WordPress email by attempting to send an email through your site. To use this testing script, save it as a php file and load it to your WP root directory via FTP.
Set your email address as indicated in the example script. Save the file as mailtest.php, then browse to it directly in your browser to fire the wp_mail function (http://yourdomain.com/mailtest.php). You will get a result on screen, and if all is functioning, you will receive a test message in your inbox. If you get an error message or you don’t receive the email, there is an issue with wp_mail.
Here is the script. I plan to put this together in a utility plugin at some point in the future, but for now, you can save it as a php file and run it from your WP root folder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This file can be used to validate that the WordPress wp_mail() function is working. | |
* To use, change the email address in $to below, save, and upload to your WP root. | |
* Then browse to the file in your browser. | |
* | |
* For full discussion and instructions, see the associated post here: | |
* http://b.utler.co/9L | |
* | |
* Author: Chad Butler | |
* Author URI: http://butlerblog.com/ | |
*/ | |
/* | |
Copyright (c) 2012-2015 Chad Butler | |
This program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License, version 2, as | |
published by the Free Software Foundation. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
You may also view the license here: | |
http://www.gnu.org/licenses/gpl.html | |
*/ | |
// Set $to as the email you want to send the test to. | |
$to = "you@yourdomain.com"; | |
// No need to make changes below this line. | |
// Email subject and body text. | |
$subject = 'wp_mail function test'; | |
$message = 'This is a test of the wp_mail function: wp_mail is working'; | |
$headers = ''; | |
// Load WP components, no themes. | |
define('WP_USE_THEMES', false); | |
require('wp-load.php'); | |
// send test message using wp_mail function. | |
$sent_message = wp_mail( $to, $subject, $message, $headers ); | |
//display message based on the result. | |
if ( $sent_message ) { | |
// The message was sent. | |
echo 'The test message was sent. Check your email inbox.'; | |
} else { | |
// The message was not sent. | |
echo 'The message was not sent!'; | |
} |
Testing vs Troubleshooting
Now, this post and this script are focused on determining if wp_mail is working. Troubleshooting is another matter altogether. For some next steps, this post has some things to know when troubleshooting wp_mail, and it includes some possible solutions.
Incidentally, since this post was originally written, I have become one of the compontent maintainers for WP’s mail component (which includes wp_mail()). Also, I have identified and documented a number of filters in wp_mail for the wordpress.org codex.
For more information:
- Troubleshooting wp_mail WordPress Email Configuration – not everything that can go wrong is directly a problem with WP. This post has information on host restrictions and other outside problems that should be checked.
- WordPress Email Settings: Changing the wp_mail address with a simple plugin – here is a very simple and lightweight script you can load as a plugin to change the email address that WordPress sends email from.
- Changing the wp_mail from address in WordPress without a plugin – provides a simple code snippet you can use to change the email address that WordPress sends from, no plugin required.
- Easy SMTP email settings for WordPress – how to change your WordPress email configuration to send email from a valid SMTP server with a simple script, no plugin required.
Enjoyed this article?
Don't miss a single post. Subscribe to our RSS feed!