createAContactFormWithRecaptcha

This is what I did for my joomla site, I couldnt find this so I made one myself.

 

Basically, we will install recaptcha that we got from google on joomla, then create two php pages, 1 for contact form and 1 for email sending, we will put the captcha client side inside the form and the server side form into email sending area to validate.

Here we go.

 

1. Go to https://www.google.com/recaptcha/admin#whyrecaptcha

to get your recaptcha private and public key first, download the recaptcha library and upload the recaptchalib.php to the server root. 
I use profile to do this. Just find it from Joomla extension, it is very convenient.

 

2. Now we can create a module with the form. Here is how to create your own module,

http://www.itlei.com.au/index.php/web/37-joomla/48-create-an-about-author-module-for-joomla-3-0

Similar to above module, we only need to change the module name in everywhere and then we can modify the default.php under tmpl folder into:

<?php // no direct access
defined( '_JEXEC' ) or die( 'Restricted access' ); ?>
<?php // no direct access
defined( '_JEXEC' ) or die( 'Restricted access' ); ?>
<div style="width:100%">
<div style="font-size:11px">

<script type="text/javascript">
 var RecaptchaOptions = {
    theme : 'clean'
 };
 </script>
<form name="contactform" method="post" action="./send_form_email.php">
<table width="450px">
<tr> 
 <td valign="top">
   <label for="name">Name *</label>
  </td>
  <td valign="top">
   <input  type="text" name="name" maxlength="50" size="30">
  </td>
 </tr>
 <tr>
  <td valign="top">
   <label for="email">Email Address *</label>
  </td>
  <td valign="top">
   <input  type="text" name="email" maxlength="80" size="30">
  </td>
 </tr>
 <tr>
  <td valign="top">
   <label for="comments">Comments *</label>
  </td>
  <td valign="top">
   <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>
  </td>
 </tr>
 <tr><td colspan="2">
  <?php
          require_once('recaptchalib.php');
          $publickey = "<Your Public Key from Google>"; 
          echo recaptcha_get_html($publickey);
        ?>
 </td>
 </tr>
<tr>
  <td colspan="2" style="text-align:center">
   <input type="submit" value="Submit">  
  </td>
 </tr>
 </table>
 </form>
</div>
</div>

 

Zip the whole package and install it on Joomla, the add the module to anywhere you want, refresh the page to see if the client side works. Do not submit the form yet, We need prepare the server side.

 

3. Now for the server side, we need send_form_email.php to validate both ReCaptcha and the form, on success it will send the email to your specified address, following is the content for send_form_email.php:

 

<?php
  require_once('recaptchalib.php');
  $privatekey = "<your private key from google";
  $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 {
  if(isset($_POST['email'])) {
 
   // EDIT THE 2 LINES BELOW AS REQUIRED
     $email_to = "your email address";
     $email_subject = "your email subject";
	 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['comments'])) {
         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
     $comments = $_POST['comments']; // 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 Name you entered does not appear to be valid.<br />';
   }
   if(strlen($comments) < 2) {
     $error_message .= 'The Comments 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 .= "Name: ".clean_string($name)."\n";
     $email_message .= "Email: ".clean_string($email_from)."\n";
     $email_message .= "Comments: ".clean_string($comments)."\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 lei. Lei will be in touch with you.
<input action="action" type="button" value="Go back to previous page" onclick="history.go(-1);" />

<?php
 
}
   
}
?>

 

4. upload this send_form_email.php to root directory by using profile. 

5. Now we can test the send form and your email box to see if it works. Cheers.

 

Reference :

http://www.freecontactform.com/email_form.php

https://developers.google.com/recaptcha/docs/php

https://developers.google.com/recaptcha/docs/customization

http://webdeveloperplus.com/php/integrate-customized-recaptcha-in-your-php-application/

About Lei

I am an IT specialist with over 10 year experience - years on Automation, on-Premise or Azure.

I am happy to develop however never want be a full time developer. Only do what I have to do. If it has to be PowerShell,HTML, PHP, CSS, C#, VBS or JS, front end or backend, so be it, doesn't matter!

Spent years with Windows, SCCM, SharePoint, SQL and Exchange servers. For last several years, I have been actively working under On Premise > Azure environment.

THERE IS NO WAY BACK!!!

Current Certificates:
    Microsoft® Certified-
  • -Enterprise Administrator
  • -Database Administrator
  • -SharePoint Administrator
  • -Administering and Deploying SCCM 2012
Red Hat Certified Technician
ITIL V3 Foundation - Practitioner

Working on Azure Certificates now and hopefully they can stop upgrading their questions one day! GIVE ME A BREAK!

Contact Lei

Name *
Email *
Comments *

Traffic since 10/08/2016

Today48
Yesterday83
This week672
This month1968
Total416328

Visitor Info

  • IP: 54.227.51.103
  • Browser: Unknown
  • Browser Version:
  • Operating System: Unknown

Who Is Online

1
Online

2017-12-17

Login