Why won't I send an email thru Wordpress when I can send it thru a PHP script?

Viewed 887

I've migrated a Wordpress server running version 4.9.8 from Ubuntu to CentOS and emails from Wordpress don't seem to be sending properly. I'm testing sending emails by creating new users associated with various email addresses.

I've installed Postfix and configured it to match as closely as possible with the original configuration. I say this because there are some slight differences in how Postfix is implemented.

I'm able to send emails to various addresses through the command-line and thru a PHP script hosted on the same server (php-fpm) as Wordpress.

However, when I try to send emails (by creating a new user in wp-admin) to the same addresses in Wordpress they don't reach the recipient. I've checked the Wordpress configuration files and I'm not seeing anything overriding the default mail operations. There are no new entries in /var/log/maillog when email is triggered via my PHP script:

<?php
// the message
$msg = "First line of text\nSecond line of text";

// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);

// send email
mail("me@{mydomain}","My subject",$msg);
?>

There are also no entries after triggering an email from Wordpress.

EDIT: I turned on debugging to a log file and added a function to catch wp_mail errors. This is the error that I'm seeing when an attempt is made:

(
            [wp_mail_failed] => Array
                (
                    [0] => Invalid address:  (setFrom) wordpress@{mydomain}
                )

        )

The From email address is the same as it was in original server.

EDIT2:

I've added some more custom code to functions.php to set the sender and I've tried a couple of different sender email addresses - both of which are valid email addresses. All of them are failing with this "invalid address" error. What's weird is PHP's mail function sends the email as wordpress@{mydomain} and that works fine.

1 Answers

My initial solution was to change class-phpmaiiler.php directly to 'noregex' but thanks to @diondesigns on the Wordpress forums I found out that I could add a line to my theme's functions.php:

add_filter( 'wp_mail_from', function($from){PHPMailer::$validator = 'noregex'; return $from;} );

Now emails send fine.

UPDATE:

I wanted to add that the main issue here was that PHP was compiled with PCRE2 - not PCRE. That broke the validateAddress in PHPMailer. After moving to a version of PHP with PCRE compiled I am no longer seeing the issue. I recommend this path over the workaround.

Related