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 Sending The Contact Form Email

Johnatan Guzman
PLUS
Johnatan Guzman
Courses Plus Student 2,360 Points

The message is blank in the sent email

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = trim($_POST['name']);
    $email = trim($_POST['_replyto']);
    $message = trim($_POST['textarea']);

    if ($name == "" || $email == "" || $message = "") {
        echo "You must specify a value for you name, email adress, and/or message.";
        exit;
    }

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

    if ($_POST["address"] != "") {
        echo "Your submission was not added";
        exit;
    }

    require "inc/phpmailer/PHPMailerAutoload.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 . "</br>";
    $email_body = $email_body . "Email: " . $email . "</br>";
    $email_body = $email_body . "Message: " . $message;

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

    if(!$mail->send()) {
        echo 'Message could not be sent.' . "</br>";
        echo 'Error: ' . $mail->ErrorInfo;         
        exit;
    } 
    header("location: index.php?status=thanks");
    exit;
}

?>

Everything works, I get the email with the $name and $email but the message is blank. I tested the $message variable seperately and it is set-up correctly. Somehow msgHTML cant properly process it?

4 Answers

Welby Obeng
Welby Obeng
20,340 Points

I got the problem....$message = should be $message ==

= assigns ==compares

:)

Johnatan Guzman
Johnatan Guzman
Courses Plus Student 2,360 Points

Haha, that's so stupid of me. Thanks for the keen eye.

try with a hard-coded value, if that works then check if $message variable is getting the proper data.

Johnatan Guzman
Johnatan Guzman
Courses Plus Student 2,360 Points

Yes, I already tried that, the message variable gets the right information from the textarea, but somehow the method doesnt work

Welby Obeng
Welby Obeng
20,340 Points

why not use $mail->$Body and If HTML then call isHTML(true)?

Like this

$mail->Body    = $email_body;
$mail->isHTML(true);   
Johnatan Guzman
Johnatan Guzman
Courses Plus Student 2,360 Points

Sadly that didnt work :(

Message could not be sent. Error: Could not instantiate mail function.

Welby Obeng
Welby Obeng
20,340 Points

what version of phpmailer are you using?

Johnatan Guzman
Johnatan Guzman
Courses Plus Student 2,360 Points

Im not sure, I use the autoloader.php so it chooses automatically (I think the latest). I downloaded it today from their github.

The documentation is very bad though

Welby Obeng
Welby Obeng
20,340 Points

you have the link? also can I see the html part of the code?

Johnatan Guzman
Johnatan Guzman
Courses Plus Student 2,360 Points

my html is not wrong, if I echo out the $message it works properly, but now the mail is broken entirely, even with my old code i get error message

Welby Obeng
Welby Obeng
20,340 Points

I need the html to test it locally