深夜揪虫

站子昨晚发现QA发表提问前端提示“提交失败,请稍后重试”,提了工单提示可能是请求出现了问题,比如请求超时或者报错等原因,建议先排查下邮件功能。进后台果然发现发信失败,提示

SMTP Error: Could not connect to SMTP host. Failed to connect to server

好吧,先进了企业邮箱看了下设置没问题,然后进域名解析做了修复。再测还是问题存在。实际上问题可能从去年就已经有了,始终无法发信,但没有留意到。那行,开始折腾吧。

提单折腾

从阿里云到服务器挨个提工单,都没给解决方案,后来搜到一个详细的排查文档,推荐出现同类问题的可以参照这个文档做下顺序排查SMTP 发信报错:Could not connect to SMTP host_阿里邮箱 (Alibaba Mail)- 阿里云帮助中心 (aliyun.com)

顺着这个排查发现可以 ping 通 ping smtp.qiye.aliyun.com,但测不通 telnet smtp.qiye.aliyun.com 465 多半就是465端口被服务器防火墙拦了。

1724121892370.jpg

进防火墙开了端口测试,却发现依旧无法发信。最后折腾了半天,索性安了WP mail SMTP 插件打算曲线救国了。安装好后,依旧没办法解决问题,第三方插件还是无法实现正常发信。彻底抓狂了。

日志端倪

因为465端口的怀疑最大,所以一直在各种折腾端口问题。在安装三方插件后,虽然还是没能解决问题,但仔细研读了返回错误的日志发现了

failed loading cafile stream: `cacert.pem'

这一错误描述,就像打开了一个新的大门,觉得也有可能是PHP的SSL CA证书出现了问题。根据一个博主提出过的解决思路:

在 PHP 安装目录下新建一个文件夹 cacert 来存放 CA 证书

修改 php.ini 配,启用 opensscurl 扩展,搜索 extension ,去掉需要开启扩展前的分号注释

extension=curl

extension=openssl

再进到服务器如上修改配置文件,启用好了相应的扩展。

彻底解决

只启用了当然还是没法正常通信,在三方插件的作者交流页中搜索类似问题的关键词,在一个答复下不经意看到一个解决分享的回复,最终彻底解除了我的问题。

Problem solved.

The source of the problem was the php.ini for PHP 8.1, and a small difference compared to the php.ini for PHP7.4.

Somehow the last row of the following content was missing in the new PHP 8.1 php.ini file causing a certificate error, because OpenSSL didn’t find the cacert.pem.

[openssl]

; The location of a Certificate Authority (CA) file on the local filesystem

; to use when verifying the identity of SSL/TLS peers. Most users should

; not specify a value for this directive as PHP will attempt to use the

; OS-managed cert stores in its absence. If specified, this value may still

; be overridden on a per-stream basis via the "cafile" SSL stream context

; option.

openssl.cafile=/usr/local/etc/cacert.pemThe Plugin is now functioning normally.

当然,这个人的回答并非是我的问题最完整的解题答案。但最后一句十分关键,马上下好.pem的证书(cacert.zip),按照路径放置在服务器对应目录里,然后把最后一句拷到配置文件中。再测通过,完美解决。

当然,测了一下465还是没办法通,不过无所谓了,问题解决能正常发信就是(阶段性)小胜。

暂且存一下小经验放到这里,也算是解决WordPress SMTP Error: Could not connect to SMTP host. Failed to connect to server问题的另一个思路