0

I am sending email with the wp_mail() function but it is not working. It is returning false. Also it is not giving any error in the server log.

This is my code:

         $mailResult = false;
         $to = "[email protected]";
         $subject = "HUDPowerBid Mailgun Test exec on ";
         $message = "Testing Mailgun at: ";
         $mailResult = wp_mail( $to, $subject, $message, $headers);
         $myarr= array('error' => true, 'mail' => $mailResult);

I have also tried with the above code and below code also I have tried with these two codes at once but nothing is working

    $to1 = '[email protected]';
    $subject1 = 'Post Published by Author';
    $headers1 = 'From: admin <[email protected]>' . "\r\n";  
    $message1 = 'your';

        $a=wp_mail($to1, $subject1, $message1, $headers1);
        $my=array('checkstatus'=>$a,'second'=>$mailResult);
        echo json_encode($my);
2
  • Have you tried PHP mail with the same headers? Perhaps your mail host has some limitations on what you can do.
    – Jos
    Commented Mar 5, 2019 at 8:59
  • remove ."\r\n" from $headers1.
    – Yatix
    Commented Dec 17, 2019 at 9:27

1 Answer 1

3

Although wp_mail returns false on failure, it does also contain an action hook to allow you access to error information.

If phpmailer fails to send, these lines run:

 /**
         * Fires after a phpmailerException is caught.
         *
         * @since 4.4.0
         *
         * @param WP_Error $error A WP_Error object with the phpmailerException message, and an array
         *                        containing the mail recipient, subject, message, headers, and attachments.
         */
        do_action( 'wp_mail_failed', new WP_Error( 'wp_mail_failed', $e->getMessage(), $mail_error_data ) );

        return false;

If you attach a function to wp_mail_failed you can look at the WP_Error object to get more info on your problem.

1
  • @butlerblog Thanks for your edit, but my use of the word "does" is deliberate emphasis so I've rolled it back. Commented Apr 12, 2019 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.