laravel5已经有很好的邮件发送功能,但都是常规 tls 不加密协议,如今有的云服务器已经慢慢禁止使用不加密协议,要求使用ssl加密协议;如阿里云新购买的服务器都开始禁止。php
因为laravel5默认使用的是 swiftmailer 扩展。发送使用的是 stream 其中并未对ssl提供证书等内容配置,因此当使用ssl时又未指定证书时会错:laravel
Connection could not be established with host *******.com [ #0]git
链接失败,形成错误的地方:vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php 类
github
Swift_Transport_StreamBuffer 的 _establishSocketConnection 方法在调用 stream_context_create 时缺乏证书相关配置。swift
看看PHP官方文档:http://php.net/manual/zh/context.ssl.php
服务器
其中须要注意的是 verify_peer_name 要求验证证书名默认值为true,这里是问题因此,当没有指定证书时该值会影响链接验证失败致使整个链接失败。所以须要修改代码并把 verify_peer_name 设置为 false。socket
这个问题在 https://github.com/swiftmailer/swiftmailer/issues/544 中已经有说明。ide
但其增长了两行代码把 verify_peer 和 verify_peer_name 都设置为false 。依文档中看,verify_peer 默认值已是 false ,因此能够不加。this
修改代码以下:阿里云
/** * 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 加上也没有问题。