今天在用yii2发送邮件的时候提示
Connection could not be established with host smtp.exmail.qq.com [ #0]
openssl没啥问题,函数权限也有打开了
折腾半天后终于找到解决办法
出问题之前的代码
/common/config/main-local.php中mailer配置
控制器中代码
解决办法
在/common/config/main-local.php中mailer配置
//更多参数可以查看 http://php.net/manual/zh/context.ssl.php
其实在
在vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php 266行代码中
$streamContext = stream_context_create($options);
$this->_stream = @stream_socket_client($host.':'.$this->_params['port'], $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $streamContext);
中间加入
$streamContext = stream_context_create($options);
//----
stream_context_set_option($streamContext, 'ssl', 'verify_peer',false);
stream_context_set_option($streamContext, 'ssl', 'verify_peer_name',false);
stream_context_set_option($streamContext, 'ssl', 'allow_self_signed',true);
//-----
$this->_stream = @stream_socket_client($host.':'.$this->_params['port'], $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $streamContext);
也得到了解决,不过不如上面的办法
如果在使用phpmailer的时候也出现此问题,可以这样解决
ps:不知道啥原因导致的这样的情况,我本地是mac环境,没有这样的问题,到了linux服务器上就出现了这样的问题,只有加上这几个参数才可以。等找到真正原因再来写。
已有 0 条评论