说明
- 环境:PHP5.6
配置PHPmailer发送邮件,所有流程都按照官网配置完成,当发送邮件的服务器设置为163时,邮件发送成功,换为自己的企业邮箱,邮件发送失败,提示错误:SMTP服务器连接失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
问题描述
配置PHPmailer发送邮件,所有流程都按照官网配置完成,当发送邮件的服务器设置为163时,邮件发送成功,换为自己的企业邮箱,邮件发送失败,
提示错误:SMTP服务器连接失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
解决过程
打开错误提供的地址:https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting,翻阅常见错误,其中有一段描述是这样的:
PHP 5.6 certificate verification failure
In a change from earlier versions, PHP 5.6 verifies certificates on SSL connections. If the SSL config of the server you are connecting to is not correct, you will get an error like this:
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
The correct fix for this is to replace the invalid, misconfigured or self-signed certificate with a good one. Failing that, you can allow insecure connections via the SMTPOptions property introduced in PHPMailer 5.2.10 (it's possible to do this by subclassing the SMTP class in earlier versions), though this is not recommended:
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
You can also change these settings globally in your php.ini, but that's a really bad idea; PHP 5.6 made this change for very good reasons.
Sometimes this behaviour is not quite so apparent; sometimes encryption failures may appear as the client issuing a QUIT immediately after trying to do a STARTTLS. If you see that happen, you should check the state of your certificates or verification settings.
所以,按照文档中描述,应该是SSL验证证书的问题导致的邮件发送失败,需要将PHPmailer配置加入到PHPmailer实例化中:
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
最终问题得到解决。