I\'m trying to send an email from Gmail using Laravel from localhost. I\'m getting this error: Connection could not be established with host smtp.gmail.com [ #0]
I\'
I had the same error: Connection could not be established with host smtp.gmail.com [ #0] and followed the steps outlined here: https://github.com/swiftmailer/swiftmailer/issues/544.
Try adding the following lines to _establishSocketConnection()
in Swift/Transport/StreamBuffer.php on line 263:
$options['ssl']['verify_peer'] = FALSE;
$options['ssl']['verify_peer_name'] = FALSE;
Note that this is not a perfect solution as it involves: a) Hacking core code which may be overwritten in a SwiftMailer update, and b) decreases the security of your app as you're no longer validating the connection. But as a temporary work-around it worked for me.
This one line change in .env
file will make it work
MAIL_DRIVER=sendmail
If it also has no effect try to switch between the mail port
MAIL_PORT=587
Or
MAIL_PORT=465
This will work only if you enable "Allow Less secure app access" under google account
this worked for me after a long search time, the best configuration :
'driver' => 'smtp',
'host' => 'smtp-mail.outlook.com',
'port' => 587, ( or 25)
'encryption' => 'tls',
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=test@gmail.com
MAIL_PASSWORD=testpassword
In your .env file you will need to set the email address and password of your email account:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=test@gmail.com
MAIL_PASSWORD=testpassword
and in mail.php
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => 'yourEmail@gmail.com', 'name' => 'Your Title'],
'encryption' => 'tls',
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
and clear the config cache with:
php artisan config:cache
https://accounts.google.com/DisplayUnlockCaptcha https://www.google.com/settings/security/lesssecureapps
env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=test@gmail.com
MAIL_PASSWORD=test
MAIL_ENCRYPTION=tls
/********/
email.php
'from' => ['address' => 'test@gmail.com', 'name' => 'test'],