Internal Server Error - Contact Forms???

svviift

New Member
Dec 6, 2015
17
6
Hello DevBest,

I've been editing sites and contact forms for a long time now, but it wasn't until recently where I've gained a client and was asked to put in a form, and have had major problems from day one. We're due course to launch now and I'm not quite sure what I'm doing wrong.

The layout I was working from (they wanted me to use a template online because they liked it and wanted to speed up design and development) didn't have a contact form built into it per se, but did have the elements of it.

Anyway, I googled a simple form and pasted the code, as follows:

<form action="mail.php" method="POST">
Name <input type="text" name="name">
E-Mail<input type="text" name="email">
Message<textarea name="message" rows="6" cols="25"></textarea><br />
<input type="submit" value="Send"><input type="reset" value="Clear">
</form>

<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = ‘[email protected]';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message. We will contact you shortly.');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to [email protected]');
window.location = 'index.html';
</script>
<?php
}

I'm useless at php, so all of the PHP bit goes over my head, I've obviously created a mail.php form with the php code and put it in it, as have I got an index.html but I constantly get:

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at webmaster@[mysite].co.uk to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

Where do I go from here? What should I be looking at? I'm days off launching this and have only just come across the problem, the clients pushy to get live (as they always are) and I don't fancy delaying this much further as I had to push back because of uni stuff.

Any suggestions?
 

GarettM

Posting Freak
Aug 5, 2010
833
136
Hmm without seeing more source code it is kinda hard to figure out what the real issue for the 500 Internal Server Error is. The only thing that catches my eye is the line
PHP:
$mail_status = mail($mail_to, $subject, $body_message, $headers);
Also everything else about this script is wrong...

For the HTML you have the fallowing
Code:
<form action="mail.php" method="POST">
   Name <input type="text" name="name">
   E-Mail<input type="text" name="email">
   Message<textarea name="message" rows="6" cols="25"></textarea><br />
   <input type="submit" value="Send"><input type="reset" value="Clear">
</form>
That gives you the fallowing GET data
Code:
Array(
   'name' => '',
   'email' => '',
   'message' => '',
   'submit' => true,
)

But instead your using different GET key names like 'cf_name', 'cf_email', 'cf_*', ...etc, so maybe in a weird way the error is from out of bound array error?

Another thing that comes to mind is when PHP errors are turned off sometimes it is possible to set it up where if there is an error just show a 500 error kinda like what IIS does.

Been awhile but try this see if it works better
PHP:
<?php
 /**
  * Simple Send Mail Script
  */

/**
 * Filter Data.
 */
function filter($input, $filter = FILTER_DEFAULT)
{
    return filter_var($key, $filter);
}
/**
 * Clean Input Information.
 */
function clean($input)
{
    return stripslashes(strip_tags(htmlspecialchars($input, ENT_IGNORE, 'utf-8')));
}

$field_name     = filter($_POST['name'])  ? clean($_POST['name']) : null;
$field_email    = filter($_POST['email'], FILTER_VALIDATE_EMAIL) ? clean($_POST['email']) : null;
$field_message  = filter($_POST['message']) ? clean($_POST['message']) ? null;

$mail_to        = "[email protected]";
$subject        = "Message from visitor";

$body_message   = sprintf("From: %s\nE-Mail: %s\nMessage: %s\n", $field_name, $field_email, $field_message);
$headers        = sprintf("From: %1\nReply-To: %1\n", $field_email);

$mail_status    = !is_null($field_name) && !is_null($field_email) && !is_null($field_message) ? mail($mail_to, $subject, $body_message, $headers) : false;

echo '<script language="javascript" type="text/javascript">', (($mail_status) ? 'alert("Thank you for your message.");' : 'alert("Message Failed");'), 'window.location = "index.php";</script>';
exit;
 
Hmm without seeing more source code it is kinda hard to figure out what the real issue for the 500 Internal Server Error is. The only thing that catches my eye is the line
PHP:
$mail_status = mail($mail_to, $subject, $body_message, $headers);
Also everything else about this script is wrong...

For the HTML you have the fallowing
Code:
<form action="mail.php" method="POST">
   Name <input type="text" name="name">
   E-Mail<input type="text" name="email">
   Message<textarea name="message" rows="6" cols="25"></textarea><br />
   <input type="submit" value="Send"><input type="reset" value="Clear">
</form>
That gives you the fallowing GET data
Code:
Array(
   'name' => '',
   'email' => '',
   'message' => '',
   'submit' => true,
)

But instead your using different GET key names like 'cf_name', 'cf_email', 'cf_*', ...etc, so maybe in a weird way the error is from out of bound array error?

Another thing that comes to mind is when PHP errors are turned off sometimes it is possible to set it up where if there is an error just show a 500 error kinda like what IIS does.

Been awhile but try this see if it works better
PHP:
<?php
 /**
  * Simple Send Mail Script
  */

/**
 * Filter Data.
 */
function filter($input, $filter = FILTER_DEFAULT)
{
    return filter_var($key, $filter);
}
/**
 * Clean Input Information.
 */
function clean($input)
{
    return stripslashes(strip_tags(htmlspecialchars($input, ENT_IGNORE, 'utf-8')));
}

$field_name     = filter($_POST['name'])  ? clean($_POST['name']) : null;
$field_email    = filter($_POST['email'], FILTER_VALIDATE_EMAIL) ? clean($_POST['email']) : null;
$field_message  = filter($_POST['message']) ? clean($_POST['message']) ? null;

$mail_to        = "[email protected]";
$subject        = "Message from visitor";

$body_message   = sprintf("From: %s\nE-Mail: %s\nMessage: %s\n", $field_name, $field_email, $field_message);
$headers        = sprintf("From: %1\nReply-To: %1\n", $field_email);

$mail_status    = !is_null($field_name) && !is_null($field_email) && !is_null($field_message) ? mail($mail_to, $subject, $body_message, $headers) : false;

echo '<script language="javascript" type="text/javascript">', (($mail_status) ? 'alert("Thank you for your message.");' : 'alert("Message Failed");'), 'window.location = "index.php";</script>';
exit;
 

svviift

New Member
Dec 6, 2015
17
6
Thanks for your response, I'll give this a go. Would providing the link so you can show source be good?

The template didnt come with a contact form, but did have the elements to create one (appropriate buttons and what not) so maybe you're right with the out of bound array bit.



Hmm without seeing more source code it is kinda hard to figure out what the real issue for the 500 Internal Server Error is. The only thing that catches my eye is the line
PHP:
$mail_status = mail($mail_to, $subject, $body_message, $headers);
Also everything else about this script is wrong...

For the HTML you have the fallowing
Code:
<form action="mail.php" method="POST">
   Name <input type="text" name="name">
   E-Mail<input type="text" name="email">
   Message<textarea name="message" rows="6" cols="25"></textarea><br />
   <input type="submit" value="Send"><input type="reset" value="Clear">
</form>
That gives you the fallowing GET data
Code:
Array(
   'name' => '',
   'email' => '',
   'message' => '',
   'submit' => true,
)

But instead your using different GET key names like 'cf_name', 'cf_email', 'cf_*', ...etc, so maybe in a weird way the error is from out of bound array error?

Another thing that comes to mind is when PHP errors are turned off sometimes it is possible to set it up where if there is an error just show a 500 error kinda like what IIS does.

Been awhile but try this see if it works better
PHP:
<?php
/**
  * Simple Send Mail Script
  */

/**
* Filter Data.
*/
function filter($input, $filter = FILTER_DEFAULT)
{
    return filter_var($key, $filter);
}
/**
* Clean Input Information.
*/
function clean($input)
{
    return stripslashes(strip_tags(htmlspecialchars($input, ENT_IGNORE, 'utf-8')));
}

$field_name     = filter($_POST['name'])  ? clean($_POST['name']) : null;
$field_email    = filter($_POST['email'], FILTER_VALIDATE_EMAIL) ? clean($_POST['email']) : null;
$field_message  = filter($_POST['message']) ? clean($_POST['message']) ? null;

$mail_to        = "[email protected]";
$subject        = "Message from visitor";

$body_message   = sprintf("From: %s\nE-Mail: %s\nMessage: %s\n", $field_name, $field_email, $field_message);
$headers        = sprintf("From: %1\nReply-To: %1\n", $field_email);

$mail_status    = !is_null($field_name) && !is_null($field_email) && !is_null($field_message) ? mail($mail_to, $subject, $body_message, $headers) : false;

echo '<script language="javascript" type="text/javascript">', (($mail_status) ? 'alert("Thank you for your message.");' : 'alert("Message Failed");'), 'window.location = "index.php";</script>';
exit;
 
Hmm without seeing more source code it is kinda hard to figure out what the real issue for the 500 Internal Server Error is. The only thing that catches my eye is the line
PHP:
$mail_status = mail($mail_to, $subject, $body_message, $headers);
Also everything else about this script is wrong...

For the HTML you have the fallowing
Code:
<form action="mail.php" method="POST">
   Name <input type="text" name="name">
   E-Mail<input type="text" name="email">
   Message<textarea name="message" rows="6" cols="25"></textarea><br />
   <input type="submit" value="Send"><input type="reset" value="Clear">
</form>
That gives you the fallowing GET data
Code:
Array(
   'name' => '',
   'email' => '',
   'message' => '',
   'submit' => true,
)

But instead your using different GET key names like 'cf_name', 'cf_email', 'cf_*', ...etc, so maybe in a weird way the error is from out of bound array error?

Another thing that comes to mind is when PHP errors are turned off sometimes it is possible to set it up where if there is an error just show a 500 error kinda like what IIS does.

Been awhile but try this see if it works better
PHP:
<?php
/**
  * Simple Send Mail Script
  */

/**
* Filter Data.
*/
function filter($input, $filter = FILTER_DEFAULT)
{
    return filter_var($key, $filter);
}
/**
* Clean Input Information.
*/
function clean($input)
{
    return stripslashes(strip_tags(htmlspecialchars($input, ENT_IGNORE, 'utf-8')));
}

$field_name     = filter($_POST['name'])  ? clean($_POST['name']) : null;
$field_email    = filter($_POST['email'], FILTER_VALIDATE_EMAIL) ? clean($_POST['email']) : null;
$field_message  = filter($_POST['message']) ? clean($_POST['message']) ? null;

$mail_to        = "[email protected]";
$subject        = "Message from visitor";

$body_message   = sprintf("From: %s\nE-Mail: %s\nMessage: %s\n", $field_name, $field_email, $field_message);
$headers        = sprintf("From: %1\nReply-To: %1\n", $field_email);

$mail_status    = !is_null($field_name) && !is_null($field_email) && !is_null($field_message) ? mail($mail_to, $subject, $body_message, $headers) : false;

echo '<script language="javascript" type="text/javascript">', (($mail_status) ? 'alert("Thank you for your message.");' : 'alert("Message Failed");'), 'window.location = "index.php";</script>';
exit;
 

Ecko

23:37 [autobots] -!- eckostylez [[email protected]]
Nov 25, 2012
1,397
961
But instead your using different GET key names like 'cf_name', 'cf_email', 'cf_*', ...etc, so maybe in a weird way the error is from out of bound array error?

Another thing that comes to mind is when PHP errors are turned off sometimes it is possible to set it up where if there is an error just show a 500 error kinda like what IIS does.
That shouldn't throw a 500 error.

Check if an error_log file has been created. If you can, tail -f /usr/local/apache/logs/error_log
 

GarettM

Posting Freak
Aug 5, 2010
833
136
Thanks for your response, I'll give this a go. Would providing the link so you can show source be good?

The template didnt come with a contact form, but did have the elements to create one (appropriate buttons and what not) so maybe you're right with the out of bound array bit.
What platform is this running on?
Linux+Apache2+PHP5+MySQL?
NT+IIS7+PHP5+MySQL?

@eckostylez is right the php code shouldn't produce a 500 error, the only thing I can think of that would produce that error is
  1. Runtime error, not having the right modules enabled? Or if on NT not having the visual studio runtime library
  2. Htaccess or web.config error
Please post log information so we can diagnose what is wrong right know we have no good information to work with.
 

svviift

New Member
Dec 6, 2015
17
6
Site is live here whilst I test it:


and see below the error it logs, I thought originally it was my perms, but I asked what my mate uses (as its his server) and I also have the same settings set for other sites I host or have on my account with mailing forms that work perfectly fine!

Thu Jul 07 08:35:05.583039 2016] [:error] [pid 14553] [client 82.26.70.23:51212] SoftException in Application.cpp:256: File "/home/rjngraph/public_html/WIP/NABCEL/site/mail.php" is writeable by group, referer:
 

Ecko

23:37 [autobots] -!- eckostylez [[email protected]]
Nov 25, 2012
1,397
961
Site is live here whilst I test it:


and see below the error it logs, I thought originally it was my perms, but I asked what my mate uses (as its his server) and I also have the same settings set for other sites I host or have on my account with mailing forms that work perfectly fine!

Thu Jul 07 08:35:05.583039 2016] [:error] [pid 14553] [client 82.26.70.23:51212] SoftException in Application.cpp:256: File "/home/rjngraph/public_html/WIP/NABCEL/site/mail.php" is writeable by group, referer:
SoftException in Application.cpp:256: File "/home/rjngraph/public_html/WIP/NABCEL/site/mail.php" is writeable by group

That is a permissions error. Permissions should be 0644 for suPHP and can vary when using DSO/CGI
 

svviift

New Member
Dec 6, 2015
17
6
SoftException in Application.cpp:256: File "/home/rjngraph/public_html/WIP/NABCEL/site/mail.php" is writeable by group

That is a permissions error. Permissions should be 0644 for suPHP and can vary when using DSO/CGI
Righto, I'll change the perms of mail.php then yeah?
 
After changing mail.php to 0644, I'm thrown these error logs.

[Thu Jul 07 15:31:27.770553 2016] [:error] [pid 2415] [client 82.26.70.23:53083] SoftException in Application.cpp:603: Directory "/home/rjngraph/public_html/WIP/NABCEL/site" is writeable by group
[Thu Jul 07 15:31:26.743245 2016] [authz_core:error] [pid 2415] [client 82.26.70.23:53083] AH01630: client denied by server configuration: /home/rjngraph/public_html/WIP/NABCEL/.htaccess
[Thu Jul 07 15:31:25.286700 2016] [authz_core:error] [pid 2415] [client 82.26.70.23:53083] AH01630: client denied by server configuration: /home/rjngraph/public_html/WIP/.htaccess

Also, to add to this, for some reason, the files permissions in my /WIP folder is a mess, I'll list the folders and file names with their perms, bold will be a folder, italics a file within it!

WIP/... [0755]
NABCEL/... [0777]
site/... [0777]
index.html [0777]
mail.php [0644] (changed due to you guys' responses, was also 0777​
 

Weasel

👄 I'd intercept me
Nov 25, 2011
4,135
2,461
Can't see any PHP errors either, so try @eckostylez his solution. On another note, the way you are doing this is really user unfriendly. Instead of actually posting it with the form, redirecting to mail.php and then back to index.html is not user-friendly at all. Take a look into jQuery's $.post function, and return a JSON response from mail.php which you can handle with JavaScript to show the mail.php's response. This prevents redirecting by the form, and sending you back to the top of the page.
 

svviift

New Member
Dec 6, 2015
17
6
Can't see any PHP errors either, so try @eckostylez his solution. On another note, the way you are doing this is really user unfriendly. Instead of actually posting it with the form, redirecting to mail.php and then back to index.html is not user-friendly at all. Take a look into jQuery's $.post function, and return a JSON response from mail.php which you can handle with JavaScript to show the mail.php's response. This prevents redirecting by the form, and sending you back to the top of the page.

Thanks for your reply Wess, I know nothing about JQuery and JavaScript but I will take a look. Going to have to try the code suggestion as you said.
 
Thanks all for the help. The problems now resolved, it was simply a problem with the permissions. They were all a mess, after changing all folders to 0755 and files to 0644, I've finally been able to send a message without the 500 error.

Appreciated!
 

Users who are viewing this thread

Top