排查 WordPress SMTP 功能失效

最近一次登录 WordPress 后台时,看到插件 Easy WP SMTP 有一个报错,发送测试邮件也失败了。报错信息如下:

Versions:
WordPress: 7.1.5
WordPress MS: No
PHP: 8.3.6
Easy WP SMTP: 2.15.1

Params:
Mailer: smtp
Constants: No
Host: smtp.qq.com
Port: 587
SMTPSecure: tls
SMTPAutoTLS: bool(true)
SMTPAuth: bool(true)

Server:
OpenSSL: OpenSSL 3.0.13 30 Jan 2024

Error:
SMTP Error: Could not connect to SMTP host. Failed to connect to server SMTP server error: Failed to connect to server SMTP code: 110 Additional SMTP info: Connection timed out

SMTP Debug:
Connection: opening to smtp.qq.com:587, timeout=30, options=array()
Connection failed. Error #2: stream_socket_client(): Unable to connect to smtp.qq.com:587 (Connection timed out) [/var/www/www.shelwee.com/wp-includes/PHPMailer/SMTP.php line 423]
SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP Error: Could not connect to SMTP host. Failed to connect to server

一开始还以为是服务器的安全组、防火墙策略之类的问题,但另一个站点配置的 smtp.sina.cn:465 确实通的,也能正常发送邮件。

所以安全组、防火墙等因素就可以排除了。于是 SSH 到服务器分别执行如下命令查看连通情况:

telnet smtp.sina.cn 465
Trying 116.133.8.222...
Connected to smtp.sina.cn.
Escape character is '^]'.
telnet smtp.qq.com 465
Trying 2402:4e00:1020:24d3:6:5bfc:f92d:eb90...
Connection failed: Connection timed out

这下问题就很明显了,大概率是之前腾讯邮箱服务器的默认 IP 地址从 IPv4 转换到了 IPv6,而云服务器默认是不支持 IPv6 的。因此对于配置新浪邮箱的站点不影响,能正常使用,而配置了腾讯邮箱的站点就出现异常了。

定位到问题,那么解决起来就简单了。

nano /etc/gai.conf

找到 #precedence ::ffff:0:0/96 100 这一行,将前面的 # 去掉,这样配置会让系统在双栈环境下优先选择 IPv4。

precedence ::ffff:0:0/96 100

改完保存后,再来试一下连通性:

telnet smtp.qq.com 465
Trying 121.14.22.61...
Connected to smtp.qq.com.
Escape character is '^]'.

可以看到,服务器到腾讯邮箱的服务器已经可以连通了。但此时 WordPress 后台大概率发送邮件还是会失败,还要重启一下对应的 PHP-FPM 服务, 让 PHP-FPM 进程重新读取最新的配置:

systemctl restart php8.3-fpm.service

php8.3-fpm.service 需修改为你的服务器对应版本的 PHP-FPM 。

至此,SMTP 功能就恢复正常了。

THE END

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注