0

I have a php contact form which has been working reliably for several years up until Jan 2022. It uses a Google reCaptcha. Apparently it fails at the mailcheck part which is near the end and prints out the "MAIL Sending Failed" message

<?php
//dont need these
error_reporting(E_ALL);
ini_set('display_errors', 1);

// check form is submitted
if ( isset($_POST['form_submit']) ) {

    // get values
    $error = ''; //error variable
    $visitor_name    = $_POST['name'];
    $visitor_email   = $_POST['email'];
    $visitor_message = $_POST['message'];
    $captcha         = $_POST['g-recaptcha-response'];

    //required values
    if ( empty($visitor_name) || empty($visitor_email) ) {
        $error = "<h3>Name and Email are required. END.</h3>";
    }

    //required captcha
    if ( empty($captcha) ) {
        $error = "<h3>Please check the the captcha form. END.</h3>";
    }

    // if no errors
    if( empty($error) ){

        $secretKey = " *** hidden *** ";
        $ip = $_SERVER['REMOTE_ADDR'];
        // post request to server
        $url = 'https://www.google.com/recaptcha/api/siteverify?secret=' .
        urlencode($secretKey) .  '&response=' . urlencode($captcha);
        $response = file_get_contents($url);
        $responseKeys = json_decode($response, true);
        // should return JSON with success as true

        if ($responseKeys["success"]) {
            echo '<h3>Captcha Success. Writing Mail...</h3>';

         // write mail
         $to = " *** my user email ***.com";
         $email_subject = "C&H form v2c inquiry";
         // To send HTML mail, the Content-type header must be set
         // If the From header is not set, then Luxsci servers reject
         //    $headers = "";
         $headers = "From: *** my email ***.com \r\n";
         $headers .= "Reply-To: $visitor_email \r\n";
         $headers .= "MIME-Version: 1.0 \r\n";
         $headers .= "Content-type: text/html; charset=iso-8859-1 \r\n";
         $headers .= "X-Mailer: PHP/".phpversion();

        $email_body = "message from " . $visitor_name . "<br>" .
            "sender's email: " . $visitor_email . "<br>" .
            "here is the message: " . $visitor_message;
        // compose headers

        //Send the mail
        $mail_check = mail($to, $email_subject, $email_body, $headers);
        if ($mail_check) {
            echo "<h5>Mail sending successful. Redirecting... </h5>";
            echo '<script> window.location.href = "thank_you_CG.html"; </script>';
        } else {
            echo "<h5>MAIL sending failed.</h5><br<br>
                    <h3> This is the failure message it sends out HERE  </h3>";
        }

    } else { // if response not success
         echo '<h3>reCaptcha verification failed!</h3>';
         echo "Response from reCaptcha: <br>";
         print_r($responseKeys);
     }

     } else { //if errors
        echo $error;
        exit;
    }
 }

I can also add the html but there seems to be no problem with that. As mentioned this php form was working fine for years then stopped. I've heard that the Google recaptcha uses a smaller string now?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Get the error message to see if there is something wrong or not. https://stackoverflow.com/questions/3186725/how-can-i-get-the-error-message-for-the-mail-function You should test and focus on question that is it a problem with recaptcha or `mail()` function. If recaptcha is working fine then no need to mention about it but if it isn't then please ask in new question. – vee Sep 06 '22 at 02:46
  • Read more [here](https://stackoverflow.com/questions/24644436/php-mail-function-doesnt-complete-sending-of-e-mail) – vee Sep 06 '22 at 04:54
  • the variable in the above SO question 3186725 comment: echo error_message; causes an error: my server is not able to handle this request. Thus something more error_message must be required. – Nicholas Bourbaki Sep 06 '22 at 16:04
  • You copy their question's code without reading it at all. Please read on the answers section because there are a lot of answers there. Or use `print_r(error_get_last());` – vee Sep 06 '22 at 16:06
  • Posted the Question on Upwork with a bounty of about $35. A person fixed my code: apparently Google changed the whole recapcha format starting in 2022 and it's somewhat more complex. The recaptcha requires apparently a bit more coding. – Nicholas Bourbaki Nov 20 '22 at 18:43

0 Answers0