使用PHPMailer 中的報錯解決 "Connection failed. Error #2: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:"


PHPMailer項目地址:https://github.com/PHPMailer/PHPMailer

項目中用到PHPMailer,使用過程中報錯:"Connection failed. Error #2: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:"

由於我用的第三方smtp是ssl鏈接,所以需要再添加一些參數:

$mail->SMTPOptions = array(
    'ssl' => array(
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
    )
);

官方是這樣說明的:"

This is covered in the troubleshooting docs. PHP 5.6 verifies SSL certificates by default, and if your cert doesn't match, it will fail with this error. The correct solution is to fix your SSL config - it's not PHP's fault!

If you're sending on localhost you can use isMail() and it won't go through an encryption layer at all."

翻譯如下:

疑難解答文檔中介紹了這一點。默認情況下,PHP 5.6驗證SSL證書,如果您的證書不匹配,則會出現此錯誤。正確的解決方案是修復您的SSL配置 - 這不是PHP的錯誤! 如果您正在本地發送,您可以使用isMail(),而不會通過加密層。

 

可是,我用的是php7.1啊!可見,php7及以上版本保留了php5.6這一特點。加了上述參數后問題解決了。

不過,官方的建議是,為了安全起見,還是建議大家效驗ssl證書的。關於PHP是如何效驗ssl證書的,請參考:https://secure.php.net/manual/en/context.ssl.php

 

在PHPMailer里效驗證書的方法,是有給出配置的:

$mail->SMTPOptions = array (
    'ssl' => array(
        'verify_peer'  => true,
        'verify_depth' => 3,
        'allow_self_signed' => true,
        'peer_name' => 'smtp.example.com',
        'cafile' => '/etc/ssl/ca_cert.pem',
    )
);

 

出處:https://github.com/PHPMailer/PHPMailer/issues/368


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM