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:现在就可以在控制器里面调用了
- $info = Yii::$app->mailer->compose()
- ->setTo(‘********@qq.com‘) //对方的邮箱
- ->setSubject(‘yii2邮件发送‘)
- ->setTextBody(‘发送成功‘)
- ->send();
- var_dump($info);
此时邮件就发送成功了......0.0