首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >带有recaptcha v3的phpmailer不发送电子邮件-未定义索引: g-recaptcha-response

带有recaptcha v3的phpmailer不发送电子邮件-未定义索引: g-recaptcha-response
EN

Stack Overflow用户
提问于 2018-07-10 03:31:13
回答 2查看 1.2K关注 0票数 0

我在用phpmailer实现google captcha v3时遇到了问题。当我填写表单并单击发送时。错误发生:注意:未定义索引:G-recaptcha-第6行form.php中的响应错误!验证码有问题,你欺骗了我们!你是个机器人!或者你只是没有点击它:)错误告诉我们错误在form.php so: JS文件的第6行

代码语言:javascript
复制
<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文件:

代码语言:javascript
复制
<?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文件:

代码语言:javascript
复制
    <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>
EN

回答 2

Stack Overflow用户

发布于 2018-07-10 07:24:39

这与PHPMailer无关--错误发生在任何PHPMailer代码运行之前。

您正在$_POST数组中查找一个名为g-recaptcha-response的值,但该值并不存在,因此出现了错误。您没有具有该名称的输入元素,这就是$_POST中缺少该元素的原因。它可能是google代码从JS动态添加的,但在您发布的内容中没有证据表明它是这样做的。我建议您多阅读recaptcha文档。

这里不是特别的问题,但是您运行的是一个非常旧的PHPMailer版本,并且您的代码基于一个过时的示例。

票数 0
EN

Stack Overflow用户

发布于 2018-08-02 21:21:34

您没有将令牌传递给请求。您必须在表单中设置令牌值。你可以做这样的事情。

代码语言:javascript
复制
 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 + '">');
     });
  });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51252814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档