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 trialJohnatan Guzman
Courses Plus Student 2,360 PointsThe 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
20,340 PointsI got the problem....$message = should be $message ==
= assigns ==compares
:)
Calistus Oledibe
176 Pointstry with a hard-coded value, if that works then check if $message variable is getting the proper data.
Johnatan Guzman
Courses Plus Student 2,360 PointsYes, I already tried that, the message variable gets the right information from the textarea, but somehow the method doesnt work
Welby Obeng
20,340 Pointswhy not use $mail->$Body and If HTML then call isHTML(true)?
Like this
$mail->Body = $email_body;
$mail->isHTML(true);
Johnatan Guzman
Courses Plus Student 2,360 PointsSadly that didnt work :(
Message could not be sent. Error: Could not instantiate mail function.
Welby Obeng
20,340 Pointswhat version of phpmailer are you using?
Johnatan Guzman
Courses Plus Student 2,360 PointsIm 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
20,340 Pointsyou have the link? also can I see the html part of the code?
Welby Obeng
20,340 Pointshtml code?
Johnatan Guzman
Courses Plus Student 2,360 Pointsmy 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
20,340 PointsI need the html to test it locally
Johnatan Guzman
Courses Plus Student 2,360 PointsThe limited error messages are so annoying.
Johnatan Guzman
Courses Plus Student 2,360 PointsJohnatan Guzman
Courses Plus Student 2,360 PointsHaha, that's so stupid of me. Thanks for the keen eye.