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 Basic PHP Website (2018) Enhancing a Form Error Message Priority

issue using PHPMailer alway display "Invalid Email Address"

why it alway display Invalid Email Address although i type right or wrong My picture: http://imgur.com/7HAaOWy My code:

<?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(!isset($error_message) && ($name == "" || $email == "" || $category=="" || $title=="")){
      $error_message ="Please fill in the required fields: Name, Email, Title and Category";

  }
  if (!isset($error_message) && $_POST["address"] != "") {
    $error_message = "Bad form input";

  }

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

$mail = new PHPMailer;
if( $mail ->ValidateAddress($email)){
 $error_message = "Invalid Email Address";
}


if(!isset($error_message)){
      $email_body = "";
      $email_body.= "Name ".$name. "\n";
      $email_body.= "Suggest 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 ".$email. "\n";
      $email_body.= "Email ".$email. "\n";
      $email_body.= "Details ".$details. "\n";

       $mail->setFrom($email, $name);
      $mail->addAddress('treehouse@localhost', 'Alena');     // Add a recipient


      $mail->isHTML(false);                                  // Set email format to HTML

      $mail->Subject = 'Here is the subject';
      $mail->Body    = $email_body;


      if($mail->send()) {
           header("location:suggest.php?status=thanks");
      }
          $error_message = 'Message could not be sent.';
          $error_message .= 'Mailer Error: ' . $mail->ErrorInfo;



   } 

//To Do: Send Email

}

$pageTitle = "Suggest a Media Item";
$section = "suggest";

include("inc/header.php"); 


?>

<div class="section page">
  <div class="wrapper" >
     <h1>Suggest a Media Item</h1>
      <?php if(isset($_GET["status"]) && $_GET["status"] == "thanks"){
          echo  "<p> Thanks for the email! I will check out your suggestion shortly!</p>"; } else {


      if(isset($error_message)){
            echo "<p class='message'>".$error_message ."</p>";
          }
            else {
              echo "<p>If you think there is something I&rsquo;m missing, let me know! Complete the form to send me an email.</p>";
          }

      ?>

hey,

you are forget to include ! operator that's the only problem i guess

if($mail ->ValidateAddress($email)){  //not operator is missing here
 $error_message = "Invalid Email Address";
}

2 Answers

Simon Coates
Simon Coates
28,694 Points

The explanation of PHPmailers validateAddress function is "Check that a string looks like an email address.". So as Ashish Mehra suggested, you need to use: if(!$mail ->validateAddress($email)){

Thanks