Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
File for testing the wp_mail function
* 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:
* Author: Chad Butler
* Author URI:
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
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:
// Set $to as the email you want to send the test to.
$to = "";
// 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);
// 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!';

This comment has been minimized.

Copy link

@keithpickett keithpickett commented Mar 10, 2017

Thanks for this. I just ran it and got this error:

Fatal error: Uncaught exception 'phpmailerException' with message 'Invalid address: (setFrom) wordpress@' in /home/kuen321/public_html/jest/wp-includes/class-phpmailer.php:1023
Stack trace:
#0 /home/kuen321/public_html/jest/wp-includes/pluggable.php(352): PHPMailer->setFrom('wordpress@', 'WordPress', false)
#1 /home/kuen321/public_html/jest/wp_mail_test.php(49): wp_mail('keithp@r...', 'wp_mail functio...', 'This is a test ...', '')
#2 {main}
thrown in /home/kuen321/public_html/jest/wp-includes/class-phpmailer.php on line 1023

I stumbled across this Link where someone was reporting a similar error. The fix was to add this to functions.php:
add_filter( 'wp_mail_from', function() { return 'wordpress@[MyDomain]'; } );

I just put it in above the wp_mail() call and it worked.


This comment has been minimized.

Copy link

@ownboss2015 ownboss2015 commented Mar 24, 2017

Received this: The test message was sent. Check your email inbox.

No email though...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment