Fixing slow performance of sending mail from PHP via Sendmail

On some servers the Sendmail program can be quite slow at sending emails due to hostname resolution. In this article, we will explore one possible solution.

Looking for a new simple way to plan your day? Get organized with Five.Today

Recently I have had two separate test servers that were very slow sending out emails from PHP scripts via the mail() function, where PHP was configured to use a local Sendmail binary to send the emails. The scripts were hanging for roughly one minute while waiting for Sendmail to work, which was severely impacting testing efforts.

When I run a test call to mail() and tail the maillog file, I see the following entries:

$ tail -f /var/log/maillog
Sep 13 11:30:27 myserver sendmail[29118]: My unqualified host name (myserver) unknown; sleeping for retry
Sep 13 11:31:27 myserver sendmail[29118]: unable to qualify my own domain name (myserver) -- using short name
Sep 13 11:31:27 myserver sendmail[29118]: My unqualified host name (myserver) unknown; sleeping for retry
Sep 13 11:31:35 myserver sendmail[29135]: My unqualified host name (myserver) unknown; sleeping for retry

So it seems that this is a local domain name resolution issue. The trick here is to ensure that you have an entry in your /etc/hosts file for your unqualified hostname, that is the short version returned by the hostname command, which points to the local loop-back IP rather than it's external IP like so:               localhost localhost.localdomain myserver

You need to also ensure that this is the only line in that file for the loop-back IP, as Sendmail will only look at a single line.

Once this change is made, I retested and confirmed that emails are now being sent out within milliseconds.

Looking for a modern PHP framework to build your new project? Please consider using the Alpha Framework

John Collins

I have been writing about web technology and software development since 2001. I am the developer of the Alpha Framework for PHP, and the Five.Today personal productivity app. I love open source, technology, and economics.