Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

jason taylor
jason taylor
2,455 Points

Error using PHPmailer on live site. Getting warning re: the header redirect: Warning: Cannot modify header information - headers already sent by (output started at... etc.

Hi Everyone. Working through the PHPmailer lessons and set it up on my live server. The email sends but after submitting the form I get a warning: Warning: Cannot modify header information - headers already sent by (output started at... I added a few extra things like a jquery datepicker and a few extra form fields but essentially my code is identical to the lesson (I think!!)

//THIS THE JQUERY CODE FOR THE TE PICKER YOU CAN IGNORE THIS

<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
$( "#date" ).datepicker();
});
</script>

<style>
.address{
    display:none !important;    
    }
</style>

//THE REAL CODE STARTS HERE

<?php
//PROCESS THE FORM
if ($_SERVER["REQUEST_METHOD"] == "POST"){
        $name = trim($_POST["name"]);
        $email = trim($_POST["email"]);
        $tel = trim($_POST["tel"]);
        $date = trim($_POST["date"]);
        $message = trim($_POST["message"]);

        if ($name== "" OR $email == "" OR $tel == "" OR $date == ""){
            echo "You left a required field blank! Please fill in ALL the required fields.";
            exit;
            }

            //KILL SPAMMERS
foreach($_POST as $value){
    if(stripos($value,'Content-Type:') !== FALSE){
        echo "You are a spammer";
        exit;
        }

    }
    if ($_POST["address"] !=""){
        echo "You didn't fill in the honey pot. Please try again.";
        exit;
        }

//LOAD PHPMAILER OBJECT
require_once("class.phpmailer.php");
$mail = new PHPMailer();
if($mail ->ValidateAddress($email)){
    echo "Sorry your email did not go through. Please try again.";
    exit;
    }
//END PHPMAILER

//GET THE FORM INFO AND SEND THE EMAIL
        $email_body="";
        $email_body = $email_body . "Name" . $name . "<br>";
        $email_body = $email_body . "Email" . $email . "<br>";
        $email_body = $email_body . "Tel" . $tel . "<br>";
        $email_body = $email_body . "Date" . $date . "<br>";
        $email_body = $email_body . "People" . $people . "<br>";
        $email_body = $email_body . "Message" . $message;

        $email->SetFrom($email, $name);
        $address="info@mysite.com";
        $mail->AddAddress($address, "Contact Message");
        $mail->Subject = "Online Reservation";
        $mail->MsgHTML($email_body);

        if(!$mail->Send()){
            echo "There was an error sending your reservation." . $mail->ErrorInfo;
            exit;
        }

    header("Location: http://mysite.com/testing/contact.php?status=thanks");
        exit;
    }
?>





<?php 
//SUCCESS MESSAGE
if (isset ($_GET["status"]) AND $_GET["status"] == "thanks") { 
?>
<h1>Thanks for contacting us! Talk to you soon...</h1> <?php } else{  ?>




<!--Contact FORM-->
<form method="post" action="contact.php">

<table>
    <tr>
        <th><label for="name">Name / Nombre:</label></th> 
            <td><input type="text" name="name" id="name"></td>
     </tr>

    <tr>     
        <th><label for="email">Email:</label></th> 
            <td><input type="text" name="email" id="email"></td>
    </tr>

    <tr>
        <th><label for="tel">Tel:</label> </th>
            <td><input type="text" name="tel" id="tel"></td>
    </tr>

            <tr class="address">
            <th><label for="address">Address</label></th> 
            <td><input type="text" name="address" id="address"></td>
            </tr>


    <tr>
        <th><label for="date">Date of Reservation? / Fecha?</label> </th>
            <td><input type="text" name="date" id="date"></td>
    </tr>

    <tr>
        <th><label for="people">How many people? / Cuantas personas?</label> </th>
            <td><input type="text" name="people" id="people"></td>
    </tr>

    <tr>
        <th><label for="message">Message / Mensaje:</label> </th>
            <td><textarea name="message" id="message"></textarea></td>
    </tr>

</table>

<input type="submit" value="Send">
</form>
<?php } ?>```

2 Answers

Pol Martin
Pol Martin
8,200 Points

Hi Jason. I think its not because of PHPMailer but PHP itself. When you change headers in PHP it must be BEFORE all the output sent by that script.

You can read about it with full details and try to find your case: http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php

jason taylor
jason taylor
2,455 Points

Thanks for the reply Pol My question is how do you move the location of the header i it's part of the conditional statement relating to the sending of the mail? I have it in the same place that is described in the tutorial. Is there a major issue in just using an echo statement instead? eg. echo "message sent successfully";