2

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('email@email.com','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?

Technology:

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

Updates:

UPDATE: 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)

UPDATE 2: 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('address@domain.com','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.

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

UPDATE 4: 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?

Community
  • 1
  • 1
aurbano
  • 3,324
  • 1
  • 25
  • 39
  • enable PHP debugging and post error message then. see: http://thinkvitamin.com/code/how-to-debug-in-php/ – Marek Sebera Mar 11 '12 at 20:53
  • With PHP error reporting set to all, it still gives error 500. I already have my own debugging system installed, but the mail function seems to "override" everything, and just goes to error 500. – aurbano Mar 11 '12 at 21:01
  • no way to get no logs if your code fails and Apache gives you HTTP status. It must be in apache, virtual host or at least php framework logs. Maybe your functions are called with `error-output suppress` feature `@`, see http://stackoverflow.com/questions/7149030/php-return-500-error-but-no-error-log – Marek Sebera Mar 11 '12 at 21:21
  • The mail function was not called with `@`, although I have tried it with it, only to find it still gives error 500. I know its the mail function because I commented line by line testing where was the error coming from. The strange thing is that the exact same mail function, with the same parameters when called from another file works fine.... – aurbano Mar 11 '12 at 22:51
  • Are you sure it is the mail function causing the error and not something else. eg if you comment out the call to mail() function does the script return as expected? – bumperbox Mar 11 '12 at 23:18
  • Yes, the mail function is the one causing it. Even if I wrap it in another function like so `function sendmail($to,$subject,$msg){ mail($to,$subject,$message); }` the server returns an internal server error – aurbano Mar 12 '12 at 08:44

3 Answers3

1

Your question is very confused.

You've not said what operating system you are running on, nor provided details of the configuration: on MSWindows php's mail function acts as an SMTP client. On POSIX OS's (including Linux) it executes a command program to send the email. The SMTP client needs to know the server and port to connect to. The POSIX function needs the config to tell it which program to run.

You keep refering to a 500 error - do you mean an HTTP 500 status code at the browser?

What does the log for the mail program / server show? If this is a posix platform, try changing the php.ini to run a shell script to log actions and parameters.

symcbean
  • 47,736
  • 6
  • 59
  • 94
  • I've added all the information I could about the problem. I asked in hope someone had encountered something similar sometime, where the mail function would cause an internal server error on one script, while working fine on another. – aurbano Mar 12 '12 at 08:43
  • You've not provided the relevant php.ini settings (PHP seems to be having a problem running the mail program - please check it's where it is supposed to be and that you can run it from the command line) – symcbean Mar 12 '12 at 10:02
  • The problem is not with the mailer, as I stated in the question, the same mail function with the same parameters works as expected elsewhere – aurbano Mar 17 '12 at 12:27
1

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 (\r\n) in the headers

Try every solution listed from this cPanel Forums

Starx
  • 77,474
  • 47
  • 185
  • 261
0

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

aurbano
  • 3,324
  • 1
  • 25
  • 39