Skip to content

Instantly share code, notes, and snippets.

@KustomDeveloper
Created December 4, 2018 16:23
Show Gist options
  • Select an option

  • Save KustomDeveloper/7e1ff85e0a88345d0c587c0a0b085d05 to your computer and use it in GitHub Desktop.

Select an option

Save KustomDeveloper/7e1ff85e0a88345d0c587c0a0b085d05 to your computer and use it in GitHub Desktop.
Basic php form using recatcha v2 and curl
//Fetch Recaptcha API
<script src='https://www.google.com/recaptcha/api.js'></script>
<div id="contact_form">
<h1>Contact Form</h1>
<form method="POST" action="includes/php/mail.php">
<p class="">
Your Name:</p>
<input class="" name="name" type="text" size="25" maxlength="100" />
<br /><br />
<p class="">
Your Email Address:</p>
<input class="" name="email" type="text" size="25" maxlength="100" />
<br /><br />
<p class="">
Your Phone Number:</p>
<input class="" name="telephone" type="text" size="25" maxlength="100" />
<br />
<br />
<p class="">
Your Message:
</p><textarea class="" name="message"></textarea>
<br />
<div class="g-recaptcha" data-sitekey="6Lfon34UAAAAAGS_cI1a21CEFd4xCkBytB4VvMpt"></div>
<br />
<div id="submit">
<p><input type="submit" value="Send Email" name="submit" />
<input type="reset" value="Clear Form" /></p>
</div>
</form>
</div>
<?php
if(isset($_POST['submit']) && !empty($_POST['submit'])):
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
//your site secret key
$secret = '!!!SECRET_KEY_GOES_HERE!!!!';
$ip = $_SERVER['REMOTE_ADDR'];
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response'].'&remoteip='.$ip);
//Prevent printing curl object on page, instead store data in response variable below and decode json
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// grab URL and pass it to the browser
$response = curl_exec($ch);
$responseData = json_decode($response);
// close cURL resource, and free up system resources
curl_close($ch);
if($responseData->success):
//contact form submission code
$name = !empty($_POST['name'])?$_POST['name']:'';
$email = !empty($_POST['email'])?$_POST['email']:'';
$telephone = !empty($_POST['telephone'])?$_POST['telephone']:'';
$message = !empty($_POST['message'])?$_POST['message']:'';
$to = '[email protected]';
$subject = $_POST['subject'];
$body = "
<h1>Contact request details</h1>
<p><b>Name: </b>".$name."</p>
<p><b>Email: </b>".$email."</p>
<p><b>Telephone: </b>".$telephone."</p>
<p><b>Message: </b>".$message."</p>";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// // More headers
$headers .= 'From:'.$name.' <'.$email.'>' . "\r\n";
//send email
mail($to,$subject,$body,$headers);
//Successful form submission - message then redirect to thank you page for tracking in GA
echo '<h1>Success!</h1><p>Your contact request has submitted successfully.</p>';
echo '<script>setTimeout(function(){window.location = "https://mywebsite.com/thank-you-page"}, 3000)</script>';
else:
$errMsg = '<h1>Error</h1><p>Robot verification failed, please try again.</p>';
echo $errMsg;
endif;
else:
$errMsg = '<h1>Error</h1><p>Please click on the reCAPTCHA box.</p>';
echo $errMsg;
endif;
else:
$errMsg = '';
$succMsg = '';
endif;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment