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 trialLixi Daniele
9,849 PointsvalidateAddress not working
I'am following along Alena and trying to display error messages on the same page if the form is incomplete or if there is an invalid email address, but unfortunately the validateAddress of PHPMailer method doesn't seems to work as intended. I can write anything in the email field and I doesn't throw out the error, instead it actually send the email and I receive and can read it through the console utility. Can you help to spot where the problem could be?
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim(filter_input(INPUT_POST, "name", FILTER_SANITIZE_STRING));
$email = trim(filter_input(INPUT_POST, "email", FILTER_SANITIZE_EMAIL));
$category = trim(filter_input(INPUT_POST, "category", FILTER_SANITIZE_STRING));
$title = trim(filter_input(INPUT_POST, "title", FILTER_SANITIZE_STRING));
$format = trim(filter_input(INPUT_POST, "format", FILTER_SANITIZE_STRING));
$genre = trim(filter_input(INPUT_POST, "genre", FILTER_SANITIZE_STRING));
$year = trim(filter_input(INPUT_POST, "year", FILTER_SANITIZE_STRING));
$details = trim(filter_input(INPUT_POST, "details", FILTER_SANITIZE_SPECIAL_CHARS));
if ($name == "" || $email == "" || $category == "" || $title == "") {
$error_message = "Pleas fill in the required fields: Name, Email and Category and Title";
}
if ($_POST["address"] != "") {
$error_message = "Bad form Input";
}
require_once("inc/phpmailer/class.phpmailer.php");
if (!isset($error_message)) {
$mail = new PHPMailer();
if (!$mail->validateAddress($email)) {
$error_message = "Invalid email address";
}
$email_body = "";
$email_body .= "Name " . $name . "\n";
$email_body .= "Email " . $email . "\n";
$email_body .= "Suggested Item\n";
$email_body .= "Category " . $category . "\n";
$email_body .= "Title " . $title . "\n";
$email_body .= "Format " . $format . "\n";
$email_body .= "Genre " . $genre . "\n";
$email_body .= "Year " . $year . "\n";
$email_body .= "Details" . $details . "\n";
$mail->setFrom($email, $name);
$mail->addAddress('treehouse@localhost', 'Daniele'); // Add a recipient
$mail->isHTML(false); // Set email format to HTML
$mail->Subject = 'Personal Media Library Suggestion from ' . $name;
$mail->Body = $email_body;
if ($mail->send()) {
header("location:suggest.php?status=thanks");
exit;
}
$error_message = 'Message could not be sent.';
$error_message .= 'Mailer Error: ' . $mail->ErrorInfo;
}
}
$pageTitle = "Suggest a Media Item";
$section = "suggest";
include("inc/header.php");
if (isset($error_message)) {
echo $error_message;
} ?>
1 Answer
Lixi Daniele
9,849 PointsEven though the solution you proposed was not the correct one it made me realize that I erroneously placed that if statement to early. It should encapsulate all the code after the validateAddress check and not before. I corrected the code and now it's working. Thank you!
$mail = new PHPMailer();
if (!$mail->validateAddress($email)) {
$error_message = "Invalid email address";
}
if (!isset($error_message)) {
.....
Jason Anello
Courses Plus Student 94,610 Pointsmarked as Best Answer
michaelleonard4
7,013 PointsGlad it helped you
michaelleonard4
7,013 Pointsmichaelleonard4
7,013 PointsJust remove the if statement where is checks to see if the error message is not set