8

I am trying to send email from my laravel 5.1 system. I can send emails from my localhost and unable to send from the server. See my email configuration settings in .env file,

MAIL_DRIVER=smtp
MAIL_HOST= smtp.gmail.com
MAIL_PORT= 587
MAIL_USERNAME= username***@gmail.com
MAIL_PASSWORD= *********
MAIL_ENCRYPTION=tls

This configuration only work on my localhost. On server I am getting this error,

Swift_TransportException in StreamBuffer.php line 268:
Connection could not be established with host smtp.gmail.com [Network is unreachable #101]

I also try with changing MAIL_PORT from 587 to 465 and MAIL_ENCRYPTION from tls to ssl. But I am geeitng the same error. How can I fix this issue?

1

5 Answers 5

37

I solved my problem in cpanel using the code:

change MAIL_DRIVER=smtp to MAIL_DRIVER=sendmail in ".env" file
6
  • It worked for me as well. Why it's working ? Any one know the reason behind it ?
    – Bhavin
    Commented Apr 19, 2018 at 14:55
  • 1
    its working. Am also surprised. Somebody should explain Commented Feb 8, 2019 at 21:49
  • 1
    It doesn't solve the issue because it uses local SMTP server to send emails rather than host you've set. You can try to put wrong mail driver, user and password and it still would "work". Not recommended. Commented Jul 30, 2019 at 12:53
  • This worked because the server didn't calling into Gmail server but using sendmail service to send the email.
    – minhhq
    Commented Sep 1, 2019 at 9:12
  • Then what you would recommend to solve the issue? @minhhq
    – Nur Uddin
    Commented Sep 2, 2019 at 5:45
2

1.) clear the cache on the server after changing the configuration in the .env file.

php artisan cache:clear;
php artisan config:cache;

and if the error still comes

Connection refused is a very explicit and clear error message. It means that the socket connection could not be established because the remote end actively refused to connect.

It's very unlikely that Google is blocking the connection.

It's very likely that your web hosting provider has firewall settings that block outgoing connections on port 465, or that they are blocking SMTP to Gmail. 465 is the "wrong" port for secure SMTP, though it is often used, and Gmail does listen there. Try port 587 instead. If the connection is still refused, call your host and ask them what's up.

0
2

If your website URL is http://yourdomain.com

you need to use this port 587

or if your website URL is https://yourdomain.com

you need to use this port 25

0
2

Localhost -> worked
Production -> Not Worked
Review your Production Firewall policies or Hosting Provider policies if they blocks email ports (because those ports were heavily spammed)

1

acces to your server and edit nano /etc/csf/csf.conf

SMTP_ALLOWUSER = "cpanel,yourusr"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.