user3653474
user3653474

Reputation: 3852

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

Upvotes: 4

Views: 14173

Answers (5)

Shaolin
Shaolin

Reputation: 434

when I migrated to laravel 9, sending with sendgrid didn't work anymore. The problem was because the new symfony mailer was trying to send with ssl beside tls was set in the config file. So in the config file, I changed the port from 587 to 465 and it worked again.

Upvotes: 0

forestbaba
forestbaba

Reputation: 317

I Experienced this while using a Linode server. Apparently, Some service provider like Linode blocks an SMTP port until you reach out to them via support and ask them for unblock before you can use that PORT and send Email. That was the fix for me

Upvotes: 1

Grimmret
Grimmret

Reputation: 221

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.

Upvotes: 3

Lekens
Lekens

Reputation: 1969

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

Upvotes: 4

user8555937
user8555937

Reputation: 2395

If you say it works locally but not on production server, try a php artisan config:cache on your web host server. Make sure the ENV configuration is loaded properly (if your host does not support SSH connections, you need to manually update the configuration from ENV into /app/config/* files.


If the issue persists, there are only two possibilities:

  1. Make sure your web host allows outgoing SMTP connections.
  2. Make sure sendgrid.net did not ban your web host outgoing SMTP IP address.

Upvotes: 0

Related Questions