Monday, 31 March 2014

Fixing slow performance of sending mail from PHP via Sendmail

Source: http://www.alphadevx.com/a/372-Fixing-slow-performance-of-sending-mail-from-PHP-via-Sendmail


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:
 1 
 2 
 3 
 4 
 5 
$ 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:
 1 
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.

No comments: