laravel 发送邮件 使用ssl 报错为:" stream_socket_client(): 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官方文档:php.net/manual/zh/context.ssl.php

wKiom1nbHRLBV0JdAADsz4MC7Do148.png

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

这个问题在 github.com/swiftmailer/swiftmailer... 中已经有说明。

但其增加了两行代码把 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 = [];

 if (!empty($this->params['sourceIp'])) {
  $options['socket']['bindto'] = $this->params['sourceIp'].':0';
  }

  if (isset($this->params['stream_context_options'])) {
  $options = array_merge($options, $this->params['stream_context_options']);
  }
 //在这里增加代码,修改默认值
 $options [ 'ssl' ][ 'verify_peer' ] = false;
 $options [ 'ssl' ][ 'verify_peer_name' ] = false;
  $streamContext = stream_context_create($options);

  set_error_handler(function ($type, $msg) {
  throw new Swift_TransportException('Connection could not be established with host '.$this->params['host'].' :'.$msg);
  });
 try {
  $this->stream = stream_socket_client($host.':'.$this->params['port'], $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $streamContext);
  } finally {
 restore_error_handler();
  }

  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;
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!