Check the SMTP Connection Without Sending
Adds Tools > SMTP Check, which connects and logs in to your configured SMTP server and reports success or the exact error, without sending an email to anyone.
Opens WPIDE on
Code
add_action( 'admin_menu', function () {
add_management_page( 'SMTP Check', 'SMTP Check', 'manage_options', 'wpide-smtp-check', function () {
echo '<div class="wrap"><h1>SMTP Check</h1>';
if ( isset( $_POST['wpide_smtp_check'] ) && check_admin_referer( 'wpide_smtp_check' ) && current_user_can( 'manage_options' ) ) {
require_once ABSPATH . WPINC . '/PHPMailer/PHPMailer.php';
require_once ABSPATH . WPINC . '/PHPMailer/SMTP.php';
require_once ABSPATH . WPINC . '/PHPMailer/Exception.php';
$mailer = new PHPMailer\PHPMailer\PHPMailer( true );
$mailer->Timeout = 10;
do_action_ref_array( 'phpmailer_init', array( &$mailer ) );
if ( 'smtp' !== $mailer->Mailer ) {
echo '<div class="notice notice-warning"><p>WordPress is not configured to use SMTP. Emails are sent with PHP mail().</p></div>';
} else {
try {
$ok = $mailer->smtpConnect();
$mailer->smtpClose();
printf( '<div class="notice notice-%1$s"><p>%2$s</p></div>', $ok ? 'success' : 'error', esc_html( $ok ? sprintf( 'Connected and authenticated with %s:%d.', $mailer->Host, $mailer->Port ) : 'Could not connect: ' . $mailer->ErrorInfo ) );
} catch ( Exception $e ) {
printf( '<div class="notice notice-error"><p>%s</p></div>', esc_html( 'Could not connect: ' . $e->getMessage() ) );
}
}
}
echo '<p>Connects to the SMTP server with your current settings and logs in, without sending anything.</p><form method="post">';
wp_nonce_field( 'wpide_smtp_check' );
echo '<button class="button button-primary" name="wpide_smtp_check" value="1">Run the check</button></form></div>';
} );
} );
Notes
Uses whatever SMTP settings your other snippets or mail plugin apply through phpmailer_init; if nothing switches WordPress to SMTP, it tells you so.
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.