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

phpmailer not working

Hi, I'm working on the lessons where we integrate phpmailer. I just started it, and my code is exactly the same as in the video (in regards to phpmailer), but when I require phpmailer I just get a blank screen when I submit the form. If I remove the require_once command everything works fine. I have no idea why this is happening. Here is what I have (and yes the file is located in the correct place)

<?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 "The form is not complete";
            exit;
        }

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

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

        if (!$mail->ValidateAddress($email)) {
            echo "You must enter a valid email";
            exit;
        }

        $email_body = "";
        $email_body = $email_body . "Name: " . $name . "\n";
        $email_body = $email_body . "Email: " . $email . "\n";
        $email_body = $email_body . "Message: " . $message . "\n";

        //TODO: Send Email

        header("Location: contact.php?status=thanks");
        exit;
    }

    $pageTitle = "Mike's Contact Info"; 
    $section = "contact";
?>

2 Answers

The PHPMailerAutoload.php file needs to be included into your includes folder. This will correct your problem. At least it did mine.

This worked for me too. Thanks Marianne!

Removing

require_once("inc/phpmailer/class.phpmailer.php");

shouldn't allow your script to keep executing as you should get an exception saying class PHPMailer was not found, could you please check your PHP error log as it will help explain why your script fails at that point.