30
votes

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.

16
What's your php version?Ifedi Okonkwo
In the meantime, I guess you've been able to solve this problem? It's been four months.Ifedi Okonkwo
@IfediOkonkwo unfortunately no :/omarsafwany
which server do you have? if you have xampp check your apache error log when it's start maybe you have a line like this one: AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server namerebduvid

16 Answers

23
votes

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
15
votes

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.

13
votes

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

6
votes

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
[email protected]
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' => '[email protected]', '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
4
votes

here's what is working with me

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=<your email>
MAIL_PASSWORD=<your app password>
MAIL_ENCRYPTION=tls

if you are two auth verification with your account , should be generate new app password and use into you env file. this is what working with me.

2
votes

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

2
votes

in localhost you can set your MAIL_DRIVER=smtp but on real server like cpanel you must to set MAIL_DRIVER=sendmail and edit your config/mail.php 'default' => env('MAIL_MAILER', 'sendmail'), but its not good idea because after you send mail on google you get error on gmail like this :

Be careful with this message Gmail could not verify that it actually came from [email protected]. Avoid clicking links, downloading attachments, or replying with personal information.

1
votes

Change mail driver in both .env and mail.php into MAIL_DRIVER=mailgun

0
votes

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.

0
votes

https://accounts.google.com/DisplayUnlockCaptcha https://www.google.com/settings/security/lesssecureapps

env

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=test
MAIL_ENCRYPTION=tls
/********/

email.php

'from' => ['address' => '[email protected]', 'name' => 'test'],
0
votes

If you are using Amazon EC2, do ensure to set outbound rules for SMTP (25) and SMTPS (465) to Anywhere.

MAIL_MAILER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD='yourgmailpassword'
MAIL_ENCRYPTION=ssl
[email protected]
MAIL_FROM_NAME="${APP_NAME}"
```



  [1]: https://i.stack.imgur.com/Q9rAn.png
0
votes

for me the reason was that my ISP or home router was somehow blocking SMTP protocol

0
votes

On Cpanel changing only the MAIL_DRIVER to sendmail from smtp in .env works for me.

MAIL_DRIVER=sendmail
MAIL_HOST=smtp.example.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls
MAIL_FROM_NAME="${APP_NAME}
[email protected]
-1
votes

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',
-1
votes
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=testpassword
-6
votes

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.