[英]phpmailer with recaptcha v3 doesn't send email - Undefined index: g-recaptcha-response
I have problem with implementing google captcha v3 with phpmailer. 我在用phpmailer实现Google验证码v3时遇到问题。 When i fill a form and click send.
当我填写表格并单击发送时。 Error occur : Notice: Undefined index: g-recaptcha-response in form.php on line 6 Error!There was a problem with the Captcha, you lied to us!
发生错误:注意:未定义索引:第6行的form.php中的g-recaptcha-response错误!验证码有问题,您对我们撒谎! you are a robot!
你是机器人! or you just didnt click it :) The error tells that error is in line 6 in form.php so: JS file
或您只是没有单击它:)错误告诉您错误在form.php的第6行中,因此:JS文件
<script src='https://www.google.com/recaptcha/api.js?
render=6LemuWIUAAAAAATQOAxYz-30Uf8VbXery0I9J8ZA'></script>
<script>
grecaptcha.ready(function () {
grecaptcha.execute('6LemuWIUAAAAAATQOAxYz-30Uf8VbXery0I9J8ZA', {
action: 'form'
})
});
</script>
PHP file: PHP文件:
<?php
date_default_timezone_set('Etc/UTC');
ini_set('display_errors',1); error_reporting(E_ALL);
if(isset($_POST['submit'])){
$userIP = $_SERVER["REMOTE_ADDR"];
$recaptchaResponse = $_POST['g-recaptcha-response'];
$secretKey = 'abc...';
$request = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secretKey}&response={$recaptchaResponse}&remoteip={$userIP}");
if(!strstr($request, "true")){
echo '<div class="alert alert-danger" role="alert"><strong>Error!</strong>There was a problem with the Captcha, you lied to us! you are a robot! or you just didnt click it :)</div>';
}
else{
if(isset($_POST['submit']))
{
$message=
'name: '.$_POST['name'].'<br />
email: '.$_POST['email'].'<br />
mess: '.$_POST['message'].'
';
require "class.phpmailer.php";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "abc...";
$mail->Port = 465;
$mail->Encoding = '7bit';
$mail->SMTPDebug = 3;
$mail->Username = 'a@a.pl';
$mail->Password = 'abc';
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->WordWrap = 50;
$mail->MsgHTML($message);
$mail->AddAddress('a@a.com');
$result = $mail->Send();
$message = $result ? '<div class="alert alert-success" role="alert">
<strong>Success!</strong>Message Sent Successfully!</div>' : '<div
class="alert
alert-danger" role="alert"><strong>Error!</strong>There was a problem
delivering the message.</div>';
unset($mail);
}
}
}
HTML file: HTML档案:
<form action="form.php" method="POST" class="contact__form g-recaptcha"
id="form" data-sitekey="6LdvpmIUAAAAABEO5KGWX1KsIJgPQnZyAJep4lkw">
<div class="form__div">
<label for="name">Name </label>
<input type="text" name="name" id="name" class="input input__name" required/>
</div>
<div class="form__div">
<label for="email">E-mail </label>
<input type="email" name="email" id="email" class="input input__email " required/>
</div>
<div class="form__div">
<label>Message </label>
<textarea rows="5" aria-label="Write something" name="message" class="input input__mess" placeholder="Write..." minlength="10" maxlength="1000" required></textarea>
</div>
<button name="submit" type="submit" id="submit" class="form__button">Wyślij</button>
</form>
This has nothing to do with PHPMailer - that error occurs before any PHPMailer code is run. 这与PHPMailer无关-在运行任何PHPMailer代码之前都会发生该错误。
You're looking for a value in the $_POST
array called g-recaptcha-response
, but that does not exist, hence the error. 您正在
$_POST
数组中寻找一个名为g-recaptcha-response
,但该值不存在,因此会出现错误。 You do not have an input element with that name, which is why it's missing from $_POST
. 您没有具有该名称的input元素,这就是为什么
$_POST
缺少该元素。 It may be that the google code adds it dynamically from JS, but there's no evidence of it doing that in what you have posted. 可能是Google代码从JS动态添加了它,但是在您发布的内容中没有证据表明这样做。 I'd suggest you read the recaptcha docs more.
我建议您多阅读recaptcha文档。
Not specifically a problem here, but you're running a very old version of PHPMailer, and have based your code on an obsolete example. 这里不是一个特别的问题,但是您正在运行一个非常旧的PHPMailer版本,并且您的代码基于一个过时的示例。
You are not passing the token to request. 您没有将令牌传递给请求。 You have to set token value in the form.
您必须在表单中设置令牌值。 You can do something like this.
你可以做这样的事情。
grecaptcha.ready(function() {
grecaptcha.execute("KEY_VALUE", {action: 'form'})
.then(function(token) {
localStorage.setItem('recaptcha_token', token)
$('form').prepend('<input type="hidden" name="g-recaptcha-response" value="' + token + '">');
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.