Testing the wp_mail function

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 properly.  But how do you know if this is the case?

The use of wp_mail is as follows:

 <?php wp_mail( $to, $subject, $message, $headers, $attachments ); ?>

$to is the email you wish to send to, $subject and $message are the subject line and the message body.  $headers and $attachments are optional parameters.  If you don’t know what they are, then you probably don’t need to be too concerned with them in general.

I’ve put together a simple little script to test wp_mail and see if it is in fact sending messages.  The following script can be saved in a php file and loaded to your WP root directory.

Set your email address in the script, then browse to it directly in your browser to fire the wp_mail function.  You will get a result on screen, and if all is functioning, you will receive a test message in your email inbox.  If you get an error message or you don’t receive the email, then you know there is an issue with wp_mail.

<?php
/**
 * Update variable settings.
 * Load to your WP root folder.
 */

// Set $to as the email you want to send the test to
$to = "youremail@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');

// Call the wp_mail function, display message based on the result.
if( wp_mail( $to, $subject, $message, $headers ) ) {
	// 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!';
};	
?>

Now, this post and this script are focused on determining if wp_mail is working.  Troubleshooting is another matter altogether.

 

About Chad

Chad Butler is both a freelance writer and web developer. He has developed several popular WordPress plugins and his writing has appeared on forbes.com, sfomag.com, and investopedia.com.
This entry was posted in WordPress. Bookmark the permalink.

Related Posts:

7 Responses to Testing the wp_mail function

  1. Vasim says:

    hello,

    this test shows mail was not sent :(

    what to do now?

  2. Irwin Lawson says:

    Hi there,
    I have tested your plugin on one site and it works really well.
    However I tested it on another that is having a problem using wp_mail and it does not work.
    This problem (wp_mail not working) is so hard to solve.
    I have been using a plugin “configure smtp” on this site to get mail to work, and it does.
    Is there a way i can hook into this?

    • Chad says:

      A lot of users have had success using WP Mail SMTP. Depending on your host, it could be that you need validated SMTP to send email from a php script. That’s the plugin most users use in that situation.

  3. Eric says:

    Hi Chad,

    Seasons greetings!

    I’m trying to add the WP-Members plug in to several pages that I’ve individually blocked at my blog. Several visitors have registered at http://reloroundtable.com/blog/wikisresearch/available-tonnage-lists/ but have reported that they never received the email verification containing the password as confirmed in the dialog box they received when they completed the form.

    I tested the native wp_mail funtion by creating a new user via the WP admin panel after disabling another plugin that uses the same native WP mail function. The new user registration worked both times I tested it.

    What could be creating the hang up if wp_mail is working the way it’s supposed to? I couldn’t find any mention of the same problem in the WP support forums

    Thanks in advance for any help you can offer! ~ Eric

    • Chad says:

      The most common issue is that people actually get the email, but they either overlook it, or it ends up in spam, or some other issue that would be entirely out of your control. If you are getting “intermittent” reports on email issues from users, I would say that’s the most likely.

      If you’ve tested things yourself and things are not working, then I would start with trying smtp authenticated mail. The WP Mail SMTP plugin does this well.

      If it’s not one of those things, the last thing would probably be if you are using a cache plugin, that something is not configured to accommodate inline registration. But the previous two items are your more probable issues/resolutions.

  4. Random Individual says:

    Thank you for the test code!

  5. Sana says:

    Hi,

    I have tried your code and I am not able to send email. What could be the problem? SMTP port is set on php.ini and I am checking it on localhost. The comments panel send email but when I try manually with wp_mail, it doesnt work. Please help me out

Leave a Reply

Comments are welcome.
Your email address will not be published.
Required fields are marked *.
Please consider the following before posting your comment:

  • Comments are moderated, so your comment may not show up right away. Submit only once!
  • Keep things on topic. If you comment is not relevant to the specific post, it will be deleted.
  • NO soliciting of services is allowed! These comments will be deleted and the user banned.
  • If you are asking for support, please consider looking for the answer in the Quick Start Guide, the Users Guide, and the FAQs first.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>