Jump to content

500 Internal Server Error for enquiry form


joharm

Recommended Posts

Hi everyone, I am not a PHP expert in any way but I do dabble a bit with enquiry forms.

 

One form, setup to simply email data from the form to an email address is giving me an Internal Server Error 500.

 

PHP Version 5.2.6. Client is using Fasthosts Windows Business hosting package, I assume IIS6.

 

I simply want to know whether this is coming from a script error my end, or from Fasthosts. I have trawled the internet all morning and tried out different sections of my code.

 

I believe it is coming from when the variables are picked up with $_POST - if they are empty (which they can be) I get the 500 error.

 

<?php
// Strip tags from the email

$firstname = trim(strip_tags($_POST['firstname']));
$lastname = trim(strip_tags($_POST['lastname']));
$jobtitle = trim(strip_tags($_POST['jobtitle']));
$status = trim(strip_tags($_POST['status']));
$company = trim(strip_tags($_POST['company']));
$tel = trim(strip_tags($_POST['tel']));
$mobile = trim(strip_tags($_POST['mobile']));
$email = trim(strip_tags($_POST['email']));
$address1 = trim(strip_tags($_POST['address1']));
$address2 = trim(strip_tags($_POST['address2']));
$city = trim(strip_tags($_POST['city']));
$postcode = trim(strip_tags($_POST['postcode']));
$appcompany = trim(strip_tags($_POST['appcompany']));
$companyreg = trim(strip_tags($_POST['companyreg']));
$funding = trim(strip_tags($_POST['funding']));
$purpose = trim(strip_tags($_POST['purpose']));
$employees = trim(strip_tags($_POST['employees']));
$hear = trim(strip_tags($_POST['hear']));
$overview = trim(strip_tags($_POST['overview']));
$newsletter = trim(strip_tags($_POST['newsletter']));
$terms = trim(strip_tags($_POST['terms']));

// Check that required fields are not empty

if (($firstname == "") || ($company == "") || ($lastname == "") || ($email == "") || ($tel == "") || ($terms == "")) {

$error = "ERROR: You have not filled out all of the fields. Please try again and make sure you have ticked the Terms and Conditions box";

$overview = ereg_replace("[\n\r]", " ", $overview);
$purpose = ereg_replace("[\n\r]", " ", $purpose);

// If empty, go back to enquiry page and send information back, along with custom error.	

header( "Location:enquire-now.php?error=$error&firstname=$firstname&lastname=$lastname&jobtitle=$jobtitle&status=$status&company=$company&tel=$tel&email=$email&mobile=$mobile&address1=$address1&address2=$address2&city=$city&postcode=$postcode&appcompany=$appcompany&companyreg=$companyreg&funding=$funding&purpose=$purpose&overview=$overview" );

}


// Run the function to see if the email is valid

function valid_email($email) { 
  // First, we check that there's one @ symbol, and that the lengths are right 
  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) { 
    // Email invalid because wrong number of characters in one section, or wrong number of @ symbols. 
    return false; 
  } 
  // Split it into sections to make life easier 
  $email_array = explode("@", $email); 
  $local_array = explode(".", $email_array[0]); 
  for ($i = 0; $i < sizeof($local_array); $i++) { 
     if (!ereg("^(([A-Za-z0-9!#$%&#038;'*+/=?^_`{|}~-][A-Za-z0-9!#$%&#038;'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) { 
      return false; 
    } 
  }   
  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name 
    $domain_array = explode(".", $email_array[1]); 
    if (sizeof($domain_array) < 2) { 
        return false; // Not enough parts to domain 
    } 
    for ($i = 0; $i < sizeof($domain_array); $i++) { 
      if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) { 
        return false; 
      } 
    } 
  } 
  return true; 
}

// If email not valid, go back to contact page and send information back, along with custom error	

if (valid_email($email) == false) {

$error = "ERROR: That email address is not valid. Please try again";
header( "Location:enquire-now.php?error=$error&firstname=$firstname&lastname=$lastname&jobtitle=$jobtitle&status=$status&company=$company&tel=$tel&email=$email&mobile=$mobile&address1=$address1&address2=$address2&city=$city&postcode=$postcode&appcompany=$appcompany&companyreg=$companyreg&funding=$funding&purpose=$purpose&overview=$overview" );

}



// Setup the email to be sent out

if (($firstname != "") && ($lastname != "") && ($company != "") && ($tel != "") && ($email !="") && (valid_email($email) == true)) {


$to = "my email"; //Will be a proper email address
  	$subject = "New Enquiry from website";
  	$body = "<table cellpadding=\"30\" cellspacing=\"0\" width=\"500\" style=\"padding: 30px; border: solid 1px #cccccc;\"><tr><td align=\"left\" style=\"font-family: Arial, Helvetica, sans-serif; font-size: 12px; color: #333333\"><h1 style=\"font-size: 18px; font-weight: normal; color: #666666\" align=\"center\">New Enquiry from the website</h1><hr /><p>First Name: <strong>$firstname</strong>      Last Name: <strong>$lastname</strong></p><p>Job Title: <strong>$jobtitle</strong>      Status: <strong>$status</strong>      Company: <strong>$company</strong></p><p>Office Telephone: <strong>$tel</strong>      Mobile Telephone: <strong>$mobile</strong><br />    Email: <strong>$email</strong><br />Address: <strong>$address 1, $address 2, $city</strong>      Postcode: <strong>$postcode</strong></p><p>Name of Applicant Company: <strong>$appcompany</strong><br />Company Reg. No: <strong>$companyreg</strong></p><p>Amount of Funding Required: <strong>$funding</strong><br />Purpose of Funding: <strong>$purpose</strong><p>Number of UK based employees: <strong>$employees</strong></p><p>Business Overview: <strong>$overview</strong></p><p>Where did you hear of us?: <strong>$hear</strong></p><p>Newsletter subscribe?:<strong>$newsletter</strong></p></td></tr></table>";
  
	 $headers = "MIME-Version: 1.0\r\n";
  	$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

  	$headers .= "From:website <my email>" . "\r\n";
	 $headers .= "Reply-To: $email <$email>" . "\r\n";

ini_set("sendmail_from", $email);

$sent = mail($to, $subject, $body, $headers, "-f" .$email); 

	// Setup confirm message to display below if email is sent out

if ($sent) {
	$confirm = "Success";
} else {
	$confirm= "Error"; 
}


}


?>

 

 

Any help at all would be appreciated!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.