Getting "Connection could not be established with host smtp.gmail.com :stream_socket_client()" error in laravel 8 on localhost for sending email

Viewed 5256

I am trying to send email using gmail smtp in my laravel application which was working fine yesterday and emails were sending but today it is giving me this error. I tried every possible way but haven't figure out what is the problem. Any help would be appreciated. Thank you.

Here is my .env setting for smtp server

    MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=myemail
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=myemail
MAIL_FROM_NAME="${APP_NAME}"

I tried by changing

 MAIL_PORT= 465
 MAIL_ENCRYPTION = ssl

I also changed

MAIL_MAILER = sendmail

Still it is giving me the same error. I also allow access to unsecured apps from google but the error didn't go.

1 Answers

You can try to disable SSL Verification to check what's the problem. Open your config/mail.php file and add stream options like this:

<?php

return [
    'mailers' => [
        'smtp' => [
            'transport' => 'smtp',
            'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
            'port' => env('MAIL_PORT', 587),
            'encryption' => env('MAIL_ENCRYPTION', 'tls'),
            'username' => env('MAIL_USERNAME'),
            'password' => env('MAIL_PASSWORD'),
            'timeout' => null,
            'auth_mode' => null,
            // region Disable SSL Verify
            'stream' => [
                'ssl' => [
                    'allow_self_signed' => true,
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                ],
            ],
            // endregion
        ],
    ],
];

Note: This method is the only testing method to determine the problem, not the final solution due to security reasons.

Related