Select Page

Failed to connect to mailserver at – 5 causes & fixes for this PHP error

Here at, we provide Outsourced Tech Support to web hosting providers. As part of our service, we resolve website errors posted by hosting users.

A common error we’ve seen in PHP applications is:

Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini

Users report this error usually after a migration, or when setting up a new server or web application.


What is the error “Failed to connect to mailserver”?

Almost all PHP applications use a PHP feature (aka function) called “mail” to send mail.

That function will look for a program called “sendmail” (which is found in Unix/Linux servers), and transfer the mail to it.

If this program isn’t found, or if the mail server isn’t running, the application will fail with the error: “Failed to connect to mailserver at”.


We’ve seen many reasons for this error. Here are the top 5:


1. No “sendmail” in Windows servers

Perhaps the number 1 reporters of this error are Windows users.

PHP sends mail using a program called “sendmail”. However, unlike Linux servers, Windows doesn’t have a “sendmail” program.

We resolve this in two ways:

a. Install and/or configure a mail server

The fastest way is to re-configure IIS server to accept mails, because it will be installed by default in most Windows servers.

In case IIS is not installed, we setup open source mail servers like hMail to accept incoming mails.

Once this is setup, we reconfigure the app to connect to localhost.

b. Setup a sendmail replacement (wrapper)

There are several open source scripts that emulate the behavior of Linux’s sendmail program.

We install such scripts and:

  • Reconfigure PHP to connect to this script instead of the sendmail program.
  • Configure the script to send mail through a remote server (eg. a Gmail account).


2. No mail server installed or running

If the mail server isn’t running, PHP’s mail submission will fail.

We’ve seen two reasons for mail servers to not run:

  • Mail server isn’t configured or setup – In newly provisioned servers, mail servers may not be configured to start on reboot, or would simply be missing. In these cases, we install and configure the server to accept mails on localhost.
  • Mail server crashed due to errors – In recently updated or upgraded servers, we’ve seen mail servers to have crashed due to config or file errors. In these cases, we drill down to the root cause and bring the server back online.


3. Incorrect mail settings in the PHP application

Many applications use the mail server name (eg. to send mails.

However, for such connections to work, several settings need to be configured right:

  • SMTP port – Some servers won’t accept mails via port 25 as part of anti-spam configuration. So, alternate ports like 587 or 456 need to be used.
  • Authentication – As a security feature, mail servers will require a valid login to send mails. If it’s not configured, the mail connection will fail.
  • Relay authorization – Mail servers might look for return headers, and may require source whitelisting to accept mails. We set this up in the application as well as the mail server.
  • SSL/TLS settings – If the mail servers require SSL to connect, we use special mail wrappers like PHPMail to transfer mail.


4. Misconfigured PHP or wrong PHP config path

Often after a server migration, website owners continue using the old server’s config settings which may not match the new server.

For example, we’ve seen webmasters making changes to the PHP.ini in their home directory, whereas in the new server, the PHP path would be different.

We help webmasters setup the right values in the PHP configuration file, and guide them in using the new server environment.


5. SMTP Port 25 blocked by Firewall or Antivirus tools

In an effort to block spam scripts, many Firewalls and Antivirus tools block port 25 by default.

This includes tools such as McAffee, Norton, and more.

So, if we see everything is setup right, but the connection to the server fails, we look at the security tool configuration.

We then setup specific whitelisting to allow only the website scripts to send mail.


Bonus : Fixing this error in development environments (laptops, PCs, etc.)

As tech support for web hosts, our service is primarily limited to fixing issues in web servers.

However, that doesn’t prevent us from helping out hosting users setup their PCs to get their site online.

We’re often asked by developers how to fix this error in their Windows PCs.

One solution that is effective is to use a sendmail wrapper script.

We help configure the sendmail wrapper script to connect to the hosting provider’s mail server, and then the application to connect to the sendmail wrapper script.



PHP uses a function called “mail()” to transfer form data to a mail server. We’ve seen the top 5 reasons for this mail() function to fail, and how our Dedicated Support Engineers here at Bobcares fix them.



Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure./p>


Submit a Comment

Your email address will not be published. Required fields are marked *

Bobcares is a server management company that helps businesses deliver uninterrupted and secure online services. Our engineers manage close to 51,500 servers that include virtualized servers, cloud infrastructure, physical server clusters, and more.