Yii2 Swift_SmtpTransport gmail不能正常工作

时间:2018-05-17 06:41:27

标签: php yii yii2 gmail swiftmailer

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

config 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预期的响应代码250,但得到了代码   &#34; 535&#34;,带消息&#34; 535-5.7.8不接受用户名和密码。   在535 5.7.8了解更多信息   https://support.google.com/mail/?p=BadCredentials   a13-v6sm4133042wrc.19 - gsmtp&#34;

我已按照此处enter link description here

配置我的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',
    ]
],

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

注意:我使用此方法仅用于测试。即将开始制作,我将使用我们公司的实际电子邮件。

我的邮件配置

'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