Aljay
Aljay

Reputation: 456

Yii2 Swift_SmtpTransport gmail not working

I'm trying to send an email using yii2 mailer component.

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' => '[email protected]',
        'password' => 'password1234',
        'port' => '587',
        'encryption' => 'tls',
    ]
],

And my code.

Yii::$app->mailer->compose()
        ->setFrom('[email protected]')
        ->setTo('[email protected]')
        ->setSubject('Some Subject here')
        ->setTextBody('Plain text content')
        ->setHtmlBody("<p> This is the body of email</p>")
        ->send()

I'm getting this error.

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 "

I already configure my Gmail account as said here enter link description here

less secure app on on your gmail account

And I also try to use ssl instead 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' => '[email protected]',
        'password' => 'password1234',
        'port' => '465',
        'encryption' => 'ssl',
    ]
],

Any idea? Thanks!

Upvotes: 1

Views: 1894

Answers (1)

Aljay
Aljay

Reputation: 456

I've found a solution.

Note: I use this method just for testing. soon on production, I will use an actual email of our company.

My mail config

'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' => '[email protected]',
        'password' => 'password1234',
        'port' => '587',
        'encryption' => 'tls',
    ]
],

Then do this:

https://www.google.com/settings/security/lesssecureapps and active it. https://accounts.google.com/b/0/DisplayUnlockCaptcha and active it.

As answer from Ankit Tyagi here

Upvotes: 1

Related Questions