我不知道为什么Google Recaptcha无法在我的PHP电子邮件表单中工作

时间:2019-03-29 13:00:32

标签: javascript php forms email recaptcha

我使用了以下逐步指导: http://acmeextension.com/integrate-google-recaptcha-with-php/

但是由于某种原因,recaptcha不会在网站上显示/显示吗?

我已经在页脚中使用以下命令调用了脚本:

<script src='https://www.google.com/recaptcha/api.js' async defer >

我尝试将PHP代码放在电子邮件表单的上方,但仍然无法正常工作。因此,我将其分离并放入单独的send_form_email.php页面。

这是我实施了recaptcha的电子邮件表格:

<form name="contactform" method="post" action="send_form_email.php" class="mb-0">
                <div class="row">
                    <div class="col-md-6">

                        <input type="text" class="form-control" name="first_name" maxlength="50" placeholder="First Name: *" required>
                    </div>
                    <div class="col-md-6">
                      <input  type="text" class="form-control" name="last_name" maxlength="50" size="30" placeholder="Last Name:">

                    </div>

                    <div class="col-md-6">

                    <input type="text" class="form-control" name="telephone" maxlength="50" placeholder="Phone:">
                    </div>

                    <div class="col-md-6">
                    <input type="email" class="form-control" name="email"  maxlength="80" placeholder="Email: *" required>

                    </div>
                    <div class="col-md-12">
                        <textarea class="form-control" name="comments" rows="3" maxlength="1000" placeholder="Message: *" required></textarea>
                    </div>
                    <div class="g-recaptcha col-md-12" data-sitekey="6LexIpoUAAAAAMMftZIWNkik8IqsLfZDCKQxO9XO"></div>
                    <div class="col-md-12">
                        <input type="submit" value="Send Now" name="Submit" class="btn btn--secondary btn--rounded">
                    </div>
                </div>
            </form>

这是sendemail php文件:

<?php

if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "[REMOVED]";
    $email_subject = "Emailed from Webform";

    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }


    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }



    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }

    $string_exp = "/^[A-Za-z .'-]+$/";

  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }

  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }

  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }

  if(strlen($error_message) > 0) {
    died($error_message);
  }


  if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response']))
  {
        $secret = '[REMOVED]';
        $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
        $responseData = json_decode($verifyResponse);
        if($responseData->success)
        {
            $succMsg = 'Your contact request have submitted successfully.';
        }
        else
        {
            $errMsg = 'Robot verification failed, please try again.';
        }
   }




    $email_message = "Form details below.\n\n";


    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }



    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- include your own success html here -->

<p>Thank you for contacting us. We will be in touch with you very soon.</P>
<?php

}
?>

它应该根据教程显示Recpatcha代码。实际上不是。

0 个答案:

没有答案