Melvy Melvy - 5 months ago 19
HTML Question

PHP Contact Form returning error upon submission

So I've tried publishing a page with PHP contact form on 2 different servers and I can't get either of them to work perfectly.

Server 1:
Returns a "500 internal error" page upon submission.
I contacted web support and they replied saying I have a:

"malformed header from script 'index.php': Bad header:"

My code for index.php as follows:

<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$query = $_POST['message'];
$email_from = $name.'<'.$email.'>';

$to="testemail@email.com";
$subject="test subject";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ".$email_from."\r\n";
$message="

Name:
$name
<br>
Email:
$email
<br>
Message:
$query

";
if(mail($to,$subject,$message,$headers))
header("Location:../contact.php?msg=Successful Submission!<br />
Thank you for contacting us.");
else
header("Location:../contact.php?msg=Error To send Email !");
}
?>


Server 2:
Links me to my "Thank you page" correctly, but it's showing
"Error To send Email !" instead of
"Successful Submission!"

Any help or advice will be greatly appreciated!

Answer

I think the propblem is in this part:

 if(mail($to,$subject,$message,$headers))
        header("Location:../contact.php?msg=Successful Submission!<br />
 Thank you for contacting us.");
    else
        header("Location:../contact.php?msg=Error To send Email !");

Specificly in the header();

  1. each space would be converted to %20 as there are no URLs with spaces;
  2. There are no special characters like <, >, ! in URLs
  3. The "/" in <br /> would suggest a file called ">Thank you for contacting us."

Check out: http://www.w3schools.com/tags/ref_urlencode.asp;

Also put exit(); after each header to prevent futher execution of the script