1

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' => 'myEmail2131@gmail.com',
        'password' => 'password1234',
        'port' => '587',
        'encryption' => 'tls',
    ]
],

And my code.

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()

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' => 'myEmail2131@gmail.com',
        'password' => 'password1234',
        'port' => '465',
        'encryption' => 'ssl',
    ]
],

Any idea? Thanks!

Aljay
  • 456
  • 7
  • 21
  • Possible duplicate of [Laravel Gmail not working, "Username and Password not accepted. Learn more..."](https://stackoverflow.com/questions/25249476/laravel-gmail-not-working-username-and-password-not-accepted-learn-more) – Muhammad Omer Aslam May 17 '18 at 11:04

1 Answers1

1

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' => 'myEmail2131@gmail.com',
        '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

Aljay
  • 456
  • 7
  • 21