Swift_TransportException in Laravel 5.2 mail sending
Solution 1
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.
Solution 2
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.
Shafi
Software engineer by profession and passion. Love to learn new things, ideas and help others to learn something.
Updated on June 04, 2022Comments
-
Shafi almost 2 years
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?
-
Md Mazedul Islam Khan about 8 yearsYour code is fine. However, Gmail is not allowing your Laravel apps to log in to your Gmail account which is why you're experiencing the issue. In this case, please also ensure that you've allowed your Laravel apps from your Gmail account activity options and also try changing few settings from the allow less secure apps access options.
-
Shafi about 8 yearsHow to allow Laravel apps from Gmail account? I tried to do, but couldn't.
-
Shafi about 8 yearsAppreciate your effort.I tried, but got the same error.
-
curious_coder about 8 yearsAre you sure you haven't missed anything? I test ran the above code in laravel 5.2 with two different gmail accounts and it's working.
-
Shafi about 8 yearsCan you share your project repository or files related to sending mail?
-
Shafi about 8 yearsNo. Tried in multiple way but ends with same error.
-
curious_coder about 8 yearsTry to provide your code to new set of eyes. Maybe they can find what you missed out
-
Shafi about 8 yearsI think I am doing everything okay as I did this before. I will try again when it's time to deploy my work on server. Till then I will work with
pretend: true
. Thanks for your care about this. -
Shafi about 8 yearsbtw, I do not think the port should be different in
.env
andmail.php
. -
curious_coder about 8 yearsFirst preference is given to variable found in .env . if the variable is not found then default value is taken