yii2配置邮件发送总是提示授权不正确的解决方案?
1.不管是配置哪类邮箱,都需要先开启stmp服务
2.在main.php配置文件中添加配置
//配置邮件
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail',
'useFileTransport' =>false,//这句一定有,false发送邮件,true只是生成邮件在runtime文件夹下,不发邮件
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.163.com', //每种邮箱的host配置不一样
'username' => '15157***@163.com',
'password' => '***',//163 登录密码 qq个人邮箱填写 stmp开启时的授权密码
'port' => '25',
'encryption' => 'tls',
],
'messageConfig'=>[
'charset'=>'UTF-8',
'from'=>['15157**@163.com'=>'admin']
],
],
],
//如果是腾讯企业邮箱
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.exmail.qq.com', //每种邮箱的host配置不一样
// 'host' => 'smtp.sina.com', //每种邮箱的host配置不一样
'username' => '**@admin.com',
'password' => '****',
'port' => '465',
'encryption' => 'ssl',
],
'messageConfig' => [
'charset' => 'UTF-8',
'from' => ['***@admin.com' => 'admin']
],
],
],
3.在控制器中
//测试发邮件
$mail= Yii::$app->mailer->compose();
$mail->setTo('151286***@qq.com');
$mail->setSubject("邮件测试");
$mail->setHtmlBody("<br>问我我我我我"); //发布可以带html标签的文本
if($mail->send())
echo "success";
else
echo "failse";
die();