Connection could not be established with host smtp.gmail.com [Connection refused #111] in laravel 5.6 email send

15,241

Solution 1

Problem Solved. I made the change two things here MAIL_DRIVER and MAIL_PORT.

MAIL_DRIVER=sendmail
MAIL_PORT=587

So, my full code is:

MAIL_DRIVER=sendmail
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=ssl

It works fine

Solution 2

After couple of days research. I was having issue of Connection could not be established with host smtp.gmail.com [Connection refused ] because of my godaddy server

As GoDaddy blocks SMTP communication over ports 465 and 587 and possibly 25 originating from your site. They will allow mail flow only through their own SMTP servers.

That was issue in my case. hope it helps someone

For reference check this: https://pk.godaddy.com/community/Using-WordPress/Connection-Refused-for-GMail-SMTP/td-p/33107

Solution 3

I know this question has been answered but I have an alternative solution that matches the original description and might help someone. You did not answer Loek's question about whether you run cPanel or not. We run cPanel with lfd/csf firewall, and it has TCP_OUT configuration that lists what outbound ports may be connected to. Port 465 (tls) was not in that list, so we got 'Connection Refused' reports, even trying a low level telnet test on the command line of our VPS server:

$ telnet smtp.gmail.com 465
Trying 74.125.140.109...
telnet: connect to address 74.125.140.109: Connection refused
Trying 74.125.140.108...
telnet: connect to address 74.125.140.108: Connection refused
Trying 2a00:1450:400c:c08::6d...
telnet: connect to address 2a00:1450:400c:c08::6d: Connection refused

The same telnet test worked fine from my local machine.

After adding 465 to the TCP_OUT list:

  • Load WHM control panel
  • go to ConfigServer Security & Firewall
  • go to Firewall Configuration
  • find TCP_OUT and include 465 (or whatever port you are trying to connect to) to the list
  • click Change at the bottom of the page, then Restart lfd/csf.

Telnet now succeeds, as does smtpauth mail sending via PHPMailer:

$ telnet smtp.gmail.com 465
Trying 74.125.140.109...
Connected to smtp.gmail.com.
Escape character is '^]'.

(this indicates the socket connection was established.. you probably don't want to actually talk SMTP to Google so hit ctrl-] and type quit and hit enter to close the connection)

Of course this isn't cPanel specific, you may be running another firewall manager that is not allowing outbound connections on the port you're trying to connect to.

Share:
15,241
Amanullah Aman
Author by

Amanullah Aman

Full Stack Web Application developer with more than 3 years experience. My Backend Expertise is PHP (Codeigniter, Laravel), ASP.NET Core 2.0. My front-end expertise are JavaScript, JQuery, HTML5, CSS, Ajax. Always trying to learn new technology & try to solve problems. I'm flexible with my working hours and am happy to work closely with anyone.

Updated on June 04, 2022

Comments

  • Amanullah Aman
    Amanullah Aman almost 2 years

    Email Sending failed from cpnel but it works fine in my localhost.

    Here is my email configuration in .env file.

    MAIL_DRIVER=smtp
    MAIL_HOST=smtp.gmail.com
    MAIL_PORT=465
    [email protected]
    MAIL_PASSWORD=mypassword
    MAIL_ENCRYPTION=ssl
    

    It works fine in localhost but not working in cpanel. In cpanel it shows

    Connection could not be established with host smtp.gmail.com [Connection refused #111]
    

    How do i solve this?

  • Amanullah Aman
    Amanullah Aman over 5 years
    Welcome @BMShamsNahid
  • Abhi Burk
    Abhi Burk over 4 years
    I do not send from smtp.gmail.com server it sends from default hosting SMTP