PHP Contact Form returning error upon submission

329 views Asked by At

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="[email protected]";
 $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!

2

There are 2 answers

0
SuperDJ On BEST 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

0
Maltronic On

The problem appears to be the header location redirect call which should provide a full url, e.g.:

header("Location: http://www.example.com");

Relative urls are processed as per the browser's best efforts and your browser clearly isn't liking the ../, hence the "bad header" complaint.

This is noted on the php.net page as per the official URL spec.