Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection could not be established with host smtp.sendgrid.net :stream_socket_client(): unable to connect to tcp://smtp.sendgrid.net:465

I am trying to send mail using sendgrid in Laravel but it is working on localserver but as i hosted it on server it is giving me following error message:

enter image description here

my mail settings in .env file:

MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=myusername
MAIL_PASSWORD=XXX
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME=xyz
like image 826
user3653474 Avatar asked Sep 20 '25 17:09

user3653474


2 Answers

For laravel >= 7 And you are getting this error :

Connection could not be established with host smtp.sendgrid.net :stream_socket_client(): unable to connect to tcp://smtp.sendgrid.net:587 (A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

Simply update your .env file From:

MAIL_MAILER=smtp
MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=apikey
MAIL_PASSWORD=your_sendgrid_api_key
MAIL_ENCRYPTION=tls
MAIL_FROM_NAME="Your Sender Name"
[email protected]

To:

MAIL_MAILER=sendMail
MAIL_DRIVER=sendMail
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=apikey
MAIL_PASSWORD=your_sendgrid_api_key
MAIL_ENCRYPTION=tls
MAIL_FROM_NAME="Your Sender Name"
[email protected]

Then run this command to make sure .env changes is sync with the code.

php artisan config:cache
like image 118
Lekens Avatar answered Sep 22 '25 06:09

Lekens


Update your env file to:

MAIL_MAILER=sendmail
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=apikey
MAIL_PASSWORD=apiPassword
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="${APP_NAME}"

Also in your Laravel project under the config folder look for

mail.php

and make changes as follows:

'default' => env('MAIL_MAILER', 'sendmail'),

N/B: SMTP works on localhost and sendmail works on live server.

like image 22
Codegeoss Avatar answered Sep 22 '25 06:09

Codegeoss