laravel5發送ssl郵箱問題

laravel5已經有很好的郵件發送功能,但都是常規 tls 不加密協議,現在有的雲服務器已經慢慢禁止使用不加密協議,要求使用ssl加密協議;如阿里雲新購買的服務器都開始禁止。


由於laravel5默認使用的是 swiftmailer 擴展。發送使用的是 stream 其中並未對ssl提供證書等內容配置,所以當使用ssl時又未指定證書時會錯:

Connection could not be established with host *******.com [ #0]

連接失敗,造成錯誤的地方:vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php 類

Swift_Transport_StreamBuffer 的 _establishSocketConnection 方法在調用 stream_context_create 時缺少證書相關配置。

看看PHP官方文檔:http://php.net/manual/zh/context.ssl.php


wKiom1nbHRLBV0JdAADsz4MC7Do148.png

其中需要注意的是 verify_peer_name 要求驗證證書名默認值爲true,這裏是問題所以,當沒有指定證書時該值會影響連接驗證失敗導致整個連接失敗。因此需要修改代碼並把 verify_peer_name 設置爲 false。

這個問題在 https://github.com/swiftmailer/swiftmailer/issues/544 中已經有說明。

但其增加了兩行代碼把 verify_peer 和 verify_peer_name 都設置爲false 。依文檔中看,verify_peer 默認值已經是 false ,所以可以不加。

修改代碼如下:

    /**
     * Establishes a connection to a remote server.
     */
    private function _establishSocketConnection()
    {
        $host = $this->_params['host'];
        if (!empty($this->_params['protocol'])) {
            $host = $this->_params['protocol'].'://'.$host;
        }
        $timeout = 15;
        if (!empty($this->_params['timeout'])) {
            $timeout = $this->_params['timeout'];
        }
        $options = array();
        if (!empty($this->_params['sourceIp'])) {
            $options['socket']['bindto'] = $this->_params['sourceIp'].':0';
        }
        
        //在這裏增加代碼,修改默認值
        $options['ssl']['verify_peer_name'] = FALSE;
        
        $this->_stream = @stream_socket_client($host.':'.$this->_params['port'], $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, stream_context_create($options));
        if (false === $this->_stream) {
            throw new Swift_TransportException(
                'Connection could not be established with host '.$this->_params['host'].
                ' ['.$errstr.' #'.$errno.']'
                );
        }
        if (!empty($this->_params['blocking'])) {
            stream_set_blocking($this->_stream, 1);
        } else {
            stream_set_blocking($this->_stream, 0);
        }
        stream_set_timeout($this->_stream, $timeout);
        $this->_in = &$this->_stream;
        $this->_out = &$this->_stream;
    }

當然如果把 verify_peer 加上也沒有問題。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章