错误提示:
SMTP ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution (0)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
邮件发送失败: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting{“result”:null,“code”:0,“timestamp”:0,“message”:""}
解决方法:
首先,检查是否能ping通邮件服务器;
在linux上
ping smtp.163.com
或者
telnet smtp.163.com
其次,在确认了自己域名解析已经正常且代码配置无误的情况下,还有上面的问题,那么接下来就是重启自己的web服务器。比如,apache,nginx等。
由于我是在Docker容器运行,所以先stop,再up即可
docker-compose stop
docker-compose up -d
经过上面两步,问题就已经解决了。
最后附上我的发送邮件代码块,仅供参考
由于PHPMailer包无法自动下载,所以手动下载GitHub版本,手动引入
define("VENDOR_PATH", dirname(__DIR__) . '/../../vendor');
require VENDOR_PATH . '/PHPMailer/src/Exception.php';
require VENDOR_PATH . '/PHPMailer/src/PHPMailer.php';