12

First a few infos:

  • Debian Squeeze
  • PHP 5.3.3
  • PHP with mod_cgi
  • I absolutely HAVE to use mail() in this case. For all my other projects I already use SMTP mailing.

I have isolated the problem of a site timeout to the PHP mail() function. This is the only line of code in a test.php file on my server:

<?php mail('rudolf@geardev.de', 'test', 'test');

Which seems to take endless and is then terminated by mod_cgi after 40 seconds. The command

php -r "mail('rudolf@geardev.de', 'test', 'test');"

on the command line sends the email instantly.

Please tell me which log files you want to see, the Apache log is this:

[Thu Jan 17 12:17:00 2013] [warn] [client 178.15.148.43] mod_fcgid: read data timeout in 40 seconds
[Thu Jan 17 12:17:00 2013] [error] [client 178.15.148.43] Premature end of script headers: test.php

I think the problem is that I accidentally ran chmod -R 775 on the root directory a few days ago. I fixed all errors already, besides this one.

Rudolf
  • 1,856
  • 2
  • 19
  • 32
  • 1
    run `phpinfo()` and see if you have the sendmail extension activated. – Vlad Preda Jan 17 '13 at 11:37
  • 1
    Inspect your `/var/log/mail.log` for errors – s.webbandit Jan 17 '13 at 11:41
  • Is this happening only when you're using mail function? – AL-Kateb Jan 17 '13 at 11:43
  • 1
    Stop using the mail function. You have a low chance of emails sent by it to land in Inbox. Best use a SMTP class (there are dozens out there) to send via a local SMTP account. That will ensure this problem goes away and you email has a grater delivery chance. Make sure you set To and From headers. – transilvlad Jan 17 '13 at 11:49
  • mod_cgi also a not so good choice. Try suphp or mod_php. – transilvlad Jan 17 '13 at 11:50
  • Sendmail is activated. I will, sorry! The mail.log is empty. I know that, but not using mail() is not an option, there are a few scripts on the server that I didn't develop. – Rudolf Jan 17 '13 at 12:11
  • PHP can be configured to use an SMTP server instead of sendmail. – transilvlad Jan 17 '13 at 12:14
  • But wouldn't conflict that with the "From" headers? The SMTP probably accepts only one email address as from, the one from the owner. – Rudolf Jan 17 '13 at 12:15
  • Does the apache user on the server have permissions to send mail? This could explain why running `php -r` as the root user works but php scripts (run as user apache, usually) don't send. You may want to chmod /usr/sbin/sendmail, or whatever setup you use to send mail. – Anton Jan 20 '13 at 18:04
  • As sayed before, it's look a permission issue. Are you running suhosin, apparmor or any other tools to hardenize your webserver? By the way, try execute `` - if works forgot all I sayed. – Ragen Dazs Jan 21 '13 at 01:14
  • It is definitely a permission issue! The question is, what permissions are wrong?! Btw, the ``system()`` script also time out. I use suhosin, but I didn't reconfigure it and it was working before, too. – Rudolf Jan 21 '13 at 15:05
  • This has helped me on many Debian servers: http://serverfault.com/questions/173762/php-mail-function-painfully-slow-on-local-development-machine – Bolli Jan 21 '13 at 17:22
  • @Bolli That's not the problem. It doesn't just take long, it takes *forever*. It ran for an hour already, no success. – Rudolf Jan 21 '13 at 18:05

5 Answers5

7

Most likely you use 2 different php.ini versions. one for cli and one for cgi. Debian have this setup i believe.

  • /etc/php5/cgi/php.ini
  • /etc/php5/cli/php.ini

Make sure that your cli and cgi versions have the same email configuration and it will work.

The mail configuration key is: [mail function]

Another possibility is that your web user does't have access to execute sendmail.

Also check spool permissions (updated)

Danilo Kobold
  • 2,562
  • 1
  • 21
  • 31
  • The sendmail paths are absolutely identical, I triple checked that. – Rudolf Jan 24 '13 at 20:33
  • 1
    What permissions do I have to check to make sure sendmail is allowed to be executed? The /usr/sbin/sendmail binary has permissions 777 (Also checked 755). – Rudolf Jan 24 '13 at 20:34
  • Put this in your sendmail.cf DontBlameSendmail=GroupWritableDirPathSafe restart and try again. Also, when you are sending the email form php.. didi you checked the mail log? if you did, whats the error being displayed? Probably apache dont have right to send mails because its not in the smtp group or something like that. Not being on smtp would remove access from spool directory since u ran a chmod 775... – Danilo Kobold Jan 29 '13 at 14:02
  • 1
    It was the /var/spool/postfix directory that needed rights 777! Thank you very very much! I just started a new bounty and gave it to you. – Rudolf Jan 30 '13 at 18:37
  • I will give it to you tomorrow, I have to wait 24h first. – Rudolf Jan 30 '13 at 18:38
2

Do not rely on mail() as it is unreliable and leads to issues just like these. I've used phpMailer for years quite happily.

If you persist on mail() then check your settings in php.ini (explained at http://www.quackit.com/php/tutorial/php_mail_configuration.cfm). Note that usually CLI has a different php.ini than FastCGI.

Some more common problems:

  • FastCGI doesn't have permissions to use sendmail

  • Memory limit with large attachments

Phil
  • 422
  • 1
  • 5
  • 20
Mirko Adari
  • 5,083
  • 1
  • 15
  • 23
  • As I said already, I absolutely HAVE to use it in this case. For all my other projects I already use SMTP mailing. I don't send any attachment. Mails worked perfectly before my chmod, it shouldn't be the configuration, but I check it. – Rudolf Jan 20 '13 at 17:52
  • The configuration of sendmail is correct, plus it works on the command line with the same settings. – Rudolf Jan 20 '13 at 17:57
  • If it works from the command line, it could be FastCGI or configuration - as on unix boxes you usually have different php.ini for CLI and web. Also check the permissions for FastCGI, does it have access to sendmail? – Mirko Adari Jan 20 '13 at 18:05
  • I checked the sendmail path with a ``phpinfo()``, it is correct. The permissions are the same as on another server of mine that is working perfectly. – Rudolf Jan 20 '13 at 19:03
  • There are more options to check than just path. Execute sendmail from php via system() and see what that prints to stderr or returns. But still, different php.ini files and permissions are the ones worth investigating. – Mirko Adari Jan 20 '13 at 19:48
  • What permissions do I have to check to make sure sendmail is allowed to be executed? The /usr/sbin/sendmail binary has permissions 777 (Also checked 755). – Rudolf Jan 27 '13 at 16:07
0

I've been searching for this particular error and have found that it can be caused by a variety of things. I do not know what you have already tried to do to fix it, but here are some suggestions as to what might help:

  • Make sure to start with <?php and end with ?>
  • Make sure to chmod the file to 755
  • Try to include an output of some sort (eg. print "Mail sent.")

There's also another question about this subject here on SO: PHP Mail Function Causes Error - The owner stated that a change of server with the same setup resulted in a working mail() function, which could indicate a problem with the host.

Community
  • 1
  • 1
Dentych
  • 106
  • 9
  • 1
    I can't just change the server because it is a root server with dozens of sites and about ~1TB data. – Rudolf Jan 27 '13 at 09:39
0

Is it possible that you have a firewall blocking outgoing SMTP connections? Are there other things on that server that can successfully send email?

I would make this a comment, but am not allowed to yet.

Sajan Parikh
  • 4,668
  • 3
  • 25
  • 28
0

Do you have a problem using:

exec('php -r \"mail('rudolf@geardev.de', 'test', 'test');\"');
  • `system`, `exec` all are same. See [this](http://stackoverflow.com/questions/14378175/php-mail-timeout#comment20107655_14378175) comment – Shiplu Mokaddim Jan 27 '13 at 11:04