Ajax / php联系表格不时发送空白电子邮件

时间:2017-04-14 17:46:33

标签: javascript php jquery ajax contact-form

好吧,我有这个发送空白电子邮件的联系表格。但是我做了一些测试,并没有发生在我身上。我认为,这可能发生的唯一方法是直接访问.php文件。如果不是,我不知道可能是什么问题。表单不允许您发送空白电子邮件。如果这种情况一直发生,我也会在php文件中添加验证,但在找到问题之前我不想忽略这些消息。

这是HTML

<form name="contactForm" id="contactForm" method="post" action="/contactEngine.php" onsubmit="return validateForm()">
        <input title="Input name" type="text" name="Name" id="Name" placeholder="Nombre:" required="">
        <input title="Input email" placeholder="Email:" type="email" name="Email" id="Email" required="">
        <input type="text" placeholder="Subject:" name="Subjet" id="Subjet">
        <textarea title="Input message" placeholder="Mensaje:" name="Message" rows="20" cols="20" id="Message" required=""></textarea>
        <input title="Input result" placeholder="25 + 25 = ?" type="text" name="Captcha" id="Captcha" required="">
        <p id="wrongCaptcha"> Try again </p> 
        <input type="submit" name="submit" value="send" class="submit-button"> 
</form>

这是JS

function validateForm(e) {
e.preventDefault();
var x = document.forms["contactForm"]["Captcha"].value; 
if (x != 50) {//if captcha is wrong
    $("#Captcha").addClass("wrongCaptchaEntered");
    $("#Captcha").css("animation-name" , "none");
    setTimeout (function(){
        $("#Captcha").css("animation-name" , "changeBorder");
    },100);
    if ($("#wrongCaptcha").css("display") == "none"){
        $("#wrongCaptcha").slideDown();
    }   
}
else {  //if captcha is correct 
    var formAction = $("#contactForm").attr("action");  
    if (formAction == "/contactEngine.php"){
        var formData = $("#contactForm").serialize();
        $.post( formAction, formData, function(data){
            console.log (data);
            $(".formulario").changeTo({content: "<h2 class='section-title BackgroundGradientBlack'>"+ data +"</h2>"});
        });
    }
}
return false;
}

和PHP

<?php
$EmailFrom = "EmailFrom@test.com";
$EmailTo = "EmailTo@test.com";
$Name = Trim(stripslashes($_POST['Name'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Subject = Trim(stripslashes($_POST['Subjet']));    
$Message = Trim(stripslashes($_POST['Message'])); 


$email_content = "Frontpage";
$email_content .= "\nNombre: $Name";
$email_content .= "\nEmail: $Email";
$email_content .= "\nMotivo: $Subject";
$email_content .= "\nMensaje: $Message";


$email_headers = "From: <$EmailFrom>";


if (mail($EmailTo, $Subject, $email_content, $email_headers)) {
    http_response_code(200);
    echo "Mensaje enviado";
} else {
    http_response_code(500);
    echo "Error";
}
?>

谢谢!

1 个答案:

答案 0 :(得分:0)

可能是一些机器人正在测试它可以在你的JS中看到的PHP端点并且正在向它发送数据,试图造成破坏。我敢打赌,如果你每次发送一封电子邮件都记录了$ _POST变量,你会在一些$ _POST变量中看到很多垃圾邮件。电子邮件是空白的,因为机器人不够聪明,无法知道在POST中使用哪些密钥。