开发者

ReCaptcha doesn't accept any string

开发者 https://www.devze.com 2023-03-05 11:46 出处:网络
I\'m trying to add recaptcha to a contact form for the first time, and I\'ve made some progress, but now I\'m a little stuck.

I'm trying to add recaptcha to a contact form for the first time, and I've made some progress, but now I'm a little stuck.

It doesn't let me send any e-mail, and I can't find out why. Before I had the problem that recaptcha didn't stop false answers, but after I put the recaptcha code on top of the form it now shows itself and protects the form.

Here's the code that does:

<?php

        require_once('recaptchalib.php');
        $privatekey = "xxxxxxxxxxxxxxxxxxxxxx";
        $resp = recaptcha_check_answer ($privatekey,
                                    $_SERVER["REMOTE_ADDR"],
                                    $_POST["recaptcha_challenge_field"],
                                    $_POST["recaptcha_response_field"]);

        if (!$resp->is_valid) {
            // What happens when the CAPTCHA was entered incorrectly
            die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
             "(reCAPTCHA said: " . $resp->error . ")");
        } else {
        // Your code here to handle a successful verification
        if(@mail($your_email,$email_subject,$email_content)) {
            echo 'Message sent!'; }
          }

     if(!$_POST) exit;

     $email = $_POST['email'];


        //$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i',     $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS';
        if(!eregi("@",$email )){
           $error.="Invalid email address entered";
           $errors=1;
        }
        if($errors==1) echo $error;
      else{
             $values = array ('name','email','telephone','message');
             $required = array('name','email','telephone','message'); 
             $your_email = "email@mydomain.com";
             $email_subject = "New Message from XXX: ".$_POST['subject'];
             $email_content = "New message:\n";

           foreach($values as $key => $value){
             if(in_array($value,$required)){
               if ($key != 'subject' && $key != 'telephone') {
                 if( empty($_POST[$value]) ) { 
                   echo 'PLEASE FILL IN REQUIRED FIELDS'; exit;
                 }
               }
                 $email_content .= $value.': '.$_POST[$value]."\n";
             }
           }

        if(@mail($your_email,$email_subject,$email_content)) {
            echo 'Message sent!'; 
        }
        else {
            echo 'ERROR!';
        }
     }
?>

And here's the code from the form:

 <form action="./contact.php" method="post" id="contactform">
        <?php
          require_once('./recaptchalib.php');
          $publickey = "xxxxxxxxxxxxxxxxxx"; // you got this from the signup page
          echo recaptcha_get_html($publickey);
        ?>
          <ol>
            <li>
              <label for="name">Name</label>
              <input id="name" name="name" class="text" />
            </li>
            <li>
              <label for="email">Email</label>
              <input id="email" name="email" class="text" />
            </li>
            <li>
              <label for="telephone">Telephone</label>
              <input id="telephone" name="telephone" class="text" />
            </li>
            <li>
              <label for="message">Message</label>
              <textarea id="message" name="message" rows="6" cols="50"></textarea>
            </li>

            <li class="buttons">
              <input type="image" name="imageField" id="imageField" src="images/send.gif" cl开发者_如何学Goass="send" />
              <div class="clr"></div>
            </li>
          </ol>

 </form>

Thanks for your help!


What does "doesn't let me send mail" mean? Is the captcha failing and you get the "The recaptcha wasn't entered correctly"?

Or does it try to send the mail but nothing ever shows up?

  1. Don't use eregi. The entire set of ereg functions is deprecated. I see you have a preg_match equivalent right above, so use that instead. And if you're on PHP 5.2+, you should use filter_var($email, FILTER_VALIDATE_EMAIL) instead, which will do a better job than your regexe.
  2. Don't supress the mail() call with @. Mail by itself won't output any error messages, it'll simply return true/false. Check that return value and output an error message if it's false. At present, you're only outputting if it's true.
  3. Check your SMTP server logs, if you have access to it, on the presumption that PHP and mail are actually working and trying to hand off the mail. Maybe there's a glitch with the SMTP server and mails are stuck in the outgoing queue

And after all that, it would appear that you're trying to send the mail with $your_email, $email_subject, etc... BEFORE you've set those variables and retrieved the values from $_POST. So most likely you're calling mail() with empty variables, which does nothing.

0

精彩评论

暂无评论...
验证码 换一张
取 消