我使用GoDaddy作为我的网络服务器。
我正在尝试将reCAPTCHA实施到我的网站的电子邮件表单中,但似乎无法正常工作。也就是说,我通过了reCAPTCHA测试,但是没有发送电子邮件。
到目前为止,我只在网站上使用过PHP和HTML。
我还将脚本添加到我的头标记
<script src='https://www.google.com/recaptcha/api.js'></script>
以下是代码段。
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;
}?>
我是否实施服务器端错误?或者是客户端?
答案 0 :(得分:2)
您的代码在if和else中有语法错误。只需重写下面给出的代码并尝试一下即可。
<?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 ;
}
}
}
}
?>