问题
I'm using GoDaddy as my web server.
I'm trying to implement reCAPTCHA into my email form for my website, however it seems to not be able to work. That is, I pass the reCAPTCHA test but the email isn't sent.
I've only used PHP and HTML for the site so far.
I have also added the script to my head tag
<script src='https://www.google.com/recaptcha/api.js'></script>
Here are the code snippets.
HTML:
<form action="#" id="form" method="post" name="form">
<div class="form-group">
<label for="name-field">Name</label>
<input name="vname" class="form-control" placeholder="Enter Your Full Name" type="text" value="">
</div>
<div class="form-group">
<label for="email-field">Email address</label>
<input name="vemail" class="form-control" placeholder="Enter Your Email" type="text" value="">
</div>
<div class="form-group">
<label for="contract-field">Enter Contract Here</label>
<textarea name="msg" class="form-control" placeholder="Enter Contract Here" rows="5"></textarea>
</div>
<div class="g-recaptcha form-group" data-sitekey="SITEKEY"></div>
<div class="form-group"><button id="send" name="submit" type="submit" class="btn btn-default">Submit</button></div>
</form>
PHP:
<?php
//Checking For reCAPTCHA
$captcha;
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
// Checking For correct reCAPTCHA
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SECRETKEY&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false){
echo "Your CAPTCHA response was wrong."
exit;
}
else{
// Checking For Blank Fields..
if($_POST["vname"]==""||$_POST["vemail"]==""||$_POST["msg"]==""){
echo "Fill All Fields..";
}
else{
// Check if the "Sender's Email" input field is filled out
$email=$_POST['vemail'];
// Sanitize E-mail Address
$email =filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate E-mail Address
$email= filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email){
echo "Invalid Sender's Email";
}
else{
$to = 'johdoe@example.com';
$subject = 'Test';
$message = $_POST['msg'];
$headers = 'From:'. $email . "\r\n"; // Sender's Email
// Message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70, "\r\n");
// Send Mail By PHP Mail Function
mail($to, $subject, $message, $headers);
echo "Your mail has been sent successfully!";
}
}
} else {
echo "Your CAPTCHA response was wrong. Try again."
exit;
}?>
Am I implementing server-side wrong? Or is it client side?
回答1:
Your code has syntax errors in if and else. Just rewrite your code as given below and try it.
<?php
//Checking For reCAPTCHA
$captcha;
if (isset($_POST['g-recaptcha-response'])) {
$captcha = $_POST['g-recaptcha-response'];
}
// Checking For correct reCAPTCHA
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SECRETKEY&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
if (!$captcha || $response.success == false) {
echo "Your CAPTCHA response was wrong.";
exit ;
} else {
// Checking For Blank Fields..
if ($_POST["vname"] == "" || $_POST["vemail"] == "" || $_POST["msg"] == "") {
echo "Fill All Fields..";
} else {
// Check if the "Sender's Email" input field is filled out
$email = $_POST['vemail'];
// Sanitize E-mail Address
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate E-mail Address
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email) {
echo "Invalid Sender's Email";
} else {
$to = 'johdoe@example.com';
$subject = 'Test';
$message = $_POST['msg'];
$headers = 'From:' . $email . "\r\n";
// Sender's Email
// Message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70, "\r\n");
// Send Mail By PHP Mail Function
if (mail($to, $subject, $message, $headers)) {
echo "Your mail has been sent successfully!";
} else {
echo "Failed to send email, try again.";
exit ;
}
}
}
}
?>
来源:https://stackoverflow.com/questions/28273491/recaptcha-and-php-to-send-an-email-form