Jump to content

reCaptcha integration problems


iVazanity
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hello im very new to PHP and I got a problem with integrating reCaptcha into my Form. Step one worked pretty good but im failing on Step 2 which is: If your users send the form with integrated reCAPTCHA, you will receive among other things, a string containing the name "G-recaptcha-response". If you want to find out if Google has verified the user in question, send a POST request with the following parameters:

Secret (required) MY_SECRET_CODE Response (required) Value of 'g-recaptcha-response' remoteip The IP address of the end user

Heres my HTML Code:

<!DOCTYPE html>
<html >
  <head>
    <meta charset="UTF-8">
	<meta name="description" content="Kontaktieren Sie hier Mario Fracasso">
	<meta name="keywords" content="contact,kontakt,mario,fracasso,mariofracasso,schreiben,websites,offer">
	<meta name="author" content="Mario ">
		<title>Contact - Mario </title>
			<link rel="stylesheet" href="css/style.css">
				<script src='https://www.google.com/recaptcha/api.js'></script>
  </head>

  <body>

    <div id="form-main">
  <div id="form-div">
	<h1>Contact - Mario </h1>
	<hr>
    <form action="php/form_process.php" method="post" class="form" id="form1">
      
      <p class="name">
        <input name="name" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="First Name, Last Name" id="name" />
      </p>
      
      <p class="email">
        <input name="email" type="email" class="validate[required,custom[email]] feedback-input" id="email" placeholder="Email" />
      </p>
      
      <p class="text">
        <textarea name="text" class="validate[required,length[6,300]] feedback-input" id="comment" placeholder="Message"></textarea>
      </p>
      
<div class="g-recaptcha" data-sitekey="MY_CODE"></div>
<p>
      <div class="submit">
        <input type="submit" value="SEND" id="button-blue"/>
        <div class="ease"></div>
      </div>
    </form>
<footer>
<p>© Mario</p>    
</footer>
  </div>
	</div>
  </body>
</html>

And heres my PHP Code:

<?php
 
if(isset($_POST['email'])) {
 
     
 
    // EDIT THE 2 LINES BELOW AS REQUIRED
 
    $email_to = "MY-EMAIL";
 
    $email_subject = "Your email subject line";
 
     
 
     
 
    function died($error) {
 
        // your error code can go here
 
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
 
        echo "These errors appear below.<br /><br />";
 
        echo $error."<br /><br />";
 
        echo "Please go back and fix these errors.<br /><br />";
 
        die();
 
    }
 
     
 
    // validation expected data exists
 
    if(!isset($_POST['name']) ||
 
        !isset($_POST['email']) ||
 
        !isset($_POST['text'])) {
 
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
 
    }
 
     
 
    $name = $_POST['name']; // required
 
    $email_from = $_POST['email']; // required
 
 
    $text = $_POST['text']; // required
 
     
 
    $error_message = "";
 
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
 
  if(!preg_match($email_exp,$email_from)) {
 
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
 
  }
 
    $string_exp = "/^[A-Za-z .'-]+$/";
 
  if(!preg_match($string_exp,$name)) {
 
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
 
  }
 
  if(strlen($text) < 2) {
 
    $error_message .= 'The text you entered do not appear to be valid.<br />';
 
  }
 
  if(strlen($error_message) > 0) {
 
    died($error_message);
 
  }
 
    $email_message = "Form details below.\n\n";
 
     
 
    function clean_string($string) {
 
      $bad = array("content-type","bcc:","to:","cc:","href");
 
      return str_replace($bad,"",$string);
 
    }
 
     
 
    $email_message .= "First Name: ".clean_string($name)."\n";
 
 
    $email_message .= "Email: ".clean_string($email_from)."\n";
 
 
    $email_message .= "text: ".clean_string($text)."\n";
 
     
 
     
 
// create email headers
 
$headers = 'From: '.$email_from."\r\n".
 
'Reply-To: '.$email_from."\r\n" .
 
'X-Mailer: PHP/' . phpversion();
 
@mail($email_to, $email_subject, $email_message, $headers);  
 
?>
 
 
 
<!-- include your own success html here -->
 
 
 
Thank you for contacting us. We will be in touch with you very soon.
 
 
 
<?php
 
}
 
?>

So, heres my question: How do I implement this reCaptcha? Is there any solution? I hope you can help me - oh and hey, im sorry for my bad english.

Link to comment
Share on other sites

  • Solution

As explained in googles recaptcha documentation for verifying the recaptcha response you need to send a POST request to https://www.google.com/recaptcha/api/siteverify containing both your google recaptcha's secret key and the g-recaptcha-response value in order for google to verify the users recpatcha value is valid when your form has been submitted.

 

To do this you would use curl, take a look at the post by printf's here for the code to do this. Note you will need to set the value of $key here

$key         = 'my key'; 

to your google recaptcha secrete key. And also replace

            echo 'Recaptha Result: ';


            var_dump ( $result['success'] );

With

            if($result['success'])
            {
                // YOUR CODE FOR SENDING THE EMAIL GOES HERE
            }
  • Like 1
Link to comment
Share on other sites

 

As explained in googles recaptcha documentation for verifying the recaptcha response you need to send a POST request to https://www.google.com/recaptcha/api/siteverify containing both your google recaptcha's secret key and the g-recaptcha-response value in order for google to verify the users recpatcha value is valid when your form has been submitted.

 

To do this you would use curl, take a look at the post by printf's here for the code to do this. Note you will need to set the value of $key here

$key         = 'my key'; 

to your google recaptcha secrete key. And also replace

            echo 'Recaptha Result: ';


            var_dump ( $result['success'] );

With

            if($result['success'])
            {
                // YOUR CODE FOR SENDING THE EMAIL GOES HERE
            }

Thank you for your answer!

But if I paste this code into my Page, it gets me an error which is: Curl Error: ' . curl_error ( $curl_init ); } else { $result = json_decode ( $response, TRUE ); if($result['success']) { // YOUR CODE FOR SENDING THE EMAIL GOES HERE } } curl_close ( $curl_init ); } }

What sending code for the email is supposed to be here?

 

The Code:

<!DOCTYPE html>
<html >
  <head>
    <meta charset="UTF-8">
	<meta name="description" content="Kontaktieren Sie hier Mario ">
	<meta name="keywords" content="contact,kontakt,mario,,schreiben,websites,offer">
	<meta name="author" content="Mario">
		<title>Contact - Mario</title>
			<link rel="stylesheet" href="css/style.css">
				<script src='https://www.google.com/recaptcha/api.js'></script>
  </head>

  <body>

    <div id="form-main">
  <div id="form-div">
	<h1>Contact - Mario</h1>
	<hr>
    <form action="php/form_process.php" method="post" class="form" id="form1">
      
      <p class="name">
        <input name="name" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="First Name, Last Name" id="name" />
      </p>
      
      <p class="email">
        <input name="email" type="email" class="validate[required,custom[email]] feedback-input" id="email" placeholder="Email" />
      </p>
      
      <p class="text">
        <textarea name="text" class="validate[required,length[6,300]] feedback-input" id="comment" placeholder="Message"></textarea>
      </p>
      
<div class="g-recaptcha" data-sitekey="KEY"></div>
<p>
      <div class="submit">
        <input type="submit" value="SEND" id="button-blue"/>
        <div class="ease"></div>
      </div>
    </form>
<footer>
<p>© Mario</p>    
</footer>
  </div>
	</div>
  </body>
</html>

<?php
if ( $_SERVER['REQUEST_METHOD'] === 'POST' )

{

    if( isset ( $_POST['g-recaptcha-response'] ) && ! empty ( $_POST['g-recaptcha-response'] ) )

    {

        $key         = 'HEREISMYNORMALKEY';


        $rip         = $_SERVER['MYPAGE'];


        $captchaurl  = 'https://www.google.com/recaptcha/api/siteverify?';


        $captchaurl .= 'secret=HEREISMYSECRETKEY' . $key . '&';


        $captchaurl .= 'response=https://www.google.com/recaptcha/api/siteverify' . $_POST['g-recaptcha-response'] . '&';


        $captchaurl .= 'ip=https://www.google.com/recaptcha/api/siteverify' . $rip;


        $curl_init = curl_init ();


        curl_setopt ( $curl_init, CURLOPT_URL, $captchaurl );


        curl_setopt ( $curl_init, CURLOPT_RETURNTRANSFER, 1 );


        curl_setopt ( $curl_init, CURLOPT_TIMEOUT, 5 );


        curl_setopt ( $curl_init, CURLOPT_USERAGENT, 'PHP/reCAPTCHA' );


        curl_setopt ( $curl_init, CURLOPT_SSL_VERIFYPEER, FALSE );


        $response = curl_exec ( $curl_init );


        if ( $response == FALSE )

        {

            echo '<p>Curl Error: ' . curl_error ( $curl_init );

        }

        else

        {

            $result = json_decode ( $response, TRUE );


            if($result['success'])
            {
                // YOUR CODE FOR SENDING THE EMAIL GOES HERE
            }

        }


        curl_close ( $curl_init );

    }

}
<?

What did I do wrong?

And another question: Should I place this PHP code into my PHP file or just paste it into my html file?

 

Thanks advance for your help!

Link to comment
Share on other sites

No the PHP code for verifying the recaptcha needs to go in the file where your form is being submitted to, which is php/form_process.php

Alright. Got ya.

The error message is gone. what about this part?

            if($result['success'])
            {
                // YOUR CODE FOR SENDING THE EMAIL GOES HERE
            }
Link to comment
Share on other sites

 

As explained in googles recaptcha documentation for verifying the recaptcha response you need to send a POST request to https://www.google.com/recaptcha/api/siteverify containing both your google recaptcha's secret key and the g-recaptcha-response value in order for google to verify the users recpatcha value is valid when your form has been submitted.

 

To do this you would use curl, take a look at the post by printf's here for the code to do this. Note you will need to set the value of $key here

$key         = 'my key'; 

to your google recaptcha secrete key. And also replace

            echo 'Recaptha Result: ';


            var_dump ( $result['success'] );

With

            if($result['success'])
            {
                // YOUR CODE FOR SENDING THE EMAIL GOES HERE
            }

Hi,

 

I did that as you said but the refreshing"bug" is still there.. heres the part of the code:

      {

            $result = json_decode ( $response, TRUE );


            if($result['success'])
            {
            echo 'Recaptcha Result: ';


            var_dump ( $result['success'] );
            }

        }

Thank you so much for your help!

Link to comment
Share on other sites

 

I did that as you said

No you haven't I said this

 

As I said in the comment, replace it (the comment line, not the if statement) with your code that is processing/sending the email

You should know what part of your code is processing/sending the code if you have wrote the code

Link to comment
Share on other sites

No you haven't I said this

 

You should know what part of your code is processing/sending the code if you have wrote the code

Alright I did that. Now it gives me the error Parse error: syntax error, unexpected end of file in /var/customers/webs/ni61032_1/mario/php/form_process.php on line 220

...

Edited by iVazanity
Link to comment
Share on other sites

Here we go:

:)

<?php
 
if(isset($_POST['email'])) {
 
     
 
    // EDIT THE 2 LINES BELOW AS REQUIRED
 
    $email_to = "mariail.com";
 
    $email_subject = "Mario  Business Contact";
 
     
 
     
 
    function died($error) {
 
        // your error code can go here
 
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
 
        echo "These errors appear below.<br /><br />";
 
        echo $error."<br /><br />";
 
        echo "Please go back and fix these errors.<br /><br />";
 
        die();
 
    }
 
     
 
    // validation expected data exists
 
    if(!isset($_POST['name']) ||
 
        !isset($_POST['email']) ||
 
        !isset($_POST['text'])) {
 
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
 
    }
 
     
 
    $name = $_POST['name']; // required
 
    $email_from = $_POST['email']; // required
 
 
    $text = $_POST['text']; // required
 
     
 
    $error_message = "";
 
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
 
  if(!preg_match($email_exp,$email_from)) {
 
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
 
  }
 
    $string_exp = "/^[A-Za-z .'-]+$/";
 
  if(!preg_match($string_exp,$name)) {
 
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
 
  }
 
  if(strlen($text) < 2) {
 
    $error_message .= 'The text you entered do not appear to be valid.<br />';
 
  }
 
  if(strlen($error_message) > 0) {
 
    died($error_message);
 
  }
 
    $email_message = "Jemand hat dir eine Nachricht vom Kontaktformular von mario.samirafracasso.com gesendet\n\n";
 
     
 
    function clean_string($string) {
 
      $bad = array("content-type","bcc:","to:","cc:","href");
 
      return str_replace($bad,"",$string);
 
    }
 
     
 
    $email_message .= "Vorname, Nachname: ".clean_string($name)."\n";
 
 
    $email_message .= "Email Adresse: ".clean_string($email_from)."\n";
 
 
    $email_message .= "Nachricht: ".clean_string($text)."\n";
 
     
 
     
 
// create email headers
 
$headers = 'From: '.$email_from."\r\n".
 
'Reply-To: '.$email_from."\r\n" .
 
'X-Mailer: PHP/' . phpversion();

//RECAPTCHA

if ( $_SERVER['REQUEST_METHOD'] === 'POST' )

{

    if( isset ( $_POST['g-recaptcha-response'] ) && ! empty ( $_POST['g-recaptcha-response'] ) )

    {

        $key         = '6Ld2RxATAAAAADPBKUF9YzNvGHJDSKEC1HtS8VIs';


        $rip         = 'http://mario.samirafracasso.com';


        $captchaurl  = 'https://www.google.com/recaptcha/api/siteverify?';


        $captchaurl .= 'secret=' . $key . '&';


        $captchaurl .= 'response=' . $_POST['g-recaptcha-response'] . '&';


        $captchaurl .= 'ip=' . $rip;


        $curl_init = curl_init ();


        curl_setopt ( $curl_init, CURLOPT_URL, $captchaurl );


        curl_setopt ( $curl_init, CURLOPT_RETURNTRANSFER, 1 );


        curl_setopt ( $curl_init, CURLOPT_TIMEOUT, 5 );


        curl_setopt ( $curl_init, CURLOPT_USERAGENT, 'PHP/reCAPTCHA' );


        curl_setopt ( $curl_init, CURLOPT_SSL_VERIFYPEER, FALSE );


        $response = curl_exec ( $curl_init );


        if ( $response == FALSE )

        {

            echo '<p>Curl Error: ' . curl_error ( $curl_init );

        }

        else

        {

            $result = json_decode ( $response, TRUE );


            if($result['success'])
            {
            if($result['success'])
            {
                mail($email_to, $email_subject, $email_message, $headers);
            }

        }


        curl_close ( $curl_init );

    }

}
?>
 
<!DOCTYPE html>
<html>
	<head>
		<link rel="stylesheet" href="../css/style.css">
		<title>E-Mail versendet</title>
	</head>
<body>
	<div id="mail-sent">
		<h1>Thank you for contacting us. We will be in touch with you very soon.</h1>
	</div>
</body>
 
</html>
<?php
 
}
 
?>

(Line 190)

Link to comment
Share on other sites

Here we go:

:)

<?php
 
if(isset($_POST['email'])) {
 
     
 
    // EDIT THE 2 LINES BELOW AS REQUIRED
 
    $email_to = "mariail.com";
 
    $email_subject = "Mario  Business Contact";
 
     
 
     
 
    function died($error) {
 
        // your error code can go here
 
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
 
        echo "These errors appear below.<br /><br />";
 
        echo $error."<br /><br />";
 
        echo "Please go back and fix these errors.<br /><br />";
 
        die();
 
    }
 
     
 
    // validation expected data exists
 
    if(!isset($_POST['name']) ||
 
        !isset($_POST['email']) ||
 
        !isset($_POST['text'])) {
 
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
 
    }
 
     
 
    $name = $_POST['name']; // required
 
    $email_from = $_POST['email']; // required
 
 
    $text = $_POST['text']; // required
 
     
 
    $error_message = "";
 
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
 
  if(!preg_match($email_exp,$email_from)) {
 
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
 
  }
 
    $string_exp = "/^[A-Za-z .'-]+$/";
 
  if(!preg_match($string_exp,$name)) {
 
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
 
  }
 
  if(strlen($text) < 2) {
 
    $error_message .= 'The text you entered do not appear to be valid.<br />';
 
  }
 
  if(strlen($error_message) > 0) {
 
    died($error_message);
 
  }
 
    $email_message = "Jemand hat dir eine Nachricht vom Kontaktformular von mario.samirafracasso.com gesendet\n\n";
 
     
 
    function clean_string($string) {
 
      $bad = array("content-type","bcc:","to:","cc:","href");
 
      return str_replace($bad,"",$string);
 
    }
 
     
 
    $email_message .= "Vorname, Nachname: ".clean_string($name)."\n";
 
 
    $email_message .= "Email Adresse: ".clean_string($email_from)."\n";
 
 
    $email_message .= "Nachricht: ".clean_string($text)."\n";
 
     
 
     
 
// create email headers
 
$headers = 'From: '.$email_from."\r\n".
 
'Reply-To: '.$email_from."\r\n" .
 
'X-Mailer: PHP/' . phpversion();

//RECAPTCHA

if ( $_SERVER['REQUEST_METHOD'] === 'POST' )

{

    if( isset ( $_POST['g-recaptcha-response'] ) && ! empty ( $_POST['g-recaptcha-response'] ) )

    {

        $key         = '';


        $rip         = 'http://mario.samirafracasso.com';


        $captchaurl  = 'https://www.google.com/recaptcha/api/siteverify?';


        $captchaurl .= 'secret=' . $key . '&';


        $captchaurl .= 'response=' . $_POST['g-recaptcha-response'] . '&';


        $captchaurl .= 'ip=' . $rip;


        $curl_init = curl_init ();


        curl_setopt ( $curl_init, CURLOPT_URL, $captchaurl );


        curl_setopt ( $curl_init, CURLOPT_RETURNTRANSFER, 1 );


        curl_setopt ( $curl_init, CURLOPT_TIMEOUT, 5 );


        curl_setopt ( $curl_init, CURLOPT_USERAGENT, 'PHP/reCAPTCHA' );


        curl_setopt ( $curl_init, CURLOPT_SSL_VERIFYPEER, FALSE );


        $response = curl_exec ( $curl_init );


        if ( $response == FALSE )

        {

            echo '<p>Curl Error: ' . curl_error ( $curl_init );

        }

        else

        {

            $result = json_decode ( $response, TRUE );


            if($result['success'])
            {
            if($result['success'])
            {
                mail($email_to, $email_subject, $email_message, $headers);
            }

        }


        curl_close ( $curl_init );

    }

}
?>
 
<!DOCTYPE html>
<html>
	<head>
		<link rel="stylesheet" href="../css/style.css">
		<title>E-Mail versendet</title>
	</head>
<body>
	<div id="mail-sent">
		<h1>Thank you for contacting us. We will be in touch with you very soon.</h1>
	</div>
</body>
 
</html>
<?php
 
}
 
?>

(Line 190)

oh I see an error on line 186 gonna fix this

Edited by Ch0cu3r
Link to comment
Share on other sites

I have re-organised your code/clean it up a little. Try

<?php

function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error . "<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
}

function clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad, "", $string);
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    // first validate google recapture is valid
    if (isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {
        $key         = '***'; // replace with your site key
        $rip         = $_SERVER['REMOTE_ADDR'];
        $captchaurl  = 'https://www.google.com/recaptcha/api/siteverify?';
        $captchaurl .= 'secret=' . $key . '&';
        $captchaurl .= 'response=' . $_POST['g-recaptcha-response'] . '&';
        $captchaurl .= 'ip=' . $rip;
        $curl_init = curl_init();
        curl_setopt($curl_init, CURLOPT_URL, $captchaurl);
        curl_setopt($curl_init, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl_init, CURLOPT_TIMEOUT, 5);
        curl_setopt($curl_init, CURLOPT_USERAGENT, 'PHP/reCAPTCHA');
        curl_setopt($curl_init, CURLOPT_SSL_VERIFYPEER, FALSE);
        $response = curl_exec($curl_init);
        if ($response == FALSE) {
            echo '<p>Curl Error: ' . curl_error($curl_init);
        } else {
            $result = json_decode($response, TRUE);
            // if the result of the google recaptcha is not valid, then show error message
            if (!isset($result['success'])) {
                died('Invalid Google ReCaptcha response');
            }
        }
        curl_close($curl_init);
    }

    // if we got this far, then process/send the email
    if (isset($_POST['email'])) {

        // EDIT THE 2 LINES BELOW AS REQUIRED
        $email_to      = "mariail.com";
        $email_subject = "Mario  Business Contact";

        // validation expected data exists
        if (!isset($_POST['name']) || !isset($_POST['email']) || !isset($_POST['text'])) {
            died('We are sorry, but there appears to be a problem with the form you submitted.');
        }

        $name          = $_POST['name']; // required
        $email_from    = $_POST['email']; // required
        $text          = $_POST['text']; // required
        $error_message = "";

        $email_exp     = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
        if (!preg_match($email_exp, $email_from)) {
            $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
        }

        $string_exp = "/^[A-Za-z .'-]+$/";
        if (!preg_match($string_exp, $name)) {
            $error_message .= 'The First Name you entered does not appear to be valid.<br />';
        }

        if (strlen($text) < 2) {
            $error_message .= 'The text you entered do not appear to be valid.<br />';
        }

        if (strlen($error_message) > 0) {
            died($error_message);
        }

        $email_message = "Jemand hat dir eine Nachricht vom Kontaktformular von mario.samirafracasso.com gesendet\n\n";

        $email_message .= "Vorname, Nachname: " . clean_string($name) . "\n";
        $email_message .= "Email Adresse: " . clean_string($email_from) . "\n";
        $email_message .= "Nachricht: " . clean_string($text) . "\n";
        // create email headers
        $headers = 'From: ' . $email_from . "\r\n" . 
                   'Reply-To: ' . $email_from . "\r\n" . 
                   'X-Mailer: PHP/' . phpversion();

        mail($email_to, $email_subject, $email_message, $headers);
?>
 
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="../css/style.css">
        <title>E-Mail versendet</title>
    </head>
<body>
    <div id="mail-sent">
        <h1>Thank you for contacting us. We will be in touch with you very soon.</h1>
    </div>
</body>
 
</html>
<?php
    }
    else {
        died('Email address not entered');
    }
}
?>
Link to comment
Share on other sites

 

I have re-organised your code/clean it up a little. Try

<?php

function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error . "<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
}

function clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad, "", $string);
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    // first validate google recapture is valid
    if (isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {
        $key         = '***'; // replace with your site key
        $rip         = $_SERVER['REMOTE_ADDR'];
        $captchaurl  = 'https://www.google.com/recaptcha/api/siteverify?';
        $captchaurl .= 'secret=' . $key . '&';
        $captchaurl .= 'response=' . $_POST['g-recaptcha-response'] . '&';
        $captchaurl .= 'ip=' . $rip;
        $curl_init = curl_init();
        curl_setopt($curl_init, CURLOPT_URL, $captchaurl);
        curl_setopt($curl_init, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl_init, CURLOPT_TIMEOUT, 5);
        curl_setopt($curl_init, CURLOPT_USERAGENT, 'PHP/reCAPTCHA');
        curl_setopt($curl_init, CURLOPT_SSL_VERIFYPEER, FALSE);
        $response = curl_exec($curl_init);
        if ($response == FALSE) {
            echo '<p>Curl Error: ' . curl_error($curl_init);
        } else {
            $result = json_decode($response, TRUE);
            // if the result of the google recaptcha is not valid, then show error message
            if (!isset($result['success'])) {
                died('Invalid Google ReCaptcha response');
            }
        }
        curl_close($curl_init);
    }

    // if we got this far, then process/send the email
    if (isset($_POST['email'])) {

        // EDIT THE 2 LINES BELOW AS REQUIRED
        $email_to      = "mariail.com";
        $email_subject = "Mario  Business Contact";

        // validation expected data exists
        if (!isset($_POST['name']) || !isset($_POST['email']) || !isset($_POST['text'])) {
            died('We are sorry, but there appears to be a problem with the form you submitted.');
        }

        $name          = $_POST['name']; // required
        $email_from    = $_POST['email']; // required
        $text          = $_POST['text']; // required
        $error_message = "";

        $email_exp     = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
        if (!preg_match($email_exp, $email_from)) {
            $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
        }

        $string_exp = "/^[A-Za-z .'-]+$/";
        if (!preg_match($string_exp, $name)) {
            $error_message .= 'The First Name you entered does not appear to be valid.<br />';
        }

        if (strlen($text) < 2) {
            $error_message .= 'The text you entered do not appear to be valid.<br />';
        }

        if (strlen($error_message) > 0) {
            died($error_message);
        }

        $email_message = "Jemand hat dir eine Nachricht vom Kontaktformular von mario.samirafracasso.com gesendet\n\n";

        $email_message .= "Vorname, Nachname: " . clean_string($name) . "\n";
        $email_message .= "Email Adresse: " . clean_string($email_from) . "\n";
        $email_message .= "Nachricht: " . clean_string($text) . "\n";
        // create email headers
        $headers = 'From: ' . $email_from . "\r\n" . 
                   'Reply-To: ' . $email_from . "\r\n" . 
                   'X-Mailer: PHP/' . phpversion();

        mail($email_to, $email_subject, $email_message, $headers);
?>
 
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="../css/style.css">
        <title>E-Mail versendet</title>
    </head>
<body>
    <div id="mail-sent">
        <h1>Thank you for contacting us. We will be in touch with you very soon.</h1>
    </div>
</body>
 
</html>
<?php
    }
    else {
        died('Email address not entered');
    }
}
?>

Wow thats very kind! Thanks :)

But sadly, the E-Mail still doesn't arrive.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.