Bobcares

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

by | Sep 8, 2018

Here at Bobcares.com, 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. mail.mydomain.com) 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.

 

Conclusion

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 Support Engineers here at Bobcares fix them.

 

PREVENT YOUR SERVER FROM CRASHING!

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>

SEE SERVER ADMIN PLANS

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

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

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

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF