Swift_TransportException in Laravel 5.2 mail sending

19,505

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.

Share:
19,505
Shafi
Author by

Shafi

Software engineer by profession and passion. Love to learn new things, ideas and help others to learn something.

Updated on June 04, 2022

Comments

  • Shafi
    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
    Md Mazedul Islam Khan about 8 years
    Your 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
    Shafi about 8 years
    How to allow Laravel apps from Gmail account? I tried to do, but couldn't.
  • Shafi
    Shafi about 8 years
    Appreciate your effort.I tried, but got the same error.
  • curious_coder
    curious_coder about 8 years
    Are 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
    Shafi about 8 years
    Can you share your project repository or files related to sending mail?
  • Shafi
    Shafi about 8 years
    No. Tried in multiple way but ends with same error.
  • curious_coder
    curious_coder about 8 years
    Try to provide your code to new set of eyes. Maybe they can find what you missed out
  • Shafi
    Shafi about 8 years
    I 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
    Shafi about 8 years
    btw, I do not think the port should be different in .env and mail.php.
  • curious_coder
    curious_coder about 8 years
    First preference is given to variable found in .env . if the variable is not found then default value is taken