Yii2 使用十 发送邮件

时间:2022-10-05 09:05:38

1.配置web.php里mail信息

'mailer' => [  
           'class' => 'yii\swiftmailer\Mailer',  
           'transport' => [  
                                   'class' => 'Swift_SmtpTransport',  
                                   'host' => 'smtp.163.com',  
                                   'username' => 'account@163.com',  
                                   'password' => '*******',  
                                   'port' => '25',  
                                   'encryption' => 'tls',  
                                   
                           ],   
           'messageConfig'=>[  
               'charset'=>'UTF-8',  
               'from'=>['15618380091@163.com'=>'admin']  
               ],  
       ],  

2.controller写法

<?php
  $mail= Yii::$app->mailer->compose(); //加载模板这样写:$mail= Yii::$app->mailer->compose('moban',['key'=>'value']); 
  $mail->setTo('toaccount@qq.com');  
  $mail->setSubject("邮件测试");  
  $mail->setTextBody('textbody');  
  $mail->setHtmlBody("htmlbody");    
  if($mail->send())  
    echo "success";  
  else  
    echo "failse";   
  die(); 
?>