Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift_TransportException in Laravel 5.2 mail sending

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?

like image 782
Shafi Avatar asked Feb 12 '16 05:02

Shafi


2 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.

like image 146
Md Mazedul Islam Khan Avatar answered Sep 24 '22 23:09

Md Mazedul Islam Khan


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.

like image 44
curious_coder Avatar answered Sep 22 '22 23:09

curious_coder