yii2.0邮件发送问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34193883/article/details/82895578

yii2.0邮件发送问题

首先贴出我发送邮件的源码

$mail= Yii::$app->mailer->compose();   
$mail->setFrom(['[email protected]'=>$from]);  
$mail->setTo($to);  
if($cc){
	$mail->setCc($cc);  
}
$mail->setSubject($subject);  
$mail->setHtmlBody($body);//发布可以带html标签的文本
$mail->send();

这里我用的是yii2.0支持的swift_mailer插件

配置文件中配置为

 return [
        'components' => [
            'mailer' => [  
            'class' => 'yii\swiftmailer\Mailer',  
            'useFileTransport' =>false,//这句一定有,false发送邮件,true只是生成邮件在runtime文件夹下,不发邮件
            'transport' => [  
                'class' => 'Swift_SmtpTransport',  
                'host' => '127.0.0.1',  
                'username' => 'xx',  
                'password' => 'xxx',  
                'port' => 465,  
                'encryption' => 'ssl',                       
                           ],   
            'messageConfig'=>[  
               'charset'=>'UTF-8',  
               'from'=>['[email protected]'=>'哈哈']  
               ],  
        ],
        ],
    ];

当然,这个配置在PHP版本5.5环境下可以正常发送邮件,在5.6环境下却不可以发送邮件,这是因为PHP版本升级时变更了OpenSSL。

Exception 'Swift_TransportException' with message 'Connection could not be established with host 127.0.0.1 [ #0]'

in \vendor\swiftmailer\swiftmailer\lib\classes\Swift\Transport\StreamBuffer.php:271

这是5.6版本下的报错信息

修改方式通过查询得知有三种解决方案(前两种亲测有效,第三种失败)

  1. 修改php版本,降级(不建议)
  2. 更改端口或者屏蔽ssl的使用
    1. 端口25发送邮件
    2. 端口465发送邮件,但是不能采用ssl方式,【‘encryption’ => ‘ssl’】这句代码必须注释掉
    3. 端口587发送邮件,方式采用tls(比较安全)
  3. 更改邮件中ssl关于证书验证的配置
$options['ssl'] = array('verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true); //没有找到在哪修改(¬_¬)

上面解决方案中的第三种方案我还没有找到解决方法,有知道的可以回复我一下,谢谢!

猜你喜欢

转载自blog.csdn.net/qq_34193883/article/details/82895578
今日推荐