admin管理员组

文章数量:1124541

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.

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.

Share Improve this question edited Feb 27, 2024 at 7:06 uncovery asked Feb 24, 2024 at 6:05 uncoveryuncovery 19910 bronze badges 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 Commented Feb 24, 2024 at 9:17
  • Thanks for the suggestion, added results above. – uncovery Commented Feb 27, 2024 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 Commented Feb 27, 2024 at 11:51
Add a comment  | 

1 Answer 1

Reset to default 2

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.

本文标签: wpmail fails with quotCould not instantiate mail functionquot but quotmail()quot works fine