Yii2发送邮件功能

1.在配置文件main-local.php components=>[]里面配置(以QQ邮箱为例)

'mailer' => [  
   'class' => 'yii\swiftmailer\Mailer',  
    'useFileTransport' =>false,//这句一定有,false发送邮件,true只是生成邮件在runtime文件夹下,不发邮件
   'transport' => [  
       'class' => 'Swift_SmtpTransport',  
       'host' => 'smtp.exmail.qq.com',  //每种邮箱的host配置不一样
       'username' => '[email protected]',  
       'password' => '*******',  
       'port' => '465',  
       'encryption' => 'ssl',  
                           
                   ],   
   'messageConfig'=>[  
       'charset'=>'UTF-8',  
       'from'=>['[email protected]'=>'admin']  
       ],  
],  
  1. controller控制器中 代码:
<?php
$mail= Yii::$app->mailer->compose();   
$mail->setTo('***********@qq.com');  
$mail->setSubject("邮件测试");  
//$mail->setTextBody('zheshisha ');   //发布纯文字文本
$mail->setHtmlBody("<br>问我我我我我");    //发布可以带html标签的文本
if($mail->send())  
    echo "success";  
else  
    echo "failse";   
die(); 
?>

ok,这样就可以发送邮件了

猜你喜欢

转载自blog.csdn.net/weixin_43452467/article/details/113772484