Php 服务器端验证以避免空白垃圾邮件

Php 服务器端验证以避免空白垃圾邮件,php,Php,我使用phpmailer发送联系人表单提交。我收到许多垃圾邮件空白邮件,以避免垃圾邮件我试图添加服务器端验证,但不知何故它不起作用。 下面是我尝试的代码: form.php: <div class="form-box"> <h2 class="form-heading"> How Can We Help? </h2> <p class="form-subheading"> Request Your FREE Consult Below &

我使用phpmailer发送联系人表单提交。我收到许多垃圾邮件空白邮件,以避免垃圾邮件我试图添加服务器端验证,但不知何故它不起作用。 下面是我尝试的代码:

form.php:

     <div class="form-box">
<h2 class="form-heading"> How Can We Help? </h2>
<p class="form-subheading"> Request Your FREE Consult Below </p>

<form class="banner-form" action="banner_contact_process.php" method="post">
    <div class="banenr-field">
      <input type="text" id="Name" name="frmname" placeholder="Name *" required="">
      <p class='text-danger'><?php echo $errName; ?> </p>
    </div>
    <div class="banenr-field">
      <input type="email" id="Email" name="frmemail" placeholder="Email *" required="">
      <p class='text-danger'><?php echo $errEmail;?></p>
    </div>
    <div class="banenr-field">
      <input type="tel" id="Number" name="frmnumber" placeholder="Phone No *" required="">
      <p class='text-danger'> <?php echo $errNumber;?> </p>
    </div>
    <div class="banenr-field">
      <input type="text" id="Company" name="frmcompany" placeholder="Company">
     <!--  <p class='text-danger'><?php //echo $errCompany; ?> </p> -->
    </div>
    <div class="banenr-field-textare banenr-field">
      <input type="text" id="Message" name="frmmsg" placeholder="Message">
     <!--  <p class='text-danger'> <?php //echo $errMessage;?></p> -->
    </div>
    <div class="form-submit-btn">
        <input type="submit" name="submit" value="Submit">
    </div>
</form>
banner_contact_process.php:

 <?php
 //print_r($_POST);
 require 'phpmailer/PHPMailerAutoload.php';

 $errName ='<p></p>';
 $errEmail ='<p></p>';
 $errNumber ='<p></p>';
 $errCompany ='<p></p>';
 $errMessage ='<p></p>';

 if ($_SERVER["REQUEST_METHOD"] == "POST") {

// if(isset($_POST["submit"])){
$name = trim($_POST["frmname"]);
$email = trim($_POST["frmemail"]);
$number = trim($_POST["frmnumber"]);
$company = trim($_POST["frmcompany"]);
$message = trim($_POST["frmmsg"]);

// Check if name has been entered
if ($name == "") {
    $errName = 'Please enter your name';
    exit;
}

if ($email == "") {      
    $errEmail = 'Please enter your email address';      
}

if ($number == "") {
    $errNumber = 'Please enter your contact number';
    exit;
}

 if ($company == "") {
     $errCompany = 'Please enter your company name';
     exit;
 }


 // Check if message has been entered
 if ($message == "") {      
     $errMessage = 'Please enter your message';      
 }

$subject        = 'Enquiry submitted on Clicksplode website - home Bannner';
$userEmail      = 'poonam.katpara@atopdigital.com';
$mailcontent    = '<style type="text/css">
span{
font-family: Arial;
}
/* CLIENT-SPECIFIC STYLES */
body, table, td, a { -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; }
table, td { mso-table-lspace: 0pt; mso-table-rspace: 0pt; }
img { -ms-interpolation-mode: bicubic; }

 /* RESET STYLES */
img { border: 0; outline: none; text-decoration: none; }
 table { border-collapse: collapse !important; }
body { margin: 0 !important; padding: 0 !important; width: 100% !important; }

 /* iOS BLUE LINKS */
a[x-apple-data-detectors] {
color: inherit !important;
text-decoration: none !important;
font-size: inherit !important;
font-family: inherit !important;
font-weight: inherit !important;
line-height: inherit !important;
 }

/* ANDROID CENTER FIX */
 div[style*="margin: 16px 0;"] { margin: 0 !important; }

   .ReadMsgBody { width: 100%; background-color: #F1F1F1; }
    .ExternalClass { width: 100%; background-color: #F1F1F1; }
    body { width: 100%; background-color: #f6f6f6; margin: 0; padding: 0; -webkit-font-smoothing: antialiased; font-family: Arial, Times, serif }
    table { border-collapse: collapse !important; mso-table-lspace: 0pt; mso-table-rspace: 0pt; }
    *[class*="mobileOn"] { display: none !important; max-height: none !important; }

    @-ms-viewport{ width: device-width; }

    @media only screen and (max-width: 600px){
    .wrapper{ width:100%;  padding: 0 !important; }
    }    
    @media only screen and (max-width: 480px){ 
    .centerClass{ margin:0 auto !important; } 
    .imgClass{width:100% !important; height:auto;}    
    .wrapper{ width:320px;  padding: 0 !important; }      
    .container{ width:300px;  padding: 0 !important; }
    .mobile{ width:300px; display:block; padding: 0 !important; text-align:center; }
    .mobile50{ width:300px; padding: 0 !important; text-align:center; }
    *[class="mobileOff"] { width: 0px !important; display: none !important; }
    *[class*="mobileOn"] { display: block !important; max-height: none !important; }
    }
   </style> 
    <body style="background-color:#F2F2F2; font-family:Arial,serif;margin:0;padding:0;min-width: 100%; -webkit-text-size-adjust:none;-ms-text-size-adjust:none;">
   <center>
   <table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#F2F2F2">
    <tr>
        <td align="center" valign="top">
            <table width="600"  bgcolor="#fff" cellpadding="0" cellspacing="0" border="0" class="wrapper">
                <tr>
                    <td height="20" style="font-size:10px; line-height:10px;">&nbsp;</td>
                </tr>                    
                <tr>
                    <td align="top">

                        <table width="600" cellpadding="0" cellspacing="0" border="0" class="container">
                            <tr>
                                <td align="center" valign="top">
                                    <img src="/images/logo.png" width="30%" height="auto" style="margin:0px; padding:0; border:none; display:block;" border="0" class="imgClass" alt="">
                                </td>
                            </tr>
                        </table>

                    </td>
                </tr>
                <tr>
                    <td height="10" style="font-size:10px; line-height:10px;">&nbsp;</td>
                </tr>
            </table>   

            <table width="600" cellspacing="0" cellpadding="0" border="0" align="center" class="wrapper" bgcolor="#FFFFFF">
                <tr>
                    <td align="center" valign="top" >

                    <table width="560" cellspacing="0" cellpadding="0" border="0" align="center" class="container">
                        <tr>
                            <td align="left" valign="top" >
                                <span style="font-family: Arial;font-size:15px;color: rgb(35, 31, 32);font-weight: bold;">Enquiry submitted on Clicksplode website</span>
                            </td>
                        </tr>
                        <tr>
                            <td height="20" style="font-size:20px; line-height:20px;">&nbsp;</td>
                        </tr>
                        <tr>
                            <td align="left" valign="top" >
                                <span style="  font-family: Arial;display:block;font-size: 13px;color: rgb(35, 31, 32);line-height:25px;"> Please find details below:</span>
                            </td>
                        </tr>
                        <tr>
                            <td align="left" valign="top" >
                                <span style="  font-family: Arial;display:block;font-size: 13px;color: rgb(35, 31, 32);line-height:25px;"> Name : '.$name.'</span>
                            </td>
                        </tr>
                        <tr>
                            <td align="left" valign="top" >
                                <span style="  font-family: Arial;display:block;font-size: 13px;color: rgb(35, 31, 32);line-height:25px;"> Number : '.$number.'</span>
                            </td>
                        </tr>
                        <tr>
                            <td align="left" valign="top" >
                                <span style="  font-family: Arial;display:block;font-size: 13px;color: rgb(35, 31, 32);line-height:25px;"> Email : '.$email.'</span>
                            </td>
                        </tr>
                        <tr>
                            <td align="left" valign="top" >
                                <span style="  font-family: Arial;display:block;font-size: 13px;color: rgb(35, 31, 32);line-height:25px;"> Company : '.$company.'</span>
                            </td>
                        </tr>
                        <tr>
                            <td align="left" valign="top" >
                                <span style="  font-family: Arial;display:block;font-size: 13px;color: rgb(35, 31, 32);line-height:25px;"> Message : '.$message.'</span>
                            </td>
                        </tr>
                        <tr>
                            <td height="40" style="font-size:40px; line-height:40px;">&nbsp;</td>
                        </tr>
                    </table>

                    </td>
                </tr>
            </table>

            <table width="600" cellpadding="0" cellspacing="0" border="0" class="wrapper" bgcolor="#FFFFFF">
                <tr>
                    <td height="10" style="font-size:10px; line-height:10px;">&nbsp;</td>
                </tr>
                <tr>
                    <td align="center" valign="top">
                        <table width="600" cellpadding="0" cellspacing="0" border="0" class="container">
                            <tr>
                                <td align="center" valign="top">
                                    <img src="images/Welcome_Emailer_qafiyati.jpg" width="100%" height="" style="margin:0; padding:0; border:none; display:block;" border="0" alt="" /> 
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </td>
        </tr>
    </table>
    </center>
         </body>';



   $mail            = new PHPMailer;

   $mail->setFrom('test@gmail.com', 'Webmaster');
   $mail->addAddress($userEmail);
   //Set the subject line
   $mail->Subject   = $subject;
  //Read an HTML message body from an external file, convert referenced images to embedded,
 //convert HTML into a basic plain-text alternative body
  $mail->msgHTML($mailcontent);
  //Replace the plain text body with one created manually
  $mail->AltBody    = 'Enquiry!';

   if(!$mail->send()) 
   {
  echo "Mailer Error: " . $mail->ErrorInfo;

  }else{
  // echo "success";
  //------------------- SEND SMS --------------------------
 }

header("Location:thank-you.php");

 }
 ?>
我已打印数组,显示插入的正确值。 请帮帮我。我被卡住了。
提前感谢。

如果电子邮件有效,请与PHP联系:

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  $errEmail = "Invalid email format";
  exit;
}
增加出口;此外,信息:

 if ($message == "") {      
     $errMessage = 'Please enter your message';   
     exit;   
 }
您也可以这样做:

$error=0;
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  $errEmail = "Invalid email format";
  $error=1;
}

//List here all other check too. .. Example:
/*
if(some validation isbad){
  $errorText = "Something is not validated";
  $error=1; 
}
*/

if($error==0){
    //send mail
    $mail            = new PHPMailer;

    $mail->setFrom('test@gmail.com', 'Webmaster');
    $mail->addAddress($userEmail);
    //Set the subject line
    $mail->Subject   = $subject;
    //Read an HTML message body from an external file, convert referenced images to embedded,
    //convert HTML into a basic plain-text alternative body
    $mail->msgHTML($mailcontent);
    //Replace the plain text body with one created manually
    $mail->AltBody    = 'Enquiry!';

    if(!$mail->send()){
        echo "Mailer Error: " . $mail->ErrorInfo;
    }else{
        // echo "success";
        //------------------- SEND SMS --------------------------
    }   
}

在表单中添加验证码。它可以是简单的,也可以是复杂的。您可以添加reCaptcha,甚至可以自己滚动

通常,在显示表单之前,可以生成问题并将答案存储在会话中。然后让他们输入答案。发布后,检查会话中存储的值是否匹配


不过,更简单的是,您还可以将required添加到消息表单输入中,以确保它不是空的,这是您最初的查询。但是您仍然应该使用某种验证码。

消息可能包含一些带有hiiden等属性的html,或者一些不可打印的字符,因此您不能在修剪后仅检查消息是否为空。您的验证似乎没有任何用处-您设置了$err..='。。。有时给出口打电话,有时不打。如果你退出-对你设置的邮件不做任何处理,但如果你不退出,它只发送电子邮件。我不想使用验证码。我想学习服务器端脚本。CAPTCHA工作在服务器端,你需要用PHP编写脚本。如果你想学习,试试这个!我同意验证码在这里是件好事。OP可以使用不可见的recaptha,所以用户不需要填充它。。剩下的呢?@tejal为消息添加出口。。但我建议退出并在出现错误时重定向。。