Search code examples
phpyii2smtpswiftmailermailer

Cannot send mail with yii2 swiftmailer


I get error when trying send mail with Yii::$app->mailer->compose() function. This error appears when trying to connect smtp server, so I provide error message and mailer YII2 configuration

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 59sm3639427wrc.23 -> gsmtp"

Here is params from config/common.php file:

'mailer'       => [
        'class'            => 'yii\swiftmailer\Mailer',
        'useFileTransport' => false,
        'transport'        => [
            'class'      => 'Swift_SmtpTransport',
            'port'       => '587',
            'encryption' => 'tls',
            'username' => '[email protected]',
            'password' => 'myemailpass',
            'host' => 'smtp.mail.yahoo.com',
        ],
    ],

I already enabled "Allow apps that use less secure sign in" function in Yahoo account settings. Trying "app password" option but got the same result. Before using yahoo smtp I tried it the same way with google smtp. Error message still refers to the https://support.google.com page. Is it possible Apache cached login and pass to smtp server?

Of course I checked google support page and followed instructions included https://accounts.google.com/DisplayUnlockCaptcha page.


Solution

  • I resolved this case, but didn`t detect what exactly was the cause. Maybe there was some sort of cache of config error. So I put this direct setting right before compose function:

    \Yii::$app->mailer->setTransport([
            'class'      => 'Swift_SmtpTransport',
            'port'       => '587',
            'encryption' => 'tls',
           'username' => '[email protected]',
            'password' => 'myemailpass',
            'host' => 'smtp.mail.yahoo.com',
        ]);
    

    And it started to work.