【问题标题】:Ajax/php contact form sending blank emails from time to timeAjax/php 联系表单不时发送空白电子邮件
【发布时间】:2017-04-14 17:46:32
【问题描述】:

我有这个发送空白电子邮件的联系表。但是我做了一些测试,但它并没有发生在我身上。我认为,发生这种情况的唯一方法是直接访问 .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";
}
?>

谢谢!

【问题讨论】:

    标签: javascript php jquery ajax contact-form


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-29
      • 1970-01-01
      • 2019-09-04
      • 1970-01-01
      • 2017-09-13
      • 1970-01-01
      相关资源
      最近更新 更多