Swift_TransportException in sending Laravel 5.2 mail

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

.env for mail:

MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=library@gmail.com 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' => ' library@gmail.com ', 'name' => 'Admin'], 'encryption' => env('MAIL_ENCRYPTION', 'ssl'), 'username' => env(' library@gmail.com '), 'password' => env('********'), 'sendmail' => '/usr/sbin/sendmail -bs', 'pretend' => env('MAIL_PRETEND', false), 

At first I tried to use 'encryption' => env('MAIL_ENCRYPTION','tls') . But the following error message appeared:

 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 that everything is in recommended condition.

After that 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?

+7
source share
5 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 image.

Note. . If this still does not help, repeat the process a couple more times and wait, and then try to send an email from your Laravel applications. This should solve the problem.

+2
source

Step 1 Return your config / mail.php file to the original file. The configuration /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' => ' library@gmail.com ', '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 look like

 MAIL_DRIVER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=465 MAIL_USERNAME=library@gmail.com MAIL_PASSWORD=whateverisyourpassword MAIL_ENCRYPTION=ssl 

Port 465 , not 587

Step 3 Test the code below by inserting the appropriate controller function.

 \Mail::raw('This is an test e-mail', function ($message) { $message->to(" someone@gmail.com ", "someone"); $message->subject("hi checking"); $message->getSwiftMessage(); }); 

PS: Never forget to stop and then start the server again using php artisan serve every time if any change is made to the .env file. If not, then you can be pulled into the Labyrinth of Time .

+1
source

For me, sending mail worked by simply adding the correct information to the .ENV file, for example:

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

Do not modify anything in the mail.php file. You do not need your email credentials everywhere. The .env file was created to save all this.

I also enabled access to "less secure applications" in this Gmail account.

And finally run: php artisan config:cache

Hope this helps.

0
source

I had the same problem earlier, the main reason I changed the mail.php file.

To send an email from smtp.gmail.com, you just need to change the configuration in the .env file and enable the allowed less secure applications. Do not modify anything in the mail.php file.

0
source

All Articles