为什么我的PHP邮件表单不起作用?

时间:2015-08-06 19:16:42

标签: php html forms email

我的网络表单提交无效。它将显示已发送但从未收到任何电子邮件的消息。我确保电子邮件是正确的。

HTML表单:

  <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脚本:

<?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

}

?>

0 个答案:

没有答案