Send a Test Email From the Admin
Adds a Tools > Send Test Email screen where administrators can send a test message to any address and see right away whether WordPress could hand it off.
Opens WPIDE on
Code
add_action( 'admin_menu', function () {
add_management_page( 'Send Test Email', 'Send Test Email', 'manage_options', 'wpide-test-email', function () {
$result = null;
$error = '';
if ( isset( $_POST['wpide_test_email'] ) && check_admin_referer( 'wpide_test_email' ) && current_user_can( 'manage_options' ) ) {
$to = sanitize_email( wp_unslash( $_POST['wpide_test_email'] ) );
if ( is_email( $to ) ) {
$capture = function ( $wp_error ) use ( &$error ) {
$error = $wp_error->get_error_message();
};
add_action( 'wp_mail_failed', $capture );
$result = wp_mail( $to, 'Test email from ' . wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), "This is a test email sent from " . home_url() . " at " . wp_date( 'Y-m-d H:i:s' ) . '.' );
remove_action( 'wp_mail_failed', $capture );
} else {
$error = 'Please enter a valid email address.';
}
}
echo '<div class="wrap"><h1>Send Test Email</h1>';
if ( true === $result ) {
echo '<div class="notice notice-success"><p>The email was handed to the mail server.</p></div>';
} elseif ( false === $result || $error ) {
echo '<div class="notice notice-error"><p>' . esc_html( $error ? $error : 'The email could not be sent.' ) . '</p></div>';
}
echo '<form method="post">';
wp_nonce_field( 'wpide_test_email' );
printf( '<p><label for="wpide_test_email">Send to</label><br><input type="email" class="regular-text" id="wpide_test_email" name="wpide_test_email" value="%s" required></p>', esc_attr( wp_get_current_user()->user_email ) );
submit_button( 'Send test email' );
echo '</form></div>';
} );
} );
Notes
A "sent" result means the mail server accepted it; check spam folders if it does not arrive.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.