首页 > 代码库 > yii2发送邮件(配置QQ版本)

yii2发送邮件(配置QQ版本)

1:首先在配置文件main-local.php components=>[]里面配置

‘mailer‘ => [  
            ‘class‘ => ‘yii\swiftmailer\Mailer‘,  
            ‘useFileTransport‘ =>false,//这句一定有,false发送邮件,true只是生成邮件在runtime文件夹下,不发邮件
                ‘transport‘ => [  
                ‘class‘ => ‘Swift_SmtpTransport‘,  
                ‘host‘ => ‘smtp.qq.com‘,  //每种邮箱的host配置不一样,这里是QQ发送!
                ‘username‘ => ‘******@qq.com‘,  //这里是你本人的QQ邮箱
                ‘password‘ => ‘********‘,  //qq授权码(可以在邮箱设置/账户/)
                ‘port‘ => ‘465‘,  
                ‘encryption‘ => ‘ssl‘,                                  
                                   ],   
                ‘messageConfig‘=>[  
                ‘charset‘=>‘UTF-8‘,
                ‘from‘=>[‘*******@qq.com‘=>‘admin‘]  //这里邮箱是你本人邮箱
                       ],  
        ], 

技术分享

2:现在就可以在控制器里面调用了

  1. $info = Yii::$app->mailer->compose()
  2.                ->setTo(‘********@qq.com‘)  //对方的邮箱
  3.                ->setSubject(‘yii2邮件发送‘)  
  4.                ->setTextBody(‘发送成功‘)  
  5.                ->send(); 
  6.  var_dump($info);

此时邮件就发送成功了......0.0

 

yii2发送邮件(配置QQ版本)