21

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('recipient@somewhere.com', 'the subject', 'the message', null, '-fsender@somewhere.com');

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.

Michael B
  • 341
  • 1
  • 2
  • 9

5 Answers5

6

Well I know that this is not what you are asking, but why you don't try Postfix or Exim? They're both available to ubuntu (Postfix is even the default mta on Ubuntu systems) and they both provide a compatible 'sendmail' command that works very well. IMHO sendmail is kind of dated and you will get better chances of support with more modern MTA.

coredump
  • 12,573
  • 2
  • 34
  • 53
  • Even if there are better mail systems than sendmail, the issue is mainting a development server that mirrors the production server. sendmail is the default mail agent for php. – Michael B Aug 24 '10 at 05:12
  • 1
    You mean sendmail the software or sendmail the binary? Because Exim and Postfix also have the `sendmail` binary that accepts the same options as the original sendmail, for compatibility matters. – coredump Aug 24 '10 at 12:22
  • I'm not much of a fan of Exim from past experience, but gave Postfix a try and solved w/e problems we happening w/ Sendmail immediately! – quickshiftin Jul 13 '12 at 15:58
2

This worked for me:

  • Install postfix

See instructions here on how to do this here: https://help.ubuntu.com/community/Postfix (It may already be installed, and the 'sendmail' binary may actually be an alias for postfix)

  • Follow instructions here:

http://lenss.nl/2009/01/making-php-mail-work-on-ubuntu-through-postfix/

mkfifo /var/spool/postfix/public/pickup

Find the sendmail process

ps aux | grep mail

Kill it

kill <thepid>

Restart postfix

/etc/init.d/postfix restart

I think you can just set the 'myorigin' parameter to any active domain name such as a domain name you own.

Steve Robbins
  • 1,904
  • 5
  • 23
  • 26
Michael B
  • 341
  • 1
  • 2
  • 9
1

This may or may not be a solution for you: add mail.force_extra_parameters = "-fsender@somewhere.com" to your php.ini file. It makes PHP automatically append -fsender@somewhere.com as a fifth parameter to PHP's mail() function.

That's a hardcoded value and only applicable in php.ini so it won't be very flexible, but perhaps works for you in your development case?

Janne Pikkarainen
  • 31,454
  • 4
  • 56
  • 78
0

By watching the network traffic (think tcpdump) or by running an strace on your sendmail or apache processes, you should be able to get an idea of why that delay is there so that you can fix the root problem.

Usually delays in that range are due to failing DNS lookups, but you won't know till you look. Unless you fix the underlying problem, it will probably end up being an issue no matter how you try to work around it.

tylerl
  • 14,885
  • 7
  • 49
  • 71
  • 1
    The delay was being caused by the hostname being a single word rather than a FQDN (fully qualified domain name). By pointing sendmail to /etc/hosts and having localhost.domain as the first domain name (which is a FQDN) there is no delay. This was fixed in steps 1-4. The remaining issue is having to use '-f' - still a major improvement on having to wait 2 minutes to send mail. – Michael B Aug 24 '10 at 05:15
0

I have never come across a problem using mail() in PHP that wasn't a result of a problem on the MTA.

In your php.ini file it will show what command it runs to send an email (default: "sendmail -t -i"). Try sending an email from the command line using this - I'll bet its slow.

Usual things to check are whether a DNS server is configured correctly on the machine (and can resolve all the addresses configured in the MTA) and that if a smart relay is configured then the name is resolvable to an address the system can connect to.

symcbean
  • 19,931
  • 1
  • 29
  • 49