How to error message display in wp_mail in wp

When using wp_mail() in WordPress, you can display error messages in a few ways:

  1. Using the wp_mail filter: You can use the wp_mail filter to catch and display any errors that occur during the mail sending process. Here's an example:

    function display_wp_mail_errors($wp_mail) {
     if (is_wp_error($wp_mail)) {
         $error = $wp_mail->get_error_message();
         echo '<div class="error">'. $error. '</div>';
     }
    }
    add_action('wp_mail', 'display_wp_mail_errors');

    In this example, we're using the wp_mail filter to catch any WP_Error objects that are returned by wp_mail(). We then extract the error message using the get_error_message() method and display it as a <div> element with a class of "error".

  2. Using the wp_mail_failed action: Another way to display error messages is to use the wp_mail_failed action, which is triggered when wp_mail() fails to send an email. Here's an example:

    function display_wp_mail_failed($error) {
     echo '<div class="error">'. $error->get_error_message(). '</div>';
    }
    add_action('wp_mail_failed', 'display_wp_mail_failed');

    In this example, we're using the wp_mail_failed action to catch any WP_Error objects that are returned by wp_mail(). We then extract the error message using the get_error_message() method and display it as a <div> element with a class of "error".

  3. Using a custom error handler: You can also use a custom error handler to catch and display error messages. Here's an example:

    function custom_error_handler($errno, $errstr, $errfile, $errline) {
     if ($errno == E_ERROR || $errno == E_WARNING || $errno == E_PARSE) {
         echo '<div class="error">'. $errstr. '</div>';
     }
    }
    set_error_handler('custom_error_handler');

    In this example, we're using the set_error_handler() function to set a custom error handler. This handler will catch any errors that occur during the execution of wp_mail(), and display them as a <div> element with a class of "error".

Remember to adjust the error handling code to fit your specific needs and requirements.