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 Build a Simple PHP Application Wrapping Up The Project Deploying The Site

Contact form not working in live site

In my live site, the error message is fine but instead of redirecting to to /index.php?status=thanks it goes to /index.php and is blank. It's not sending emails either. On my localhost, the redirect to header("Location: index.php?status=thanks"); works fine.

I'm looking at the blog post (http://blog.teamtreehouse.com/sending-email-with-phpmailer-and-smtp) from the notes but I'm confused because I thought I was already using phpmailier (via this tutorial) already?

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = trim($_POST["name"]);
    $email = trim($_POST["email"]);
    $message = trim($_POST["message"]);

    if ($name == "" OR $email == "" OR $message == "") {
        echo "You must specify a value for name, email address, and message. Please press the back button to try again.";
        exit;
    }

    foreach( $_POST as $value ){
        if( stripos($value,'Contnet-Type:') !== FALSE){
            echo "There was a problem with the information you entered.";
            exit;       
        }
    }

    if ($_POST["adress"] != "") {
        echo "Your form submission has an error.";
        exit;
    }

    require_once("inc/phpmailer/class.phpmailer.php");
    $mail = new PHPMailer;

    if (!$mail->ValidateAddress($email)) {
        echo "You must specify a valid email address. Use the back button to continue";
        exit;
    }

    $email_body = "";
    $email_body = $email_body . "Name: " . $name . "<br />";
    $email_body = $email_body . "Email: " . $email . "<br />";
    $email_body = $email_body . "Message: " . $message;

    $mail->setFrom($email, $name);
    $address = "##email##@gmail.com";
    $mail->addAddress($address , '##name##');
    $mail->Subject = "Contact Form Submission | " . $name;
    $mail->msgHTML($email_body);


    if (!$mail->send()) {
        echo "There was a problem sending the email: " . $mail->ErrorInfo;
        exit;
    }
    header("Location: index.php?status=thanks");
    exit;
}   
?>


<?php include('header.php'); ?>


    <?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>

        <div class="thanks-section">
            <div class="text-container">
            <h1>Thank You!</h1>
                <p id="message-confirm">Your message has been sent.</p>
            </div> <!--/text-container-->
        </div><!--/thanks-section-->        

    <?php } else { ?>

<body>......</body>

<?php include('footer.php'); ?>

<?php } ?>

Could you post the code from index.php so we could take a look?

Thanks for adding code! I cannot see anything that jumps out right away at me. I think the first thing we should do is turn on error reporting so that we can see what is going wrong, rather than that blank page! Add the following code right after your opening <?php tag to enable error messages.

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

I have a feeling that php is encountering an error somewhere in your initial if statement, and that should allow us to see what that error is.

5 Answers

Thanks for getting those error messages! Let's knock them out one at a time

  1. Notice: Undefined index: adress in /home/valeriegallagher/public_html/index.php on line 23 That's if ($_POST["adress"] != "") {

This is just a small typo, change this to:

if ($_POST["address"] != "") {

I noticed one other small typo that could cause you problems. The line:

if( stripos($value,'Contnet-Type:') !== FALSE)

should be:

if( stripos($value,'Content-Type:') !== FALSE)
  1. Warning: require_once(inc/phpmailer/class.phpmailer.php): failed to open stream: No such file or directory in /home/valeriegallagher/public_html/index.php on line 28

  2. Fatal error: require_once(): Failed opening required 'inc/phpmailer/class.phpmailer.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/valeriegallagher/public_html/index.php on line 28

This seems to be the problem! Since require_once was used, php will give up completely when it fails to load the file. The next thing I would do is verify that the file 'inc/phpmailer/class.phpmailer.php' exists and is in the correct location. Since your index file is at /home/valeriegallagher/public_html/index.php, the phpmailer file should be at /home/valeriegallagher/public_html/inc/phpmailer/class.phpmailer.php

Thanks for helping! Here's the error message:

  1. Notice: Undefined index: adress in /home/valeriegallagher/public_html/index.php on line 23 That's if ($_POST["adress"] != "") {

  2. Warning: require_once(inc/phpmailer/class.phpmailer.php): failed to open stream: No such file or directory in /home/valeriegallagher/public_html/index.php on line 28

  3. Fatal error: require_once(): Failed opening required 'inc/phpmailer/class.phpmailer.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/valeriegallagher/public_html/index.php on line 28

After fixing the typos, the error messages are the same.

Ok, the line 28 errors are gone but the line 23 error is still there, even though I fixed the typo:

if ($_POST["address"] != "") { echo "Your form submission has an error."; exit; }

Notice: Undefined index: address in /Applications/MAMP/htdocs/valerie-gallagher.com/index.php on line 23

Fixing the line 28 error made this happen:

Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/valerie-gallagher.com/index.php:23) in /Applications/MAMP/htdocs/valerie-gallagher.com/index.php on line 52

The line 23 error may be caused by a typo in the form itself, not passing a field with the name address. Check to see what the field's name attribute is on the form. to suppress this error, change the if statement to:

 if (!isset($_POST["address"] && $_POST["address"] != "") {

that will ensure that if no address is set, the error message you set will display. This will of course exit the application if the form itself does not have a field named "address" that is being submitted.

That last one is a bit of a false flag I think. The error comes up when something is outputted before you attempt to redirect. I believe one of the earlier conditions may have been causing it. A possible quick fix would be to add this to the beginning of your php script:

ob_start();

this will enable output buffering on this script, something that is normally configured on the server. As you are hosting this, it's possible your server has this turned off. You can see the documentation for ob_start() on PHP.net.

I'd be more than happy to take a look at this on a live example. Do you have a url I could look at to see the form submission in action?

You were totally right! I forgot that I had replaced address with something else in the actual form. Everything works now. I really, really appreciate you quick and persistent help!