php mail() function painfully slow on local development machine
- by Michael B
Background: If you have set up a local apache server for development purposes you may have run into the problem where sendmail takes a long time (at least one minute) to send emails. This is extremely frustrating if you are trying to debug a problem with an email you have generated.
There are several forum posts on the internet that discuss this problem. However, none of theme described what to do in enough detail for my limited knowledge. Here are the steps that worked for me:
1) find your hostname (in case you've forgotten it) using this command:
:~$ cat /hosts/hostname
myhostname
2) edit the file /etc/hosts and make sure the first line is the following:
127.0.0.1 localhost.localdomain localhost myhostname
3) edit the sendmail configuration file ( /etc/mail/sendmail.cf in Ubuntu) and
Uncomment the line #O HostsFile=/etc/hosts
4) Restart the computer. The computer should boot up much faster now and the mail() function should return almost immediately. HOWEVER, the emails won't actually be sent unless you follow step 5.
5) You must new use the sendmail '-f' option whenever using the mail function. For example:
mail('[email protected]', 'the subject', 'the message', null, '[email protected]');
My question for my fellow serverfaulters is:
What further changes can be made so that I don't have to use the sendmail -f option? Although it's not very hard to add the -f option, it is a problem when your CMS (such as Drupal) does not use the -f option when sending mail. You would need to hack a core module to add this option.