Laravel - Connection could not be established with host smtp.gmail.com [ #0]

戏子无情 提交于 2019-11-30 06:43:40

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.

Kiran naik
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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!