Home » Php » php – Swift_TransportException in Laravel 5.2 mail sending

php – Swift_TransportException in Laravel 5.2 mail sending

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am trying to send mail for my laravel app from a gmail account with Allow less secure apps: ON and 2-Step Verification OFF

.env part for mail:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=********
MAIL_ENCRYPTION=ssl

config/mail.php:

'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.gmail.com'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => '[email protected]', 'name' => 'Admin'],
'encryption' => env('MAIL_ENCRYPTION', 'ssl'),
'username' => env('[email protected]'),
'password' => env('********'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => env('MAIL_PRETEND', false),

First I tried using 'encryption' => env('MAIL_ENCRYPTION','tls'). But got the following error message:

ERROR: exception 'Swift_TransportException' with message 'Expected
response code 250 but got code "530", with message "530-5.5.1
Authentication Required. Learn more at 
530 5.5.1  https://support.google.com/mail/answer/14257  
z3sm16020712par.17 - gsmtp"' in
/home/shafi/Projects/Lib/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php:383

I visited https://support.google.com/mail/answer/14257 and found everything is at the recommended state.

After this I used 'encryption' => env('MAIL_ENCRYPTION','ssl') but this time the same ERROR.

What should I do to fix the error? What am I missing?

How to&Answers:

Visit http://www.google.com/accounts/DisplayUnlockCaptcha and sign in with your Gmail username and password. If asked, enter the letters in the distorted picture.

Note: If this still didn’t help then retry the process couple of more times and wait then try to send the email from your Laravel apps. This should resolve the issue.

Answer:

Step 1: Revert your config/mail.php to original file. The config/mail.php should look like this

'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => '[email protected]', 'name' => 'Admin'],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',

Step 2: The .env file should be like

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=whateverisyourpassword
MAIL_ENCRYPTION=ssl

Port is 465 and not 587

Step 3: Test the below code by pasting in appropriate controller function

\Mail::raw('This is an test e-mail', function ($message) {
    $message->to("[email protected]", "someone");
    $message->subject("hi checking");
    $message->getSwiftMessage();
});

P.S: Never ever forget to stop and then again start the server by using php artisan serve every time if some change is made to .env file. If not then you may be sucked into The Labyrinth of Time.

Answer:

For me, sending mail worked by just adding the correct info in the .env file like so:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_gmail_email
MAIL_PASSWORD=your_gmail_password
MAIL_ENCRYPTION=tls

Don’t change anything in the mail.php file. You don’t want your email credentials all over the place. The .env file was made to store all of this.

I also turned on access to “less secure apps” on that Gmail account.

And lastly, run: php artisan config:cache

Hopefully that helps.

Answer:

I had the same problem previously, the root cause was I changed the mail.php file.

To send email with smtp.gmail.com, you just need to change the configuration in .env file and enable allow less secure apps. Don’t change anything in the mail.php file.

Answer:

I’d recommend you to take the time to read this :

https://phpnotebook.com/how-to-fix-swift_transportexception-530-in-laravel-5-7.html