0

I'm trying to create a contact form where the user will fill out their name, email, subject and message in order to contact me. It is suppose to send an email to my email account but every time i test it, it does not work. I was positive it was correct, but i guess it is not. Any help please?

<?php

//Get user input
$name = $_POST["name"];
$email = $_POST["email"];
$subject = $_POST["subject"]
$message = $_POST["message"];

//error messages
$missingName = '<p><strong>Please enter your name!</strong></p>'; 
$missingEmail = '<p><strong>Please enter your email address!</strong></p>'; 
$invalidEmail = '<p><strong>Please enter a valid email address!</strong></p>';
$missingSubject = '<p><strong>Please enter a Subject!</strong></p>';
$missingMessage = '<p><strong>Please enter a message!</strong></p>'; 

//if the user has submitted the form
if($_POST["submit"]){
   //check for errors
     if(!$name){
       $errors .= $missingName;  
    }else{
       $name = filter_var($name,FILTER_SANITIZE_STRING);   
    }
     if(!$email){
       $errors .= $missingEmail;   
    }else{
      $email = filter_var($email, FILTER_SANITIZE_EMAIL);
      if(!filter_var($email,FILTER_VALIDATE_EMAIL)){
          $errors .=$invalidEmail;   
      }
  }

    if(!$subject){
       $errors .= $missingSubject;
   }else{
       $message = filter_var($subject, FILTER_SANITIZE_STRING);   
   }

   if(!$message){
      $errors .= $missingMessage;
   }else{
       $message = filter_var($message, FILTER_SANITIZE_STRING);   
   }

    //if there are any errors
    if($errors){
       //print error message
       $resultMessage = '<div class="alert alert-danger">' . $errors .'</div>';   
   }else{
     $to = "fanonxr@gmail.com";
     $subject = "Contact";
     $message = "
     <p>Name: $name.</p>
     <p>Email: $email.</p>
     <p>Subject: $subject.</p>
     <p>Message:</p>
     <p><strong>$message</strong></p>"; 
     $headers = "Content-type: text/html";
     if(mail($to, $subject, $message, $headers)){
        $resultMessage = '<div class="alert alert-success">Thanks for your message. We will get back to you as soon as possible!</div>';  
        header("Location: index.php");
      }else{
          $resultMessage = '<div class="alert alert-warning">Unable to send Email. Please try again later!</div>';  
     }
 }
 echo $resultMessage;

} ?>

FanonX Rogers
  • 29
  • 2
  • 6

2 Answers2

0

it seems you are missing the form action and mailto since its Html i think those should be included.

Bright umor
  • 88
  • 1
  • 1
  • 9
0

your validation looks good to me. In terms of sending email, try finding some answers here:

PHP mail form doesn't complete sending e-mail

If that doesn't address your question, please provide some more info, like what mailer you're using. Hope this helps :)

KayeeJayee
  • 46
  • 7