3

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ken J
  • 877
  • 12
  • 21
  • Use Wordpress' built-in [wp_mail()](https://developer.wordpress.org/reference/functions/wp_mail/) function. – APAD1 Sep 14 '18 at 16:44
  • I'm not trying to create custom code here...I'm trying to get the built-in functionality of sending an email from the admin console to a user working. – Ken J Sep 14 '18 at 16:51
  • The code you provided above is custom. I'm simply suggesting to use Wordpress' built-in mail function to handle the sending of e-mails. Why tag with Wordpress if you're just talking about a standard PHP mail function? – APAD1 Sep 14 '18 at 16:55
  • 1
    The PHP script is an example of sending an email from the web server _without_ wordpress. This is what I am asking: why can I send it with PHP's native mail function but Wordpress itself doesn't send an email to the same address? – Ken J Sep 14 '18 at 16:58
  • I ended up having to hack validateAddress. Not sure why it kept on denying the addresses but after locking it to noregex it works fine and emails send. – Ken J Sep 14 '18 at 23:48

1 Answers1

2

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.

Ken J
  • 877
  • 12
  • 21