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'm using ssl with port 465. I also tried 587 but it didn't work.
I also tried this but it didn't work. I found a lot of people suffering from the same problems, but the solutions I found didn't work.
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.
In your .env file you will need to set the email adres 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 filled in at 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 this.
php artisan config:cache
In Laravel project directory, edit config/mail.php
and add the following:
'stream' => [
'ssl' => [
'allow_self_signed' => true,
'verify_peer' => false,
'verify_peer_name' => false,
],
]
It worked for me.
Fyi, my SMTP settings are:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=465
MAIL_USERNAME=[Full Gmail Address]
MAIL_PASSWORD=[Google App Password obtained after two step verification on Google Account Page]
MAIL_ENCRYPTION=ssl
Have you tried changing the encryption to tls? I currently use a Gmail SMTP sever to send emails from my Laravel app. I use TLS and port 587
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'],
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',
For me, it turned out TripMode blocked nginx & php-fpm. So make sure no app like this or firewall is blocking the connection to the mail server.
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=test@gmail.com
MAIL_PASSWORD=testpassword
You can't send emails from your localhost by default.
If you configure your files correctly, you would be able to send emails from it.
You could also try to upload the site on an server or production zone. There it will work.
来源:https://stackoverflow.com/questions/27721120/laravel-connection-could-not-be-established-with-host-smtp-gmail-com-0