Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP's mail() function causes a 500 Internal Server Error only after a certain point in the code

Tags:

php

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
like image 626
0b10011 Avatar asked Nov 15 '25 10:11

0b10011


2 Answers

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 [email protected] 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.

like image 122
Pascal_dher Avatar answered Nov 17 '25 07:11

Pascal_dher


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.

like image 22
Tom Avatar answered Nov 17 '25 07:11

Tom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!