title: php redirect after mail sent header error
body: <p>I have a form I got working fine but when I try and redirect after a form submission to a thank you page but get and error of " Cannot modify header information - headers already sent by ". I understand the headers are being modified in the <code>mail()</code> part but how do you do this without an error?</p>
  
  <p>Code:</p>
  
  <pre><code>&lt;?php
  //if "email" variable is filled out, send email
    if ( isset($_REQUEST['email']) ) {
  
      function died($error) {
          // your error code can go here
          echo "We are very sorry, but there were error(s) found with the form you
         submitted. ";
          echo "These errors appear below.&lt;br /&gt;&lt;br /&gt;";
          echo $error."&lt;br /&gt;&lt;br /&gt;";
          echo "Please go back and fix these errors.&lt;br /&gt;&lt;br /&gt;";
          die();
      }
  
     // validation expected data exists
      if(!isset($_POST['first_name']) ||
          !isset($_POST['last_name']) ||
          !isset($_POST['email']) ||
          !isset($_POST['phone'])) {
          died('We are sorry, but there appears to be a problem with the form you submitted.');      
      }
  
    //Form Variables
      //Personal Information 
      $subject = "Smile Guide";
      $email = $_REQUEST['email'];
      $first_name = $_REQUEST['first_name'];
      $last_name = $_REQUEST['last_name'];
      $phone = $_REQUEST['phone'];
      $admin_email = "packy@ocularlogic.com,$email";
  
      //Stage 1
      $chipped = $_REQUEST['smile-fault-1'];
      $color = $_REQUEST['smile-fault-2'];
      $gaps = $_REQUEST['smile-fault-3'];
      $worn = $_REQUEST['smile-fault-4'];
      $crooked = $_REQUEST['smile-fault-5'];
      $metal = $_REQUEST['smile-fault-6'];
  
      //Stage 2
      $aggressive = $_REQUEST['smile-prefered-1'];
      $mature = $_REQUEST['smile-prefered-2'];
      $vigorous = $_REQUEST['smile-prefered-3'];
      $dominant = $_REQUEST['smile-prefered-4'];
      $focused = $_REQUEST['smile-prefered-5'];
      $enhanced = $_REQUEST['smile-prefered-6'];
      $soften = $_REQUEST['smile-prefered-7'];
      $hollywood = $_REQUEST['smile-prefered-8'];
      $functional = $_REQUEST['smile-prefered-9'];
      $natural = $_REQUEST['smile-prefered-10'];
      $youthful = $_REQUEST['smile-prefered-11'];
      $oval = $_REQUEST['smile-prefered-12'];
  
      //Stage 3
      $shade = $_REQUEST['smile-color'];  
  
      //List errors
       $error_message = "";
          $string_exp = "/^[A-Za-z0-9 .'-]+$/";
        if(!preg_match($string_exp,$first_name)) {
          $error_message .= 'The First Name you entered does not appear to be valid.&lt;br /&gt;';
        }
        if(!preg_match($string_exp,$last_name)) {
          $error_message .= 'The Last Name you entered does not appear to be valid.&lt;br /&gt;';
        }  
          $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
        if(!preg_match($email_exp,$email)) {
          $error_message .= 'The Email Address you entered does not appear to be valid.&lt;br /&gt;';
        }
        if(!preg_match($string_exp,$phone)) {
          $error_message .= 'The Phone Number you entered does not appear to be valid.&lt;br /&gt;';
        }
        if(strlen($error_message) &gt; 0) {
          died($error_message);
        }      
  
      //The Message
      $content = '&lt;html&gt;&lt;body&gt;&lt;table width="100%" border="0" cellspacing="0" cellpadding="0"&gt;&lt;tr&gt;&lt;td align="center"&gt;';
      $content .= '&lt;img src="http://smiletothemaxdentallab.com/guide/smile-logo.jpg" style="display: block; margin: 10px auto;"&gt;';
      $content .= '&lt;h1 style="color: #c51f4f;"&gt;Smile Guide&lt;/h1&gt;';
      $content .= "&lt;h2 style='background-color:#c51f4f; color:#fff; padding:10px 0;'&gt;Client Information&lt;/h2&gt;&lt;ul style='list-style-type: none; color:#999999;'&gt;";
      $content .= "&lt;li&gt;Name:".$last_name.",".$first_name."&lt;/li&gt;";
      $content .= "&lt;li&gt;Phone:".$phone."&lt;/li&gt;";
      $content .= "&lt;li&gt;Email:".$email."&lt;/li&gt;";
      $content .= "&lt;/ul&gt;";
      $content .= "&lt;h2 style='background-color:#c51f4f; color:#fff; padding:10px 0;'&gt;Current Teeth Issues&lt;/h2&gt;&lt;ul style='list-style-type: none; color:#999999;'&gt;";
      $content .= isset($_POST['smile-fault-1']) ? "&lt;li&gt;".$chipped."&lt;/li&gt;" : "";
      $content .= isset($_POST['smile-fault-2']) ? "&lt;li&gt;".$color."&lt;/li&gt;" : "";
      $content .= isset($_POST['smile-fault-3']) ? "&lt;li&gt;".$gaps."&lt;/li&gt;" : "";
      $content .= isset($_POST['smile-fault-4']) ? "&lt;li&gt;".$worn."&lt;/li&gt;" : "";
      $content .= isset($_POST['smile-fault-5']) ? "&lt;li&gt;".$crooked."&lt;/li&gt;" : "";
      $content .= isset($_POST['smile-fault-6']) ? "&lt;li&gt;".$metal."&lt;/li&gt;" : "";
      $content .= "&lt;/ul&gt;";
      $content .= "&lt;h2 style='background-color:#c51f4f; color:#fff; padding:10px 0;'&gt;Desired Color&lt;/h2&gt;&lt;ul style='list-style-type: none; color:#999999;'&gt;";
      $content .= "&lt;li&gt;".$shade."&lt;/li&gt;";
      $content .= "&lt;/ul&gt;";
      $content .= "&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td style='background-color:#F7F7F7; color:#999999; text-align:center; padding: 50px 0;'&gt;&lt;p&gt;Brought to you be AOC Dental Lab&lt;/p&gt;&lt;p&gt;Toll free (800) 729-1593&lt;/p&gt;&lt;p&gt;1724 E Sherman Ave | Coeur d’Alene, Idaho | 83814&lt;/p&gt;";
      $content .= "&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;";
  
      //Set up Header
      $headers = "From: " . strip_tags($_POST['email']) . "\r\n";
      $headers .= "Reply-To: ". strip_tags($_POST['email']) . "\r\n";
      $headers .= 'MIME-Version: 1.0' . "\r\n";
      $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  
      //Send Email
      $send = mail($admin_email, $subject, $content, $headers);
  
      if($send) {
          header("Location: http://website.com/smile-guide/thank-you");
      } 
  
  }
  
  ?&gt;
  </code></pre>