1

I have issues with wordpress emails not being sent. So for testing, I created this code:

add_action( 'wp_mail_failed', 'onMailError', 10, 1 );
    function onMailError( $wp_error ) {
        echo "<pre>";
        print_r($wp_error);
        echo "</pre>";
    }  

echo "Testing normal PHP Email<br>";
$to = "[email protected]";
$msg = "test email via PHP";
$check = mail($to, $msg, $msg);

echo "Testing Wordpress Email<rb>";
$msg = "test email via wordpress";
$headers = array('From: My Website<[email protected]>');
$headers = implode( PHP_EOL, $headers );
$check = wp_mail($to, $msg, $msg, $headers);

The result is that the first email sent with mail() works fine, but the email sent with wp_mail returns the following:

WP_Error Object
(
[errors] => Array
    (
        [wp_mail_failed] => Array
            (
                [0] => Could not instantiate mail function.
            )
    )

[error_data] => Array
    (
        [wp_mail_failed] => Array
            (
                [to] => Array
                    (
                        [0] => [email protected]
                    )

                [subject] => test email via wordpress
                [message] => test email via wordpress
                [headers] => Array
                    (
                    )

                [attachments] => Array
                    (
                    )
                [phpmailer_exception_code] => 2
            )
    )
[additional_data:protected] => Array
    (
    )
)

I tried to set the debug in PHPMailer to 4 but only got the following in addition:

Sending with mail() 
Sendmail path: /usr/sbin/sendmail -t -i  // this is correct by the way and the console mail command works fine
Envelope sender: // the email details suach as recipient, subject, header
Result: false

So the debug was not really helpful.

Any idea what could be wrong here? When I looked online for solutions, I read that wp_mail IS using mail() as a function. So why would it not work?

If I define an external SMTP host with my_phpmailer_smtp( $phpmailer ) {...}, it works fine, but the internal mail() seems to fail.

3
  • 1
    Can you turn on PHPMailer debugging? Set $SMTPDebug in wp-includes/PHPMailer/PHPMailer.php. You can also do this by hooking phpmailer_init, but for a quick test I'd just edit the class definition. It looks like this error just means the mail() called failed for some reason: I'd guess one of the other settings it's giving you is causing this, but it would be useful to see if this is getting as far as the mail server and if so what error message it's returning.
    – Rup
    Feb 24 at 9:17
  • Thanks for the suggestion, added results above.
    – uncovery
    Feb 27 at 7:07
  • 1
    Ah, sorry, I forgot it called out to sendmail and assumed you'd get the actual SMTP traffic. Sorry! I'm not sure. If configuring a server works fine then there are also plugins that'll do that for you too - just add an admin page to enter settings onto, and/or pick them up from wp-config - but I haven't used one to recomment it.
    – Rup
    Feb 27 at 11:51

1 Answer 1

1

So I managed to trace down the issue with the following method:

I created a php file with the following:

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// include wordpress
require_once('wp-load.php');
$to = "[email protected]";
$msg = "test email via wordpress";

$headers = array(
  'From: The Domain<[email protected]>'
);
$headers = implode( PHP_EOL, $headers );
// use wordpress email function
$check = wp_mail($to, $msg, $msg, $headers);

var_dump($check);

and ran it on the console with php8.2 ./email_test.php This then returned the following:

Testing Wordpress Email
sendmail: the server did not accept the mail
sendmail: server message: 554-Transaction failed
sendmail: server message: 554 Unauthorized sender address.
sendmail: could not send mail (account default from /homepages/config/smtp/msmtprc.u93774155)
bool(false)

Which allowed me to go to the hoster and they fixed it by adding the domain.com as a whitelisted sender to their setup and then it worked. It seems that phpMailer is supressing those errors that you get when you run it from the console. So just run wp_mail from the console, you might get error messages that you miss otherwise.

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.