通过reCAPTCHA验证发送电子邮件

时间:2019-08-06 00:26:17

标签: php recaptcha

reCAPTCHA验证后,我正尝试通过php发送我的网站表单的电子邮件。单击提交按钮后页面会刷新,但是不会发送电子邮件。我想我缺少一些简单的东西,但是我尝试过的东西没有用。

这一切都在localhost上完成,并且电子邮件发送正常,没有reCAPTCHA代码。抱歉,对于这一切我还是陌生的。

HTML片段:

<form class="contact100-form validate-form" action="#" method="post">
                            <span class="contact100-form-title">
                                Request a demo today!
                            </span>

                            <div class="wrap-input100 validate-input">
                                <input class="input100" type="text" required name="name" placeholder="Full Name">
                                <span class="focus-input100"></span>
                            </div>

                            <div class="wrap-input100 validate-input">
                                <input class="input100" type="text" required name="email" placeholder="E-mail">
                                <span class="focus-input100"></span>
                            </div>
<div class="g-recaptcha" data-sitekey="MY KEY"></div>
</form>

PHP代码段:

<?php 
          /* [VERIFY CAPTCHA FIRST] */
            $secret = 'SECRET KEY'; // CHANGE THIS TO YOUR OWN!
            $url = "https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=".$_POST['g-recaptcha-response'];
            $verify = json_decode(file_get_contents($url));


          if($verify -> success)
          {
              $to = "email@email.com"; // this is your Email address
              $subject = "SUBJECT NAME";
              $email = $_POST['email];
              $name = $_POST['name'];


              $message = $name .  " wrote the following:" . "\n\n" . $_POST['message'] ."\n\n" . "Contact details: " . $from . "\n\n" . "Company Name: " . $company . "\n\n" . "Company Industry: " . $industry . "\n\n" . "Company Size: " . $size . "\n\n" . "Phone: " . $phone . "\n\n" . "Location: " . $location;

              $headers = "From:" . $from;
              $headers2 = "From:" . $to;
              mail($to,$subject,$message,$headers);
              mail($from,$headers2); // sends a copy of the message to the sender
              // echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";
              // You can also use header('Location: thank_you.php'); to redirect to another page.

          }
?>

1 个答案:

答案 0 :(得分:0)

您错过了$email = $_POST['email];上的报价

应为$email = $_POST['email'];