-3

I'm trying to have my contact form redirect aftering it gets mailed. But I'm running into the following error:

Warning: Cannot modify header information - headers already sent by (output started at /home3/ettrick/public_html/test3/mail.php:12) in /home3/ettrick/public_html/test3/mail.php on line 80

<?php 
$email_message .= "Name: ".($name)."\n";

$email_message .= "Email: ".($email)."\n";

$email_message .= "Message: ".($message)."\n";
mail($ToEmail, $EmailSubject, $email_message, $mailheader); 
?>
<?php

if(isset($_POST['email'])) {



    // EDIT THE 2 LINES BELOW AS REQUIRED

    $email_from = "newsletter@kingdomhearts7.com";

    $email_to = "fritzgattereau@gmail.com";

    $email_subject = "Contact Form";


    $email_message = "Form details below.\n\n";



    function clean_string($string) {

      $bad = array("content-type","bcc:","to:","cc:","href");

      return str_replace($bad,"",$string);

    }



    $email_message .= "Name: ".clean_string($name)."\n";

    $email_message .= "Email: ".clean_string($email)."\n";

    $email_message .= "Message: ".clean_string($message)."\n";





// create email headers

$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);  
if(mail){
echo "Thank you for contacting us. We will be in touch with you very soon.";
}
else {
echo "There's been an error. Please try again.";
}

 ?>

<?php

}

?>
<?php
   header( 'Location: http://kingdomhearts7.com/test3' ) ;
?>

I would really appreciate it if someone would help me out as I am a complete noob at php.

Thanks.

UPDATE: I was able to fix the headers error by removing some whitespace, but now I ran into a new problem.

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in >>/home3/ettrick/public_html/test3/mail.php on line 28

<?php 
$email_message .= "Name: ".($name)."\n";

$email_message .= "Email: ".($email)."\n";

$email_message .= "Message: ".($message)."\n";
mail($ToEmail, $EmailSubject, $email_message, $mailheader); 
?>
<?php
if(isset($_POST['email'])) {
       // EDIT THE 2 LINES BELOW AS REQUIRED
$email_from = "newsletter@kingdomhearts7.com";
$email_to = "fritzgattereau@gmail.com";
$email_subject = "Contact Form";
$email_message = "Form details below.\n\n";
function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" 

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);  
if(mail){
echo "Thank you for contacting us. We will be in touch with you very soon.";
}
else {
echo "There's been an error. Please try again.";
}
?>
<?php
}
?>
<?php
header( 'Location: http://kingdomhearts7.com/test3' ) ;
?>

2 Answers2

1

Remove all the occurrences of

?>

<?php

Because they already make for output (the newlines in between), before you start to send your header.

Also, if(isset($_POST['email'])) , you are doing echo text. This will also cause output that will prevent you from sending headers.

To solve this, only send headers if you didn't echo anything (Especially if you are sending the 'Location' header. Because what would even be the point of echoing text, as you won't get to read it?).

Or by using ob_start() at the start and ob_end_flush() at the end/after header(). (But this would still mean you are echoing text that you won't get to read, because header( "Location: ..."); will redirect before you get to read the echoed text.

nl-x
  • 11,762
  • 7
  • 33
  • 61
  • So you're saying that I should redirect before the email is sent? I don't think that'll work. – user3044278 Dec 17 '13 at 00:00
  • @user3044278 : no, I'm saying that you are doing `echo` after you send the e-mail, but before you do the redirect. That is one of your problems. ( `echo "Thank you for contacting us. We will be in touch with you very soon.";` and `echo "There's been an error. Please try again.";` ) If the mail was successful, you should just redirect without any echo. And if the mail failed, you should echo the error, but not redirect for example. – nl-x Dec 17 '13 at 00:11
1

Never leave spaces or new line between <?php and ?>

<?php 
$email_message .= "Name: ".($name)."\n";

$email_message .= "Email: ".($email)."\n";

$email_message .= "Message: ".($message)."\n";
mail($ToEmail, $EmailSubject, $email_message, $mailheader); 
?><?php
  ^
  +--- new line
dynamic
  • 46,985
  • 55
  • 154
  • 231