Php yii2中不接受用户名和密码

Php yii2中不接受用户名和密码,php,email,yii,gmail,yii2,Php,Email,Yii,Gmail,Yii2,我的yii2有一个错误,我不能用电子邮件帐户通过yii发送电子邮件。如果我的密码正确:( 这是我的代码: web.php 'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', 'transport' => [ 'class' => 'Swift_SmtpTransport', 'host' => 'sm

我的yii2有一个错误,我不能用电子邮件帐户通过yii发送电子邮件。如果我的密码正确:( 这是我的代码:

web.php

'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'transport' => [
            'class'      => 'Swift_SmtpTransport',
            'host'       => 'smtp.gmail.com',
            'username'   => 'user@hya.com.mx',
            'password'   => 'passwd',
            'port'       => '587',
            'encryption' => 'tls',
            ],
        ],
        'log'
Yii::$app -> mailer -> compose()
          -> setFrom('users@hya.com.mx')
          -> setTo('jhon@hya.com.mx')
          -> setSubject('Test')
          -> setTextBody('Plain text content')
          -> setHtmlBody('It is a test')
          -> send();
Controller.php

'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'transport' => [
            'class'      => 'Swift_SmtpTransport',
            'host'       => 'smtp.gmail.com',
            'username'   => 'user@hya.com.mx',
            'password'   => 'passwd',
            'port'       => '587',
            'encryption' => 'tls',
            ],
        ],
        'log'
Yii::$app -> mailer -> compose()
          -> setFrom('users@hya.com.mx')
          -> setTo('jhon@hya.com.mx')
          -> setSubject('Test')
          -> setTextBody('Plain text content')
          -> setHtmlBody('It is a test')
          -> send();

看起来您正在使用谷歌SMTP服务器。谷歌有一个新的安全检查,只允许您从谷歌应用程序发送电子邮件。如果您使用任何其他应用程序,您将遇到此类错误。要修复此问题,您可以执行以下操作:

使用默认的sendmail功能

 'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'useFileTransport' => false,
        ],
我发现第一个解决方案更有效

更改谷歌设置以允许不太安全的应用程序


按照此链接更改您的gmail设置

我使用以下配置并正常工作

实际上等于您的用户名,但不同的是用户名是google mail用户,而不是noyt google app用户

    'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'viewPath' => '@common/mail',
        // send all mails to a file by default. You have to set
        // 'useFileTransport' to false and configure a transport
        // for the mailer to send real emails.
        //'useFileTransport' => true,
        'useFileTransport' => false,//set this property to false to send mails to real email addresses
        //comment the following array to send mail using php's mail function
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp.gmail.com',
            'username' => 'myname@gmail.com',
            'password' => 'mypassword',
            'port' => '587',
            'encryption' => 'tls',
        ],
    ],

希望有用

请将错误粘贴到问题中。