php mail() function painfully slow on local development machine

20,799

Solution 1

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.

Solution 2

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.

Solution 3

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?

Share:
20,799

Related videos on Youtube

Michael B
Author by

Michael B

Updated on September 17, 2022

Comments

  • Michael B
    Michael B over 1 year

    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.

    • zkent
      zkent over 9 years
      on my Ubuntu system the command is cat /etc/hostname
  • Michael B
    Michael B over 13 years
    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
    Michael B over 13 years
    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.
  • coredump
    coredump over 13 years
    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.
  • Hannes Schneidermayer
    Hannes Schneidermayer about 8 years
    Perfect! Did this AFTER plaguing me around for two hours. =)