Fixing slow performance of sending mail from PHP via Sendmail

 
Published on 2012-09-13 by John Collins.

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:

127.0.0.1               localhost localhost.localdomain myserver

You need to also ensure that this is the only line in that file for the 127.0.0.1 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.


Updated 2022 : note that the above post was originally published in 2012, but is left here for archival purposes. The steps above have not been tested recently, so may be outdated.