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 Using A Third-Party Library

Charles Franklin
Charles Franklin
17,535 Points

PHP Mailer problem

Hey everyone.. Once again, noob here... I have my email contact form set up and it accepts inputs. When I got to the portion where we included the class.phpmailer.php file into the form, I wanted to add the error message if the user did not include a valid email. It seems that since the video has been done, the versions of phpmailer have progressed forward so I'm not sure I have the syntax right... Here is the exception rule as I have it right now...

if (!$mail->validateAddress($email)) {
    echo "You must specify a valid email address";
    exit;
}

Trouble is it doesnt echo out the error message. It simply gives me a blank screen. I'm guessing by the fact that it gives me the blank screen that the exit command executes, but the echo isnt... Not sure why, any thoughts?

chip

5 Answers

Gareth Borcherds
Gareth Borcherds
9,372 Points

It could be that you have another error on your page and you don't have error reporting working. Try putting something in the else statement to make sure it's validating. That or use error_log('failed validation'); in the if statement to see if you can make sure your code is evaluating.

If you post more of your code and let us know which phpmailer version you're using we might be able to help more.

Charles Franklin
Charles Franklin
17,535 Points

Here is the code for the form...

<?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 name, email and message..";
    exit;
}

foreach ($_POST as $value) {
    if(stripos($value, 'Content-Type:') !== FALSE) {
        echo "Caught ya!";
        exit;
    }
}

if ($_POST["address"] != "") {
    echo "You 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.";
    exit;
}

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


//TODO SEND EMAIL

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

} ?> <?php $pageTitle = "Contact Chip"; $section = "contact"; include('inc/header.php'); ?>

<div class="section page">

    <div class="wrapper">

        <h1>Contact</h1>

        <?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
            <p>Thanks for the email! I&rsquo;ll be in touch shortly.</p>
        <?php } else { ?>

            <p>I&rsquo;d love to hear from you! Complete the for to send me an email.</p>

            <form method="post" action="contact.php">
                <table>
                    <tr>
                        <th>
                            <label for="name">Name</label>
                        </th>
                        <td>
                            <input type="text" name="name" id="name">
                        </td>
                    </tr>
                </table>
                <table>
                    <tr>
                        <th>
                            <label for="email">Email</label>
                        </th>
                        <td>
                            <input type="text" name="email" id="email">
                        </td>
                    </tr>
                </table>
                <table>
                    <tr>
                        <th>
                            <label for="message">Message</label>
                        </th>
                        <td>
                            <textarea name="message" id="message">Yo whassup</textarea>
                        </td>
                    </tr>
                <tr style="display: none;">
                    <th>
                        <label for="address">address</label>
                    </th>
                    <td>
                        <input type="text" name="address" id="address">
                        <p>This is a botcheck...</p>
                    </td>
                </tr>

                </table>
                <table>
                    <tr>
                        <th>
                            <label for="reason">Reason</label>
                        </th>
                        <td>
                            <select>
                                <option value="General">General</option>
                                <option value="Job">Job</option>
                                <option value="Love">Love You</option>
                                <option value="Frog">Who's the frog?</option>
                            </select>
                        </td>
                    </tr>
                </table>
                <input type="submit" value="Send">
            </form>
        <?php } ?>              

    </div>

</div>

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

Gareth Borcherds
Gareth Borcherds
9,372 Points

Your code looks right. I would try playing around with putting echo 'test'; in various parts of the code to see if you can get it to show up so you can see where things are evaluated and where they fail.

Charles Franklin
Charles Franklin
17,535 Points

Ok, I finally figured it out.. I know this is stupid but here goes...

In the video it says $mail.... In the version of PHPMailer I have, it says...

Wait for it....

$Mailer

Yoi... :-) thanks for the help Gareth...

Randy Wang
Randy Wang
9,291 Points

Hi Charles, I am having the same blank page problem after submitting the form but I don't understand your solution. What did you do to solve this issue? Thanks.