验证电子邮件表单

时间:2016-06-23 11:18:14

标签: php email-validation

在表单中强制使用名称和电子邮件字段的最佳解决方案是什么?

似乎找不到我的表单的直接解决方案,也没有专家。

我目前收到大量空白电子邮件。

HTML代码

<form name="enq" method="post" action="mail/myphpfile.php" onSubmit="return validation();">
  <fieldset>
    <input type="text" name="name" id="name" value=""  class="input-block-level" placeholder="Name" />
    <input type="text" name="email" id="email" value="" class="input-block-level" placeholder="Email" />
    <textarea rows="11" name="message" id="message" class="input-block-level" placeholder="Comments"></textarea>
    <div class="actions">
      <input type="submit" value="Send Your Message" name="submit" id="submitButton" class="btn btn-info pull-right" title="Click here to submit your message!" />
    </div>
  </fieldset>
</form>

PHP代码

<?php
if(isset($_POST['submit']))
{
    $name = $_POST['name'];
    $email = $_POST['email'];
    $query = $_POST['message'];
    $email_from = $name.'<'.$email.'>';

    $to="name@myemailaddress.co.za";
    $subject="Enquiry from my website";
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= "From: ".$email_from."\r\n" . "BCC:email@email1.co.za,      email@email2.co.za, email@email3.co.za\r\n";
    $message="    
     Name: $name       
     <br>
     Email-Id:$email       
     <br>
     Message:$query";

    if(mail($to,$subject,$message,$headers))
            header("Location:../thank-you.html");
    else
        header("Location:../error.html");
        //contact:-email@myemail.co.za
}
?>

4 个答案:

答案 0 :(得分:1)

尝试

  <?php
 if(isset($_POST['submit']) && !empty($_POST['email']) && !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false)
{
$name = $_POST['name'];
$email = $_POST['email'];
$query = $_POST['message'];
$email_from = $name.'<'.$email.'>';

$to="name@myemailaddress.co.za";
$subject="Enquiry from my website";
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: ".$email_from."\r\n" . "BCC:email@email1.co.za,      email@email2.co.za, email@email3.co.za\r\n";
$message="Name: $name <br>
 Email-Id:$emai<br>
 Message:$query ";

if(mail($to,$subject,$message,$headers))
 header("Location:../thank-you.html");
 else
  header("Location:../error.html");
 //contact:-email@myemail.co.za
 }

答案 1 :(得分:0)

使用html5这样的验证

<input type="text" name="name" id="name" value=""  class="input-block-level" placeholder="Name" required/>

<input type="email" name="name" id="name" value=""  class="input-block-level" placeholder="Name" required/>

答案 2 :(得分:0)

在HTML5中,这应该可以正常工作!

<input type="text" name="name" id="name" value=""  class="input-block-level" placeholder="Name" required/>

<input type="email" name="emailField" id="name" value=""  class="input-block-level" placeholder="Email" required/>

答案 3 :(得分:0)

try this!!

if(isset($_POST['submit']))
{
    $email = test_input($_POST["email"]);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL) && $email =="" ) {
        $emailErr = "Enter valid email."; 
    }
    else
    {
        $emailErr = "";
    }

    if( $emailErr==""){
        mail($to,$subject,$message,$headers);
        header("Location:../thank-you.html");

    }
    else
    {
        echo $emailErr;
       header("Location:../error.html");
    }
}