How to solve the “SMTP Error: Failed to connect to server” Error?

It is very annoying if you can’t access your email account for business or personal use. This article provides a couple of ways that you can fix the smtp error: failed to connect to server message.

Nowadays, the ability to send email is a basic requirement of any web application.

“PHPMailer is a code library that allows you to send emails securely and easily from a web server using PHP code. Sending emails directly from PHP code necessitates deep knowledge of SMTP protocol standards, as well as issues and vulnerabilities concerning Email injection for spamming”

The steps required to configure PHPMailer may be unfamiliar to many website owners, and configuration errors frequently result in the “Smtp error: Failed to connect to server” error.

We assist website owners in resolving technical difficulties. Additionally, mail issues related to PHPMailer are a common occurrence.

What is “Smtp error: Failed to connect to server”?

Spammers frequently use PHP scripts to connect directly to remote servers and send spam emails.

To combat this, many Web Hosting providers prevent websites from connecting directly to external mail servers.

Mails from such servers can be sent only through the website’s mail server (SMTP server) port, similar to how Outlook or Windows Mail works.

PHPMailer is a mail application that functions similarly to a mail client and enables the transmission of email via an SMTP server. However, PHPMailer does not work by default. It may fail due to server-side firewall restrictions, an incorrect mail server name or port, or other reasons, and displays the error:

Smtp error: Failed to connect to server

And, based on the mail server’s reaction, we’ve seen two distinct variations of this error:

MTP ERROR: Failed to connect to server: Connection refused (111)

Or

SMTP ERROR: Failed to connect to server: Connection timed out (110)

Causes: SMTP ERROR: Failed to connect to server?

1. SMTP restrictions on the server

Servers impose restrictions on the programs that can connect directly with remote servers and send an email. SMTP connections are typically permitted only by the mail server, the root user, etc.

For example, CPanel servers use the “SMTP Restrictions” option to restrict access to external SMTP servers.

This restriction prevents PHPMailer from connecting to an external mail server. The connection will hang for a while and then terminate 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 deny connections in accordance with specific firewall policies.

All mail servers accept connections via the standard mail port 25. However, other mail ports such as 465, 587, etc. will be closed on many servers.

When a website owner attempts to send email via an external SMTP server on port 465 on a server with mail port restrictions, the following error occurs:

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)

3. Incorrect settings in PHPMailer

This SMTP error can also occur if the mail server name is incorrectly configured in PHPMailer (with additional white space). The web form then attempts and fails to connect to an invalid name.

4. DNS failures

To ensure the proper operation of PHPMailer, the mail server specified in its configuration must have valid DNS records. When the server’s DNS fails, a lookup from the server shows an incorrect IP address or no IP address for the mail server. Again, this results in an SMTP error.

How to fix it?

To send and receive emails via PHPMailer, both the sending and receiving servers must accept connections.

  1. Modify the server’s firewall rules to permit outbound connections on ports such as 465.
  2. Change the server’s SMTP restrictions. Then, add the specified website user to the list of users who are authorized to initiate outbound SMTP connections.
  3. Edit PHPMailer’s configuration settings, such as host and port.
  4. Correct DNS resolution for the mail server.

Email authentication is a good thing, but it can also be a pain when you cannot access your email account because of the “smtp error: failed to connect to server” problem.

If you are still unable to resolve the issue? Our technical expert will take care of it. Contact us.

FREE SERVER AUDITING

Get Auditing Report of Your Server for FREE!!

Fix this issue for

$25/Hour

$6.25/Hour

Applicable only to the first hour.