How to check mail function is working worddpress h0ook

To check if the mail function is working in WordPress using a hook, you can use the wp_mail hook, which is triggered after the mail function is called. Here's an example of how you can use this hook to test the mail function:

  1. Add the following code to your theme's functions.php file or a custom plugin:

    function test_wp_mail() {
     // Set the recipient's email address
     $to = '[email protected]';
    
     // Set the subject and message
     $subject = 'Test email from WordPress';
     $message = 'This is a test email sent from WordPress using the wp_mail hook.';
    
     // Send the email
     wp_mail($to, $subject, $message);
    
     // Check if the email was sent successfully
     if (wp_mail($to, $subject, $message)) {
         echo 'Email sent successfully!';
     } else {
         echo 'Error sending email!';
     }
    }
    add_action('wp_mail', 'test_wp_mail');
  2. Save the changes to your functions.php file or plugin.

  3. Go to your WordPress dashboard and navigate to the "Tools" > "Mail" page.

  4. Click the "Send Test Email" button.

  5. If the email is sent successfully, you should see the message "Email sent successfully!" in your browser.

Alternatively, you can use the wp_mail_debug filter to debug the mail function. This filter allows you to modify the mail function's output and check if the email is being sent correctly.

Here's an example of how you can use the wp_mail_debug filter:

function debug_wp_mail($args) {
    // Print the email headers and body
    print_r($args);

    // Return the modified $args array
    return $args;
}
add_filter('wp_mail_debug', 'debug_wp_mail');

This code will print the email headers and body to the browser, allowing you to inspect the email being sent.

Note that the wp_mail hook and wp_mail_debug filter are only available in WordPress 3.9 and later versions. In earlier versions of WordPress, you can use the wp_mail function directly to send an email and check if it was sent successfully.