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 = "tayyabvohra@gmail.com";
         $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 = 'abc@gmail.com';
    $subject1 = 'Post Published by Author';
    $headers1 = 'From: admin <admin@urdutahzeeb.com>' . "\r\n";  
    $message1 = 'your';

        $a=wp_mail($to1, $subject1, $message1, $headers1);
        $my=array('checkstatus'=>$a,'second'=>$mailResult);
        echo json_encode($my);
  • Have you tried PHP mail with the same headers? Perhaps your mail host has some limitations on what you can do. – Jos Mar 5 at 8:59
1

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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