简体   繁体   English

当我可以通过 PHP 脚本发送电子邮件时,为什么不能通过 Wordpress 发送电子邮件?

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

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.我已将运行 4.9.8 版的 Wordpress 服务器从 Ubuntu 迁移到 CentOS,但来自 Wordpress 的电子邮件似乎无法正常发送。 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.我已经安装了 Postfix 并将其配置为尽可能与原始配置匹配。 I say this because there are some slight differences in how Postfix is implemented.我这样说是因为 Postfix 的实现方式有一些细微的不同。

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.我能够通过命令行和通过与 Wordpress 托管在同一服务器 (php-fpm) 上的 PHP 脚本将电子邮件发送到各种地址。

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.但是,当我尝试将电子邮件(通过在 wp-admin 中创建新用户)发送到 Wordpress 中的相同地址时,它们无法到达收件人。 I've checked the Wordpress configuration files and I'm not seeing anything overriding the default mail operations.我检查了 Wordpress 配置文件,但没有看到任何覆盖默认邮件操作的内容。 There are no new entries in /var/log/maillog when email is triggered via my PHP script:通过我的 PHP 脚本触发电子邮件时, /var/log/maillog 中没有新条目:

<?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.从 Wordpress 触发电子邮件后也没有条目。

EDIT: I turned on debugging to a log file and added a function to catch wp_mail errors.编辑:我打开调试日志文件并添加了一个函数来捕获 wp_mail 错误。 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:编辑2:

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.我在functions.php 中添加了一些自定义代码来设置发件人,并且尝试了几个不同的发件人电子邮件地址——这两个都是有效的电子邮件地址。 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.奇怪的是 PHP 的邮件功能将电子邮件发送为 wordpress@{mydomain} 并且工作正常。

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:我最初的解决方案是将 class-phpmailer.php 直接更改为“noregex”,但多亏了 Wordpress 论坛上的 @diondesigns,我发现我可以在主题的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.我想补充一点,这里的主要问题是 PHP 是用 PCRE2 编译的,而不是 PCRE。 That broke the validateAddress in PHPMailer.这破坏了 PHPMailer 中的 validateAddress。 After moving to a version of PHP with PCRE compiled I am no longer seeing the issue.在迁移到已编译 PCRE 的 PHP 版本后,我不再看到该问题。 I recommend this path over the workaround.我推荐这条路径而不是解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM