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('[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.

1
  • 1
    on my Ubuntu system the command is cat /etc/hostname
    – zkent
    Dec 1, 2014 at 7:29

5 Answers 5

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.

3
  • 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, 2010 at 5: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, 2010 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! Jul 13, 2012 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.

1
  • Perfect! Did this AFTER plaguing me around for two hours. =) Apr 17, 2016 at 23:07
1

This may or may not be a solution for you: add mail.force_extra_parameters = "[email protected]" to your php.ini file. It makes PHP automatically append [email protected] 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?

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.

1
  • 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, 2010 at 5: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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .