macOS10.12下不能用PHPMailer发邮件

今天手贱将Jenkins服务器从10.11升级为10.12,马上就踩坑了。PHPMail报错

Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting+

意思很明显,连不上SMTP服务器啦,而且还指引你上github看解决方案,太贴心了。上去github上面,尝试将ssl改为tls,也不行,这次的报错很神奇

Warning: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in /Users/tanzhiyuan/Documents/Jenkins_tools/PHPMailer/class.smtp.php on line 344
Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Warning看上去是个线索,再次google一番,找到了一篇很好用的文章
https://github.com/composer/composer/issues/3346
里面提到,首先看看你的php版本

php -v
PHP 5.6.24 (cli) (built: Aug  8 2016 16:58:37) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
tanzhiyuandeMac-mini:PPStocks tanzhiyuan$ openssl version
OpenSSL 0.9.8zh 14 Jan 2016

可以看到mac10.12比mac10.11是升级了PHP的版本,在mac10.11下是这样的

php -v
PHP 5.5.36 (cli) (built: May 29 2016 01:07:06) 
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies

应该问题就出在了php版本升级上面了。进一步会发现

php -r "openssl_get_cert_locations();"

这句命令,在mac10.12和mac10.11下是不同的输出,mac10.11下直接出错,表示不支持这个函数,应该就是说mac10.11下的php用的SSL证书是跟随系统的,在mac10.12下是如下输出

Array
(
    [default_cert_file] => /usr/local/libressl/etc/ssl/cert.pem
    [default_cert_file_env] => SSL_CERT_FILE
    [default_cert_dir] => /usr/local/libressl/etc/ssl/certs
    [default_cert_dir_env] => SSL_CERT_DIR
    [default_private_dir] => /usr/local/libressl/etc/ssl/private
    [default_default_cert_area] => /usr/local/libressl/etc/ssl
    [ini_cafile] => 
    [ini_capath] => 
)

好,结果很明显,替换掉/usr/local/libressl/etc/ssl/cert.pem即可。

wget http://curl.haxx.se/ca/cacert.pem
sudo mv cacert.pem /usr/local/libressl/etc/ssl/cert.pem

会说mv失败,因为木有该目录,这个时候手动创建即可。PHPMailer又能欢快地跑起来了,不过实测发现使用smtp.qq.com是能正常发信的,但是用smtp.163.com就不行了,估计还是哪里有坑,不过并不重要了。

你可能感兴趣的:(macOS10.12下不能用PHPMailer发邮件)