March 11, 2012

PHP mail() error 500

Question by Chevi

As part of the registration process on a website, I have added mail confirmation. But for some reason, the mail function throws an Internal server error in there.

The strange thing is that if I create a test script, with the exact same email (all the same parameters) it works fine, and sends the email.

The mail is sent from a function in a Class, in case that helps. I didn’t post the code because it isn’t really relevant, even if I try mail('','subject','email'); it fails with the 500 error!

The server error logs don’t show anything at all, anyone knows what may cause such a problem?


The server is running php through mod_fastcgi, although this problem also happens if I switch to mod_suphp.


I’ll try to explain this better, the mail function works perfectly if called from another file, with the same parameters. The problem here is something that combined with the mail function causes an error 500. The rest of the file where it’s called is fine too, if I comment the mail function everything works. The way it gets called is an AJAX request to a file that calls a function where the mail is sent (Just in case this helps)

In response to answers so far, here is more information I did not share previously:
OS: CentOS release 5.8
When I say error 500, I mean that the server returns only an HTTP 500 status code.
The server does not show anything in any error log
The most important thing is that if I create a file called test.php, with only mail('','Subject','Message'), it works just fine. When called from this other file, 500 status code returned.
What I am asking is if anyone knows, probably from experience, what could be causing this.

Someone had the same problem yesterday: PHP's mail() function causes a 500 Internal Server Error only after a certain point in the code

After some testing, I have discovered that the 500 stats code is only returned when the script is called via AJAX. If I create a file called test.php, and I simply place the mail function and test it, it works. Calling it via AJAX doesn’t, any ideas?

Answer by Chevi

After some more hours of testing I have found the problem!
I was using window.location to redirect the user to a new page after the AJAX call was completed, in it’s callback function.
Apparently if you modify it after an AJAX call to a php script that uses the mail() function, the server returns a 500 status code in the request

Answer by Starx

With the amount of input you have provided, it is very hard to tell, how that error occured.

Error: 500 are Interval Server Errors and can have more than one reasons for occuring.

  • A malformed php cgi script
  • An invalid directive in an .htaccess or other config file
  • Limitation imposed by file system and server software.
    May be you are attachcing a file to be to be sent
  • Missing Line Breaks (rn) in the headers

Try every solution listed from this cPanel Forums

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+


Please fill the form - I will response as fast as I can!