These days, an option to send mails is a basic requirement of any web application.
So popular applications like WordPress, Drupal etc. include a mail program called “PHPMailer” for sending mails.
The steps to setup PHPMailer may not be intuitive to many website owners and mistakes in configuration often cause “Smtp error: Failed to connect to server” error.
As part of our Support Services, we help website owners solve their technical issues. And, mail issue related with PHPMailer is an error that we see often .
In this article, we’ll see the top reasons for “Smtp error: Failed to connect” and how we fix them.
What is “Smtp error: Failed to connect to server” ?
Spammers often use php scripts that directly connect to remote servers and send spam mails.
To defend this, many Web Hosting providers block direct connection from websites to external mail servers.
In such servers, mails from website can be sent only via its own mail server (SMTP server) port, just as how Outlook or Windows Mail works.
PHPMailer is a mail application that works like a mail client and helps to send mail via SMTP server.
But, PHPMailer do not work out of the box. It can fail due to firewall restrictions on the server, wrong mail server name, port etc. and shows the error:
“Smtp error: Failed to connect to server”
And, depending on the response from the mail server, we’ve seen 2 variations of this error :
SMTP ERROR: Failed to connect to server: Connection refused (111)
or
SMTP ERROR: Failed to connect to server: Connection timed out (110)
What causes SMTP ERROR: Failed to connect to server ?
Here, let us discuss the top reasons for “SMTP ERROR: Failed to connect to server”.
1. SMTP restrictions on the server.
Servers restrict the programs that can directly connect to remote servers and send mail. Usually, only mail server, root user etc. allow SMTP connections.
For example, CPanel servers block access to external SMTP servers using the “SMTP Restrictions” option.
With this restriction, connection from PHPMailer to an external mail server do not work. The connection wait for some time and eventually die with the following error:
2018-10-12 04:12:37 SMTP ERROR: Failed to connect to server: Connection timed out (110) SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Oops! Something went wrong and we couldn't send your message.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
2. Firewall restrictions on the server
Mail servers accept or reject connections based on certain firewall policies.
All mail servers allow the connection from default mail port 25. Bu,t other mail ports like 465, 587 etc. will not be open in many servers.
On a server with mail port restrictions, when a website owner tries to send mail using an external smtp server on port 465, it ends up in error:
2018-08-28 10:33:12 Connection: Failed to connect to server. Error number 2. "Error notice: stream_socket_client(): unable to connect to ssl://mail.xyz.com:465 (Connection refused) 2018-08-28 10:33:12 SMTP ERROR: Failed to connect to server: Connection refused (111)
Here, this “Connection Refused” error means that sending mail server refuses outbound connections on port 465 and is not able to connect to remote mail server.
3. Incorrect settings in PHPMailer
This SMTP error can also happen if the mail server name is incorrectly set (with additional white space) in PHPMailer configuration. Then, web form tries to connect to an invalid name and fails.
4. DNS failures
For the PHPMailer to work properly, the mail server specified in its configuration should have proper dns records. When dns do not work on the server, a look up from the server shows wrong IP address or no IP address for the mail server. Again, that causes mail to fail with SMTP error.
How to fix SMTP Error: Failed to connect to server
For mails to work with PHPMailer, both sending and receiving server has to accept connections.
Our Support Engineers primarily checks the connection between mail servers and find whether it is an incoming or outgoing block.
Then, to fix the mail error, we make changes on the server that includes the following :
- Modify the firewall rules on the server to allow outbound connections on ports like 465.
- Modify the SMTP restrictions on the server. Then, add particular website user to the list of users who can make outbound SMTP connections.
- Edit PHPMailer settings like Host, Port etc.
- Correct DNS resolution for mail server.
Conclusion
“SMTP ERROR: Failed to connect to server” mainly happens because of mail server connectivity issues, wrong port settings etc. Here, we have discussed the causes that our Support Engineers often see in servers and how we fix them.
I want to send an email through SMTP. But its not sending due to some error.
Hello Ashiq,
Can you please share the SMTP error that you are getting? If you need help, we’ll be happy to talk to you on chat (click on the icon at right-bottom).
I keep getting this error on hostinger hpanel, but when i use namecheap, it worked:
Message could not be sent. Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I keep getting this error on hostinger hpanel, but when i use namecheap, it worked:
Message could not be sent. Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
I m also getting “failed to connect (101)” . it was working fine on local server but when I uploaded my Website it is no longer working. Please help
Hello Arush,
If you still face the issue, We’ll be happy to talk to you on chat (click on the icon at right-bottom). We need to look into the details of the error.
get smtp error I configure all settings accurately fine but its not working
Versions:
WordPress: 5.9.2
WordPress MS: No
PHP: 7.3.33
WP Mail SMTP: 3.3.0
Params:
Mailer: smtp
Constants: No
ErrorInfo: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Host: mail.ekotees.com
Port: 465
SMTPSecure: ssl
SMTPAutoTLS: bool(true)
SMTPAuth: bool(true)
Server:
OpenSSL: OpenSSL 1.1.1d 10 Sep 2019
Apache.mod_security: No
Debug:
Email Source: WP Mail SMTP
Mailer: Other SMTP
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
SMTP Debug:
2022-03-14 06:40:20 Connection: opening to ssl://mail.ekotees.com:465, timeout=300, options=array()
2022-03-14 06:40:20 Connection failed. Error #2: stream_socket_client(): Peer certificate CN=`rumi.corpservers.net’ did not match expected CN=`mail.ekotees.com’ [/home/ekoteesc/public_html/wp-includes/PHPMailer/SMTP.php line 388]
2022-03-14 06:40:20 Connection failed. Error #2: stream_socket_client(): Failed to enable crypto [/home/ekoteesc/public_html/wp-includes/PHPMailer/SMTP.php line 388]
2022-03-14 06:40:20 Connection failed. Error #2: stream_socket_client(): unable to connect to ssl://mail.ekotees.com:465 (Unknown error) [/home/ekoteesc/public_html/wp-includes/PHPMailer/SMTP.php line 388]
2022-03-14 06:40:20 SMTP ERROR: Failed to connect to server: (0)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Hi,
Our Experts can help you with the issue, we’ll be happy to talk to you on chat