*** Recaptcha with AJAX GIF loader ***
Gmail SMTP server address: smtp.gmail.com
Gmail SMTP username: Your full Gmail address (e.g. yourusername@gmail.com)
Gmail SMTP password: Your Gmail password
Gmail SMTP port (TLS): 587
Gmail SMTP port (SSL): 465
Gmail SMTP TLS/SSL required: yes
HTML:
<form action="" method="POST" id="leadform">
...
..
.
</form>
<div id="loader" class="col-md-12" style="display:none;">
<img src="<?php echo get_bloginfo('template_directory'); ?>/images/spinner.gif">
</div>
<div class="col-md-12 text-center">
<div id="result" class="alert alert-success" style="display:none;"></div>
</div>
================================================================================
CSS:
<style>
#loader {
display:none;
position: fixed;
background: rgba(0, 0, 0, 0.66);
width: 100%;
top: 0;
bottom: 0;
left: 0;
padding-top: 15%;
overflow: hidden;
margin: 0 auto;
text-align: center;
}
#loader img { margin: 0 auto; }
</style>
================================================================================
JS:
<script>
jQuery(document).ready(function() {
jQuery("#leadform").submit(function(event) {
jQuery('#loader').show() ; // show the loader on init
/* Stop form from submitting normally */
event.preventDefault();
// note the html ID has dashes "-" , the var for recaptcha has underscores
// you will need to refer to this var as $_POST['g_recaptcha_response']
// not $_POST['g-recaptcha-response']
var g_recaptcha_response = jQuery('#g-recaptcha-response').val();
var currenturl = jQuery('#currenturl').val();
var values = jQuery(this).serialize();
/* Clear result div*/
jQuery("#result").html('');
ajaxRequest = jQuery.ajax({
url: "<?php echo get_template_directory_uri() ; ?>/send_email.php",
type: "post",
data: values
});
/* request cab be abort by ajaxRequest.abort() */
ajaxRequest.done(function (response, textStatus, jqXHR){
// show successfully for submit message
console.log("SUCCESS") ;
jQuery('#result').show() ;
jQuery("#result").html(response);
jQuery('#loader').hide() ; // ajax successful , hide the loader
setTimeout(function () {
window.location.href=currenturl; // the redirect goes here
},3000); // 3 seconds
});
/* On failure of request this function will be called */
ajaxRequest.fail(function (response){
// show error
console.log(" ajax fail ERROR" + response) ;
jQuery("#result").html('Error found trying to submit the form: ' + response );
});
});
}); // end document ready
</script>
============================== send_email.php ==================================
<?php
require 'PHPMailerAutoload.php';
require_once 'Recaptcha.php';
// require_once dirname(__FILE__) . '/Recaptcha.php';
if ( ! class_exists('Recaptcha') )
die('Class Recaptcha not found') ;
$recaptcha = $_POST['g_recaptcha_response'];
// sanitize POST input
extract($_POST) ;
$current_date = date('n/j/Y');
$current_time = date('g:i a') ;
// create new object, use to verify recaptcha response
$recobj = new Recaptcha();
$response = $recobj->verifyResponse($recaptcha);
if( isset($response['success']) && $response['success'] != true ) {
echo "An Error Occurred and Error code is :".$response['error-codes'];
}
else {
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "mail.smtp2go.com "; // sets GMAIL as the SMTP server
$mail->Port = 80; // set the SMTP port for the GMAIL server
$mail->Username = "armadillo"; // GMAIL username
$mail->Password = "popadmin7480#";
$mail->setFrom('admin@bristlebarn.com', 'BristleBarn Admin');
$mail->addReplyTo('jcadima85@gmail.com', 'reply to');
$mail->addAddress( 'jcadima85@gmail.com', 'Bristle Barn'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'New Lead: ' . $product_title ;
$mail->Body = "
==================================== <br>
INDUSTRIAL BRUSH CORPORATION<br>
Internet Information Form - Contact<br>
Date: $current_date Time: $current_time<br>
====================================<br>
<strong>Product Name:</strong> $product_title<br>
<strong>Company Name:</strong> $company<br>
<strong>Address:</strong> $address<br>
<strong>City:</strong> $city<br>
<strong>State:</strong> $state<br>
<strong>Zip:</strong> $zip<br>
<strong>Phone Number:</strong> $phonenumber <br>
<strong>Email:</strong> $email<br>" ;
// $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send() ) {
echo 'Message could not be sent.';
//echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
?>
============================== config.php ======================================
<?php
// [ ] will not work on old PHP versions, use array()
return [
'client-key' => '6LeT7h8UAAAAALq7qjy90QTcjphFpAtg3oW6zEcT',
'secret-key' => '6LeT7h8UAAAAAOoLSh0t6tPgv8D3hCMUeJN5FxRY'
];
?>
============================== Recaptcha.php ===================================
<?php
class Recaptcha {
public function __construct(){
// retrieve config array values
$this->config = require('config.php');
}
public function verifyResponse($recaptcha){
// $remoteIp = $this->getIPAddress();
// Discard empty solution submissions and return
if (empty($recaptcha)) {
return array(
'success' => false,
'error-codes' => 'Recaptcha is required',
);
}
// get a JSON response
$jsonresponse = $this->getJSONResponse(
array(
'secret' => $this->config['secret-key'],
//'remoteip' => $remoteIp,
'response' => $recaptcha,
)
);
// decoded JSON reCAPTCHA server response
$responses = json_decode($jsonresponse, true);
// Set array keys for success/failure
if ( isset($responses['success']) and $responses['success'] == true ) {
$status = true;
} else {
$status = false;
$error = (isset($responses['error-codes'])) ? $responses['error-codes']
: 'invalid-input-response';
}
// return the status and if error-codes (if any)
return array(
'success' => $status,
'error-codes' => (isset($error)) ? $error : null,
);
}
private function getJSONResponse($data){
$url = 'https://www.google.com/recaptcha/api/siteverify?'.http_build_query($data);
$response = file_get_contents($url);
return $response;
}
private function getIPAddress(){
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
}