0

My web form submit is not working. It will display message that it has been sent but never receive any email. I made sure email is correct.

HTML form:

  <form action="mail.php" method="post" onSubmit="formValidator()">


            <input class="input-text animated wow flipInY delay-02s" type="text" name="name" value="Your Name *" onFocus="if(this.value==this.defaultValue)this.value='';" onBlur="if(this.value=='')this.value=this.defaultValue;">
            <input class="input-text animated wow flipInY delay-04s" type="text" name="email" value="Your E-mail *" onFocus="if(this.value==this.defaultValue)this.value='';" onBlur="if(this.value=='')this.value=this.defaultValue;">
            <textarea class="input-text text-area animated wow flipInY delay-06s" cols="0" rows="0" onFocus="if(this.value==this.defaultValue)this.value='';" onBlur="if(this.value=='')this.value=this.defaultValue;" name="comment">Your Message *</textarea>
            <input class="input-btn animated wow flipInY delay-08s" type="submit" value="send message">
            </form>

PHP script:

<?php

if(isset($_POST['email'])) {

    $email_to = "hi@hi.com";

    $email_subject = "Web Form Submission";

    function died($error) {

        echo "We are very sorry, but there were error(s) found with the form you submitted. ";

        echo "These errors appear below.<br /><br />";

        echo $error."<br /><br />";

        echo "Please go back and fix these errors.<br /><br />";

        die();

    }

             // validation expected data exists

             if(!isset($_POST['name']) ||

        !isset($_POST['email']) ||

        !isset($_POST['comment'])) {

        died('We are sorry, but there appears to be a problem with the form you submitted.');       

    }



    $name = $_POST['name']; // required

    $email = $_POST['email']; // required

    $comment = $_POST['comment']; // required



    $error_message = "";

    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if(!preg_match($email_exp,$email)) {

    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';

  }

    $string_exp = "/^[A-Za-z .'-]+$/";

  if(!preg_match($string_exp,$name)) {

    $error_message .= 'The First Name you entered does not appear to be valid.<br />';

  }


  if(strlen($comment) < 2) {

    $error_message .= 'The Comments you entered do not appear to be valid.<br />';

  }

  if(strlen($error_message) > 0) {

    died($error_message);

  }

    $email_message = "Form details below.\n\n";



    function clean_string($string) {

      $bad = array("content-type","bcc:","to:","cc:","href");

      return str_replace($bad,"",$string);

    }



    $email_message .= "First Name: ".clean_string($name)."\n";

    $email_message .= "Email: ".clean_string($email)."\n";

    $email_message .= "Comments: ".clean_string($comment)."\n";


// email headers

$headers = 'From: '.$email."\r\n".

'Reply-To: '.$email."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);  

?>



<!-- success message -->



Thank you! We have received your request. We will be in touch soon. If the matter is urgent or require attention immediately please contact via phone.



<?php

}

?>
halfer
  • 19,824
  • 17
  • 99
  • 186
  • The error suppression operator `@` is probably worth removing - it is rare that this should appear in any production code. – halfer Aug 06 '15 at 19:22
  • @halfer he made that function – Sayed Aug 06 '15 at 19:23
  • Personally I think the `mail()` function is hit-and-miss, and it rather depends on your server configuration. Consider using a proper library, like PHPMailer or SwiftMailer instead. – halfer Aug 06 '15 at 19:23
  • @phplover: good spot, thanks! Comment edited. (Side note: be careful about gender assumptions please). – halfer Aug 06 '15 at 19:23
  • @halfer oh sorry :D .. @user2197569 do you have this page on localhost? you need a mail server for `mail()` to work as @halfer said – Sayed Aug 06 '15 at 19:26
  • Possibly because of all the extra newlines in the `$header` remove the empty line – RiggsFolly Aug 06 '15 at 19:30
  • @user2197569: please take a moment to learn how the code formatting tools work. I've repaired this question for you. We prefer a succinct and descriptive title, and then an introduction as to what the problem is, and what you have tried, and then the code. – halfer Aug 06 '15 at 19:34

0 Answers0