2014-10-01 55 views
1

這已經持​​續了一段時間,我似乎無法理解爲什麼。我已經構建了一個使用PHPMailer發送的表單,無論出於什麼原因,我們都會持續從表單中以空白字段值的形式獲取電子郵件。這種情況發生在不同網站上的多種表單上,全部使用相同的設置。所以它不限於這種特定的形式。PHPMailer表單發送空字段值

空白表單發送到一邊,表單在測試時確實工作正常。如果我填寫併發送測試,一切都會好起來的。我不確定這些空白提交是如何發生的。該表單在使用javascript的大多數字段上都有驗證。

這裏是我的HTML代碼:

    <div class="thank-you-message"></div> 

       <form id="contactForm" class="form-horizontal" action="" method="post"> 


        <!-- col 1 --> 
        <div class="form-l"> 

        <div class="control-group"> 

         <div class="controls"> 
          <input type="text" name="first-name" id="first-name" class="validate[required,custom[onlyLetterSp],length[0,100]]" placeholder="first name" /> 
         </div> 

        </div><!-- /.control-group --> 

        <div class="control-group"> 

         <div class="controls"> 
          <input type="text" name="last-name" id="last-name" class="validate[required,custom[onlyLetterSp],length[0,100]]" placeholder="last name" /> 
         </div> 

        </div><!-- /.control-group --> 

        <div class="control-group"> 

         <div class="controls"> 
          <input type="text" name="email" id="email" class="validate[required,custom[email]]" placeholder="email address" /> 
         </div> 

        </div><!-- /.control-group --> 

        <div class="control-group"> 

         <div class="controls"> 
          <input class="last" type="text" name="phone" id="phone" class="validate[required,custom[phone]]" placeholder="phone number" /> 
         </div> 

        </div><!-- /.control-group --> 

        </div> 
        <!--../ col 1 --> 
        <!-- col 2 -->   
        <div class="form-r">   

        <div class="control-group"> 

         <div class="controls"> 
          <textarea rows="5" name="comments" id="comments" placeholder="message"></textarea> 
         </div> 

        </div><!-- /.control-group --> 

        <div class="control-group"> 

         <div class="controls"> 
          <button type="submit" class="submit"><span>send</span></button> 
         </div> 

        </div><!-- /.control-group --> 
        </div> 
        <!--../ col 1 --> 

       </form> 

這裏是PHPMailer的代碼

<?php 

// REQUIRE PHPMAILER CLASS 
// ----------------------------------------------------------------------------- 

require_once("PHPMailerAutoload.php"); 
$mail = new PHPMailer(); 


// SET POST VARIABLES 
// ----------------------------------------------------------------------------- 

$visitorEmail=filter_var($_POST['email']); 
$name=filter_var($_POST['first-name'].' '.$_POST['last-name']); 
$phone=filter_var($_POST['phone']); 
$comments=filter_var($_POST['comments']); 



// TO ADMIN EMAIL 
// ----------------------------------------------------------------------------- 

// Mail Headers 
$mail->IsHTML(true); // Send as HTML 

$mail->AddReplyTo($visitorEmail, $name); 
$mail->From = '[email protected]'; 
$mail->FromName = 'The Admin <[email protected]>'; 
$mail->AddAddress("[email protected]"); 

// Mail Subject 
$mail->Subject = "The Web Form"; 

// Mail Body 
$mail->Body  = '<html><body> 
            <img src="http://url.to/image.jpg" alt="Thank you for contacting us!" /> 
            <p>Someone has submitted your form.</p> 
            <table rules="all" style="border-color: #666;" cellpadding="10"> 
            <tr style="background: #eee;"><td><strong>Sent From:</strong> </td><td>The Web Form</td></tr> 
            <tr><td><strong>Name:</strong> </td><td>' .$name. '</td></tr> 
            <tr><td><strong>Email:</strong> </td><td>' .$visitorEmail. '</td></tr> 
            <tr><td><strong>Phone:</strong> </td><td>' .$phone. '</td></tr> 
            <tr><td><strong>Comments:</strong> </td><td>' .$comments. '</td></tr> 
            </table> 
            </body></html>'; 

$mail->Send(); // Send mail to admin 
$mail->ClearAddresses(); // Clear all addresses and attachments for next loop       


// TO VISITOR EMAIL 
// ----------------------------------------------------------------------------- 

// Mail Headers 
$mail->IsHTML(true); // Send as HTML 

$mail->From = '[email protected]'; 
$mail->FromName = "The Admin <[email protected]>"; 
$mail->AddAddress($visitorEmail); 

// Mail Subject 
$mail->Subject = "Thank you for contacting us"; 

// Mail Body 
$mail->Body  = '<html><body> 
            <img src="http://url.to/image.jpg" alt="Thank you for contacting us!" /> 
            <p>Hello,</p> 
            <p>Thanks for contacting us. We have received your message and will get in touch with you shortly.</p> 
            <p>A copy of the information you provided us with has been posted below:</p> 
            <table rules="all" style="border-color: #666;" cellpadding="10"> 
            <tr style="background: #eee;"><td><strong>Name:</strong> </td><td>' .$name. '</td></tr> 
            <tr><td><strong>Email:</strong> </td><td>' .$visitorEmail. '</td></tr> 
            <tr><td><strong>Phone:</strong> </td><td>' .$phone. '</td></tr> 
            <tr><td><strong>Comments:</strong> </td><td>' .$comments. '</td></tr> 
            </table> 
            </body></html>'; 


// THANK YOU MESSAGE 
// ----------------------------------------------------------------------------- 

if($visitorEmail == '') { 
    // If email field is blank, and validation doesn't work 
    echo '<h2>Error</h2><p>An error has occurred. Please reload the page and try again. Thanks!</p>'; 
} else { 

    if(!$mail->Send()) { 
     // If mail fails to send 
     echo '<h2><strong>Message was not sent.</strong></h2>'; 
     echo '<p>Mailer error: ' . $mail->ErrorInfo . '</p>'; 
    } else { 
     // Success 
     echo '<h2>thanks for contacting us</h2> <p>We will get back to you as soon as possible.</p> 
     <p>If you have any immediate questions please give us a call. </p>'; 
    } 

} 


?> 
+0

製作使用'空()'和'isset的()' – 2014-10-01 18:03:09

+0

也許這是提交表單的垃圾郵件機器人是空的? – 2014-10-01 18:04:23

+1

你的phpMailer代碼是一個單獨的文件嗎?你從不檢查你的值是否設置/不爲空,即。 - >'if(!isset($ _ POST ['email'])|| empty($ _ POST ['email']))die('您沒有提供電子郵件');'。如果直接訪問此頁面,但不發佈表單,則字段值將爲空。 – Sean 2014-10-01 18:08:46

回答

4

正如評論指出,這很可能是垃圾郵件的機器人,還人們訪問你的網站,根本不輸入任何東西。這是已知的事情發生。

盡我所知,你只是檢查if($visitorEmail == '')等於什麼。

這是最好的使用條件報表表單域,使用PHP的

即:使用|| - OR運營商(如果一個或另一個是空的)

if(!empty($_POST['form_field']) || !empty($_POST['other_form_field'])) 

    { 

// we are good to go, fire up your PHP/mailing code 

} 

else { 

    // do something else 

} 

或(使用&& - AND操作者) - (如果不是空的,並且被設置)

if(!empty($_POST['form_field']) && isset($_POST['form_field'])) 

    { 

// we are good to go, fire up your PHP/mailing code 

} 

else { 

    // do something else 

} 

  • 運用這一邏輯到你的代碼將幫助你減少,如果不能消除空字段。

+1

太棒了,非常感謝!我相信這應該有助於解決問題。如果沒有,將實施和跟進,但我認爲它應該起作用。 – norsewulf 2014-10-02 18:32:47

+0

@norsewulf你非常歡迎,很高興得到了幫助,*歡呼* – 2014-10-02 18:33:13