PHPMailer表单发送空字段值

时间:2014-10-01 18:01:38

标签: javascript php html forms 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  = 'the@adminemail.com';
$mail->FromName = 'The Admin <the@adminemail.com>';
$mail->AddAddress("the@adminemail.com");

// 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  = 'the@adminemail.com';
$mail->FromName = "The Admin <the@adminemail.com>";
$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>';
    }

}


?>

1 个答案:

答案 0 :(得分: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 

}

  • 将此逻辑应用于您的代码将有助于减少(如果不是消除空字段)。

相关问题