邮件发送标题错误后{php}重定向

时间:2015-11-11 21:57:30

标签: php

我有一个表单我工作正常但是当我尝试将表单提交后重定向到感谢页面但得到并且错误“无法修改标题信息 - 已经发送的标题”。我理解在mail()部分正在修改标题,但是如何在没有错误的情况下执行此操作?

代码:

<?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.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        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.<br />';
      }
      if(!preg_match($string_exp,$last_name)) {
        $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
      }  
        $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.<br />';
      }
      if(!preg_match($string_exp,$phone)) {
        $error_message .= 'The Phone Number you entered does not appear to be valid.<br />';
      }
      if(strlen($error_message) > 0) {
        died($error_message);
      }      

    //The Message
    $content = '<html><body><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td align="center">';
    $content .= '<img src="http://smiletothemaxdentallab.com/guide/smile-logo.jpg" style="display: block; margin: 10px auto;">';
    $content .= '<h1 style="color: #c51f4f;">Smile Guide</h1>';
    $content .= "<h2 style='background-color:#c51f4f; color:#fff; padding:10px 0;'>Client Information</h2><ul style='list-style-type: none; color:#999999;'>";
    $content .= "<li>Name:".$last_name.",".$first_name."</li>";
    $content .= "<li>Phone:".$phone."</li>";
    $content .= "<li>Email:".$email."</li>";
    $content .= "</ul>";
    $content .= "<h2 style='background-color:#c51f4f; color:#fff; padding:10px 0;'>Current Teeth Issues</h2><ul style='list-style-type: none; color:#999999;'>";
    $content .= isset($_POST['smile-fault-1']) ? "<li>".$chipped."</li>" : "";
    $content .= isset($_POST['smile-fault-2']) ? "<li>".$color."</li>" : "";
    $content .= isset($_POST['smile-fault-3']) ? "<li>".$gaps."</li>" : "";
    $content .= isset($_POST['smile-fault-4']) ? "<li>".$worn."</li>" : "";
    $content .= isset($_POST['smile-fault-5']) ? "<li>".$crooked."</li>" : "";
    $content .= isset($_POST['smile-fault-6']) ? "<li>".$metal."</li>" : "";
    $content .= "</ul>";
    $content .= "<h2 style='background-color:#c51f4f; color:#fff; padding:10px 0;'>Desired Color</h2><ul style='list-style-type: none; color:#999999;'>";
    $content .= "<li>".$shade."</li>";
    $content .= "</ul>";
    $content .= "</td></tr><tr><td style='background-color:#F7F7F7; color:#999999; text-align:center; padding: 50px 0;'><p>Brought to you be AOC Dental Lab</p><p>Toll free (800) 729-1593</p><p>1724 E Sherman Ave | Coeur d’Alene, Idaho | 83814</p>";
    $content .= "</td></tr></table></body></html>";

    //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");
    } 

}

?>

2 个答案:

答案 0 :(得分:0)

此错误是由于您在尝试发送标题时已将数据发送到客户端(应首先发送)。

检查您的网页编码。可能是UTF-8 +(UTF-8 signature

如果没有,则需要检查空格。

答案 1 :(得分:0)

不要忘记在重定向后退出

if($send) {
  header("Location: http://website.com/smile-guide/thank-you");
  exit();
}