4

Several days ago, all mail() calls in my custom PHP application cause a 500 Internal Server Error and the only error logged is Premature end of script headers. Running the file via SSH sometimes returns a segfault. The strange thing is no code changed between the time when the mail() call worked, and stopped working (changes are tracked in Git).

Even though no changes were made to the code, I still went through and did the normal checks (.htaccess, php.ini, custom error handler, etc), just to be safe.

I attempted to diagnose the problem by cutting out code in the application until the issue was pinpointed. However, this did not narrow it down to a specific piece of code, but rather the amount of code loaded into PHP (but not yet run) as the time of the mail() call.

In the trimmed application, I was able to get one mail() call to work, but the second would fail. The first call was immediately before an include call (require has same outcome). The second was at the very beginning of the included file (preceded only be the opening PHP tags).

My original thoughts were it was some sort of memory issue. Oddly, the memory usage required to cause the mail() call to throw an error changes based on which code is kept/removed from the overall application. In case it can still help, the memory usage for the current collection of code before including the file is:

Usage: 3113696
Max: 3352560

The memory usage after including the file is still unknown.

I've spoken to my host about the issue, and they claim nothing has changed that would cause this issue.

I have not been able to reproduce this issue in other applications.

Technologies Used

  • PHP 5.3.10 (5.2.17 also affected)
  • MySQLi (specifically custom class extending MySQLi)
  • Suhosin Patch 0.9.10

Error Messages Logged

  • Premature end of script headers: XXX, referer: XXX

TL;DR

What would cause my PHP application to start causing 500 Internal Server Errors when mail() is called after a certain point in the code (but not after any specific call) without any PHP code being changed?

Additional Notes

  • Not a timeout issue - takes less than a second to load
  • Not a memory limit issue - only using ~3MB when it crashes, but I have a 99MB limit
pnuts
  • 58,317
  • 11
  • 87
  • 139
0b10011
  • 18,397
  • 4
  • 65
  • 86
  • This may help: http://stackoverflow.com/questions/6247132/exec-from-php-is-causing-an-premature-end-of-script-headers-php-cgi-exe-error. Are you running windows? – Mike B Mar 15 '12 at 18:11
  • @MikeB, nope, the page loads in less than a second :( (sorry, should have specified in question!) – 0b10011 Mar 15 '12 at 18:13
  • @MikeB, it's MediaTemple's GridService. They don't specify any details about the OS, but here's the result of `uname -a`: `Linux n26 3.2.6mtv9 #1 SMP Tue Feb 28 06:56:22 PST 2012 x86_64 GNU/Linux`. As for opcode cache, I'm unsure--outside of my expertise. How would I go about finding the answer to this? – 0b10011 Mar 15 '12 at 18:26
  • did you check your server's error log? should be `/var/log/httpd/error_log` or something like that, should see lines like `PHP fatal error:` near the end of the file. – JKirchartz Mar 15 '12 at 18:35
  • 1
    It sounds like something's changed on the server. Maybe some memory started going bad, maybe PHP was upgraded. They're telling you nothing's changed that should cause this, but what's changed that shouldn't cause this? It's possible it shouldn't cause this but has. – Crashspeeder Mar 15 '12 at 18:39
  • @JKirchartz, I mentioned the error logged was "Premature end of script headers" (and only that). I also tried extended logs. – 0b10011 Mar 15 '12 at 18:41
  • And commenting out the `mail()` line(s) allows the script to finish successfully? – Brad Mar 15 '12 at 18:50
  • @Brad, yes, commenting out the second `mail()` (or both) allows the script to finish successfully. Commenting out the first `mail()` does nothing. – 0b10011 Mar 15 '12 at 19:39

5 Answers5

2

The problem appears if there are undeliverable emails - this can happen if you send an email to the same domain as you are web hosting. The webserver may be "misconfigured" to think it is mailserver for domain - this is the default setting in my new web host (w00t! or not)

Example: the web host for example.com sends a mail to archive@example.com but because of a misconfiguration(), the web host thinks it needs to deliver locally. This fails and the web server reports the mentioned error 500.

The fix is to configure server as not to act as a mail server for the domain. (My host had an option in the DNS control panel. :s). Test with sending to non-associated domain.

Pascal_dher
  • 448
  • 3
  • 14
1

Sounds like sendmail or whatever mail() is trying to use to send the mail might be dying. Try running the script in the command line and see what output you get.

Tom
  • 11
  • 1
  • In such "mysterious" service denial cases, it's almost always the problem with the MTA (mail transfer agent) or even MSA. I would start by checking your /var/log/maillog. Doesn't appear to be PHP bug. – sector7 Mar 17 '12 at 18:48
  • Tom, "Running the file via SSH [command line] sometimes returns a segfault." In all other cases, it works normally. @sector7, Regrettably, this is shared hosting, so only one error log is available (used by php/apache). Is there a way to change where errors are logged for it without having to rebuild it? – 0b10011 Mar 19 '12 at 13:32
1

Transferring to a different server with similar configuration solved the problem, so it appears that it was, in fact, and issue with the host's server (specific cause is unknown, and regrettably, it will probably remain a mystery).

0b10011
  • 18,397
  • 4
  • 65
  • 86
0

This explicit code may be the reason, this page isn’t working, domain is currently unable to handle this request. HTTP ERROR 500 can be avoided by using the advanced phpmailer class: https://github.com/PHPMailer/PHPMailer

Touheed Khan
  • 2,149
  • 16
  • 26
JWC May
  • 605
  • 8
  • 14
-1

The error must probably have occurred because the php file you used to send the mail was writable by group. You should use the permissions '644' for php.

m4heshd
  • 773
  • 9
  • 23