PHP邮件验证

时间:2018-08-14 07:00:57

标签: php email

我真的需要一些帮助! 我正在尝试在我的网站上设置一个联系表单,该表单将在发送到我的收件箱之前验证并接受用户的信息。但是问题是我的代码都没有。我尝试研究各种解决方案,但似乎无济于事!我在这里完全迷路了。请帮忙!!以下包括HTML和PHP代码。

HTML:

   <form method="post" name="contact" action="contact_form.php" >
    <div>
         <input type="text" name="name" placeholder="Name" tabindex="1"/>
        <span class="error"><?php $name_error;?></span>
    </div>

    <div>
        <input type="text" name="company" placeholder="Company Name" tabindex="2"/>
      <span class="error"><?php $company_error;?></span>
    </div>

    <div>
         <input type="text" name="email" placeholder="Email" tabindex="3"/>
        <span class="error"><?php $email_error;?></span>
    </div>

    <div>
       <input type="text" name="subject" placeholder="Subject" tabindex="4"/>
       <span class="error"><?php $subject_error;?></span>
    </div>

    <div>
       <input type="text" name="website" placeholder="Enter Website (If Applicable)" tabindex="5"/>
    <span class="error"><?php $website_error;?></span>
    </div>

    <div>
      <textarea name="message" rows="8" placeholder="Message" tabindex="6"></textarea>
      <span class="error"><?php $message_error;?></span>
   </div>

   <div class="g-recaptcha" data-sitekey=" "></div>

    <button name="submit" type="submit" id="contact-button">Submit</button>
   <div class="success"><?php $success;?></div>

   </form>

PHP:

<?php
    // define variables and set to empty values
    $name_error = $company_error = $email_error = $subject_error = $message_error = $website_error = " ";
    $name = $company = $email = $subject = $message = $website = $success = " ";


    $from = $_POST["email"];
    $to = "name@example.com";
    $subject = $_POST["subject"];
    $message = $_POST["message"];

    // form is submitted with POST method
    if (isset($_POST["submit"])) {
        if (empty($_POST["name"])) {
            $name_error = "Name is required";
        } else {
            $name = test_input($_POST["name"]);
            // check if name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z]*$/", $name)) {
                $name_error = "Only letters and white space allowed";
            }
        }


        if (empty($_POST["company"])) {
            $company_error = "Company name is required";
        } else {
            $company = test_input($_POST["company"]);
        }


        if (empty($_POST["email"])) {
            $email_error = "Email is required";
        } else {
            $email = test_input($_POST["email"]);
            // check if e-mail address is well-formed
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                $email_error = "Only letters and white space allowed";
            }
        }


        if (empty($_POST["subject"])) {
            $subject_error = "Subject is required";
        } else {
            $subject = test_input($_POST["subject"]);
        }


        if (empty($_POST["website"])) {
            $website = " ";
        } else { 
            $website = test_input($_POST["website"]);
            if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
            $website_error = "Invalid URL"; 
            }
        }


        if (empty($_POST["message"])) {
            $message = " ";
        } else {
            $message = test_input($_POST["message"]);
          }


        if ($name_error == " " and $company_error == " " and $email_error == " " and $subject_error == " " and $website_error == " " and      $message_error == " " ) {
              $message_body = " ";
              unset($_POST["submit"]);
              foreach ($_POST as $key => $value){
                  $message_body .=  "$key: $value\n";
              }

              if (mail($to, $subject, $message, "From: ".$from)){
                  echo "Message sent, thank you for contacting us!";
              }
          }

    }

    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

?>

0 个答案:

没有答案