Yii2 Swift_SmtpTransport gmail 不工作

Yii2 Swift_SmtpTransport gmail not working

我正在尝试使用 yii2 邮件程序组件发送电子邮件。

配置web.php

'mailer' => [
    'class' => 'yii\swiftmailer\Mailer',
    // 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,
    'transport' => [
        'class' => 'Swift_SmtpTransport',
        'host' => 'smtp.gmail.com',
        'username' => 'myEmail2131@gmail.com',
        'password' => 'password1234',
        'port' => '587',
        'encryption' => 'tls',
    ]
],

还有我的代码。

Yii::$app->mailer->compose()
        ->setFrom('myEmail07@gmail.com')
        ->setTo('toSomeone@gmail.com')
        ->setSubject('Some Subject here')
        ->setTextBody('Plain text content')
        ->setHtmlBody("<p> This is the body of email</p>")
        ->send()

我遇到了这个错误。

Swift_TransportException Expected response code 250 but got code "535", with message "535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials a13-v6sm4133042wrc.19 - gsmtp "

我已经按照此处所述配置了我的 Gmail 帐户

less secure app on on your gmail account

我也尝试使用 ssl 代替 tls。

'mailer' => [
    'class' => 'yii\swiftmailer\Mailer',
    // 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,
    'transport' => [
        'class' => 'Swift_SmtpTransport',
        'host' => 'smtp.gmail.com',
        'username' => 'myEmail2131@gmail.com',
        'password' => 'password1234',
        'port' => '465',
        'encryption' => 'ssl',
    ]
],

有什么想法吗?谢谢!

我找到了解决办法。

注意:我使用此方法只是为了测试。即将制作,我将使用我们公司的实际电子邮件。

我的邮件配置

'mailer' => [
    'class' => 'yii\swiftmailer\Mailer',
    // 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,
    'transport' => [
        'class' => 'Swift_SmtpTransport',
        'host' => 'smtp.gmail.com',
        'username' => 'myEmail2131@gmail.com',
        'password' => 'password1234',
        'port' => '587',
        'encryption' => 'tls',
    ]
],

然后这样做:

https://www.google.com/settings/security/lesssecureapps 并激活它。 https://accounts.google.com/b/0/DisplayUnlockCaptcha 并激活它。

Ankit Tyagi 的回答here