Javascript 在PHP中添加Google重新验证码表单;联系我们表格“;
我是一个网络开发新手,我遇到了一个联系人表单的问题,我试图在表单上附加一个验证码字段以防止垃圾邮件 在添加验证码表单之前,代码有效,并且发送了电子邮件。在添加验证码验证之后,JS文件显示一条成功确认消息,但电子邮件没有发送 详细代码如下: HTML:Javascript 在PHP中添加Google重新验证码表单;联系我们表格“;,javascript,php,forms,twitter-bootstrap-3,Javascript,Php,Forms,Twitter Bootstrap 3,我是一个网络开发新手,我遇到了一个联系人表单的问题,我试图在表单上附加一个验证码字段以防止垃圾邮件 在添加验证码表单之前,代码有效,并且发送了电子邮件。在添加验证码验证之后,JS文件显示一条成功确认消息,但电子邮件没有发送 详细代码如下: HTML: <form name="sentMessage" id="contactForm" novalidate> <div class="row control-group">
<form name="sentMessage" id="contactForm" novalidate>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
//rest of the inputs
<div class="form-group">
<div class="g-recaptcha" data-sitekey="site_key_here"></div>
<span class="help-block" style="display: none;">Please check that you are not a robot.</span>
</div>
<br>
<div id="success"></div>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-success btn-lg" id="submit">Send</button>
</div>
</div>
</form>
<?php
function errorResponse ($messsage) {
header('HTTP/1.1 500 Internal Server Error');
return false;
}
// Check for empty fields
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['phone']) || empty($_POST['message']) || !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
// Create the email and send the message
$to = '_email_here_';
$email_subject = "Contact Form from website: $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply@yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";
//MY CODE STARTS HERE
// getting the captcha
$captcha = "";
if (isset($_POST["g-recaptcha-response"])) {
$captcha = $_POST["g-recaptcha-response"];
}
else {
return false;
}
// handling the captcha and checking if it's ok
$secret = "_secret_key_here_";
$google_url = "https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$captcha."&remoteip=".$_SERVER["REMOTE_ADDR"];
$google_response = file_get_contents($google_url);
$response = json_decode($google_response, true);
// if the captcha is cleared with google, send the mail and return.
if ($response["success"] != false) {
// send the actual mail
mail($to,$email_subject,$email_body,$headers);
// return goes back to the ajax, so the user can know if everything is ok
return true;
} else {
return false;
}
//AND ENDS HERE
?>
在这里找到了解决方案:
主题已结束。在此处找到了解决方案:
主题结束。问题是什么?电子邮件没有发送。我可能没有注意到这一点,用
@mail()
和@mail($to,$email\u subject,$email\u body,$headers)代码>不起作用:(使用“@”实际上不是一个好的做法。但它对于一些必要的错误处理是有用的。问题是什么?电子邮件没有发送。我可能错过了这一点,请尝试使用@mail()
和@mail($to,$email\u subject,$email\u body,$headers);
不起作用:(使用“@”实际上不是一个好的做法。但是它对于一些必要的错误处理非常有用。
mail($to,$email_subject,$email_body,$headers);
return true;