验证码: 看不清楚,换一张 查询 注册会员,免验证
  • {{ basic.site_slogan }}
  • 打开微信扫一扫,
    您还可以在这里找到我们哟

    关注我们

laravel5发送ssl邮箱报错怎么解决

阅读:1162 来源:乙速云 作者:代码code

laravel5发送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 时缺少证书相关配置。

laravel5发送ssl邮箱报错怎么解决

其中需要注意的是 verify_peer_name 要求验证证书名默认值为true,这里是问题所以,当没有指定证书时该值会影响连接验证失败导致整个连接失败。因此需要修改代码并把 verify_peer_name 设置为 false。

但其增加了两行代码把 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;
    }
分享到:
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: hlamps#outlook.com (#换成@)。
相关文章
{{ v.title }}
{{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
你可能感兴趣
推荐阅读 更多>