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