Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.