Fix your painfully slow PHP mail() function.
Having moved all of my sites to the Excellent Digital Ocean hosting I noticed that on some sites the php mail function seemed to be painfully slow and in some cases threw up some error messages telling me that it had failed. Googling didn't turn up too much in the way of solutions, most information seemed to suggest that the sendmail or postfix packages be installed instead.
Whilst sendmail and postfix are both great solutions, I did not really want to install a fully fledged mail server on my box as all email handling for my domains is handled on a separate server. It seemed like too much of a waste of resources so I decided to simply fix the issue with the php mail() function.
After doing a bit of digging I discovered that the issue lay with the hosts file. The host info was incorrect.
Editing the hosts file is pretty easy to do, but first you will need to know what the current hostname is set to. To do this simply type in the following command at the command prompt
hostname
This will return the hostname for your server, in my case it was marzipan
Next edit your hosts file. On Ubuntu this is can be found at /etc/hosts. To edit the hosts file invoke your favourite editor, for example
sudo nano /etc/hosts
What I found in my hosts file was that the fully qualified local host name was missing. Instead of looking like this
127.0.0.1 localhost localhost.localdomain marzipan
It looked like this
127.0.0.1 localhost marzipan
I simply edited the file, adding the localhost.localdomain reference and then saved the file. (obviously)
To ensure that the change was picked up I restarted apache, on my server I use the following command
sudo service apache2 restart
That's it! My email is now fixed and my websites can happily and easily send email messages without issue. There's no delays and my php code doesn't hang when trying to call the php mail() function. A pretty easy fix for a rather annoying issue.
/DM