Testing your WordPress email settings for 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.  WordPress does not provide email settings, so how do you know if this function is working?

Suppose you have some plugins or something in your site that should be sending you emails and you are not receiving any emails. 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 in order to test  your WordPress email settings to see if the wp_mail function is working and sending email.

The wp_mail function is used as follows:

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

The arguments of the function are:

  • $to – This is the email address you wish to send to
  • $subject – The email subject line
  • $message – The email message body content.
  • $headers – This is an optional argument. It could contain things like defining plain text or html email, or the “from” name of an email
  • $attachments – Another optional argument.  This one allows for an email attachment.

In general, you need to focus on the first three required arguments.  If things work OK with those, wp_mail is functioning.  If not, further testing may require looking into the $headers a little more.  Some hosts require that valid “from” headers be used, so that is something that should be checked.

I’ve put together a simple little script that allows you to test your WordPress email settings by attempting to send an email through the wp_mail function.  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 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 = "your email address in quotes";

// 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.  For some next steps, this post has some things to know when troubleshooting wp_mail, and it includes some possible solutions.

For more information on testing, troubleshooting, and changing your WordPress email configuration for wp_mail, here are some additional posts:

Comments

  1. 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.

  2. 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.

  3. 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

Trackbacks

Leave a Reply

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

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>