我猜我的某些代码有问题。 reCAPTCHA(v2)工作时,我无法发送消息(无错误代码)。如果删除所有与reCAPTCHA相关的代码,则PHP代码可以正常工作。
这是针对我的使用PHP和Apache2的Web服务器。我已经确认服务器和客户端密钥都可以。
index.html
文件的标题具有用于reCAPTCHA的此代码(以及其他代码):
<script src='https://www.google.com/recaptcha/api.js'></script>
index.html
的正文中是以下代码:
<div class="col-lg-6 col-sm-5 wow fadeInUp delay-05s">
<div class="form">
<div id="sendmessage">Your message has been sent. Thank you!</div>
<div id="errormessage"></div>
<form action="" method="post" role="form" class="contactForm">
<div class="form-group">
<input type="text" name="name" class="form-control input-text" id="name" placeholder="Your Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" />
<div class="validation"></div>
</div>
<div class="form-group">
<input type="email" class="form-control input-text" name="email" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid email" />
<div class="validation"></div>
</div>
<div class="form-group">
<input type="text" class="form-control input-text" name="subject" id="subject" placeholder="Subject" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />
<div class="validation"></div>
</div>
<div class="form-group">
<textarea class="form-control input-text text-area" name="message" rows="5" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea>
<div class="validation"></div>
</div>
<div class="g-recaptcha" data-sitekey="_______PUBLIC_KEY_______________"></div>
<div class="text-center"><button type="submit" class="input-btn">Send Message</button></div>
</form>
</div>
</div>
,此处是联系表的PHP代码:
<?php
// Checks if form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
function post_captcha($user_response) {
$fields_string = '';
$fields = array(
'secret' => '_______________PRIVATE_KEY_______________',
'response' => $user_response
);
foreach($fields as $key=>$value)
$fields_string .= $key . '=' . $value . '&';
$fields_string = rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, True);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
// Call the function post_captcha
$res = post_captcha($_POST['g-recaptcha-response']);
if (!$res['success']) {
// What happens when the CAPTCHA wasn't checked
echo '<p>Please go back and make sure you check the security CAPTCHA box.</p><br>';
} else {
// If CAPTCHA is successfully completed...
/*
PHP contact form script
Version: 1.1
Copyrights BootstrapMade.com
*/
/***************** Configuration *****************/
// Replace with your real receiving email address
$contact_email_to = "contact@gmail.com";
// Title prefixes
$subject_title = "Contat Form Message:";
$name_title = "Name:";
$email_title = "Email:";
$message_title = "Message:";
// Error messages
$contact_error_name = "Name is too short or empty!";
$contact_error_email = "Please enter a valid email!";
$contact_error_subject = "Subject is too short or empty!";
$contact_error_message = "Too short message! Please enter something.";
/********** Do not edit from the below line ***********/
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
die('Sorry Request must be Ajax POST');
}
if(isset($_POST)) {
$name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
$subject = filter_var($_POST["subject"], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
$message = filter_var($_POST["message"], FILTER_SANITIZE_STRING);
if(!$contact_email_to || $contact_email_to == 'contact@example.com') {
die('The contact form receiving email address is not configured!');
}
if(strlen($name)<3){
die($contact_error_name);
}
if(!$email){
die($contact_error_email);
}
if(strlen($subject)<3){
die($contact_error_subject);
}
if(strlen($message)<3){
die($contact_error_message);
}
if(!isset($contact_email_from)) {
$contact_email_from = "contactform@" . @preg_replace('/^www\./','', $_SERVER['SERVER_NAME']);
}
$headers = 'From: ' . $name . ' <' . $contact_email_from . '>' . PHP_EOL;
$headers .= 'Reply-To: ' . $email . PHP_EOL;
$headers .= 'MIME-Version: 1.0' . PHP_EOL;
$headers .= 'Content-Type: text/html; charset=UTF-8' . PHP_EOL;
$headers .= 'X-Mailer: PHP/' . phpversion();
$message_content = '<strong>' . $name_title . '</strong> ' . $name . '<br>';
$message_content .= '<strong>' . $email_title . '</strong> ' . $email . '<br>';
$message_content .= '<strong>' . $message_title . '</strong> ' . nl2br($message);
$sendemail = mail($contact_email_to, $subject_title . ' ' . $subject, $message_content, $headers);
if( $sendemail ) {
echo 'OK';
} else {
echo 'Could not send mail! Please check your PHP mail configuration.';
}
}
echo '<br><p>CAPTCHA was completed successfully!</p><br>';
}
}
?>