RD Mail Form: How to integrate Google reCAPTCHA correctly ??

2,610 views
Skip to first unread message

J.G. van der Horst

unread,
Apr 17, 2016, 9:00:27 AM4/17/16
to reCAPTCHA
I have a problem integrating reCAPTCHA on my website:

http://j-test2.nl/e-pmo/



RD Mail Form

My website already has a RD Mail Form along with a .php file for that form.
Here is a snippet of the html code for the contact form in my index.php file:

<!-- RD Mailform -->
<form class='rd-mailform' method="post" action="
<?php echo base_url();?>bat/rd-mailform.php">
 
<!-- RD Mailform Type -->
 
<input type="hidden" name="form-type" value="contact"/>
 
<!-- END RD Mailform Type -->
 
<fieldset>
   
<label data-add-placeholder>
     
<input type="text"
                 
name="name"
                 
placeholder="Naam"
               
data-constraints="@NotEmpty @LettersOnly"/>
   
</label>


   
<label data-add-placeholder>
     
<input type="text"
                 
name="Achternam"
                 
placeholder="Last Name:"
                 
data-constraints="@NotEmpty @LettersOnly"/>
   
</label>


   
<label data-add-placeholder>
     
<input type="text"
                 
name="phone"
                 
placeholder="Telefoonnummer"
                 
data-constraints="@NotEmpty @Phone"/>
   
</label>


   
<label data-add-placeholder>
     
<input type="text"
                 
name="email"
                 
placeholder="E-mailadres:"
                 
data-constraints="@NotEmpty @Email"/>
     
</label>


   
<label data-add-placeholder>
     
<select name="select">
       
<option disabled selected>Kies je BodyCheck brochure(s)</option>
       
<option value="Preventief Medisch Onderzoek">Preventief Medisch Onderzoek</option>
       
<option value="Sport Medisch Onderzoek">Sport Medisch Onderzoek</option>
       
<option value="e-PMO & e-SMO">e-PMO & e-SMO</option>
     
</select>
   
</label>
     
</br>
     
<p><button class="btn btn-xl btn-primary text-contrast bold" type="submit">Brochure aanvragen</button></p>
   
<div class="mfInfo">
   
</div>
 
</fieldset>
</form>


<!-- END RD Mailform -->



reCAPTCHA

I have registered with Google reCaptcha to obtain a site key and secret key and have added the:

<div>

within my section:

<form></form>

Here is the snippet of the code:

<div class="g-recaptcha" data-sitekey="MyKey"></div>


I have also added the following to my Head section:


Here is the snippet of the code in my header.php file:

<!DOCTYPE html>
<html lang="en">
<head>




 
<!-- Google reCAPTCHA -->


 
<script src='https://www.google.com/recaptcha/api.js?hl=tr'></script>

  

My initial question

Since the site already has a .php file for this Contact Form,
how do I modify the file so that the reCaptcha works properly?

Do I only need to modify the above .php file? 

From my online searching, it looks like I may also have to include the recaptchalib.php 
library file and upload it to my hosting server?!

Here is a snippet of the rd-mailform.php file already running for this website:

<?php


$recipients
= 'j.g.ds...@gmail.com';
//$recipients = '#';


try {
   
require './phpmailer/PHPMailerAutoload.php';


    preg_match_all
("/([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)/", $recipients, $addresses, PREG_OFFSET_CAPTURE);


   
if (!count($addresses[0])) {
       
die('MF001');
   
}


   
if (preg_match('/^(127\.|192\.168\.)/', $_SERVER['REMOTE_ADDR'])) {
       
die('MF002');
   
}


    $template
= file_get_contents('rd-mailform.tpl');


   
if (isset($_POST['form-type'])) {
       
switch ($_POST['form-type']){
           
case 'contact':
                $subject
= 'A message from your site visitor';
               
break;
           
case 'subscribe':
                $subject
= 'Subscribe request';
               
break;
           
case 'order':
                $subject
= 'Order request';
               
break;
           
default:
                $subject
= 'A message from your site visitor';
               
break;
       
}
   
}else{
       
die('MF004');
   
}


   
if (isset($_POST['email'])) {
        $template
= str_replace(
           
["<!-- #{FromState} -->", "<!-- #{FromEmail} -->"],
           
["Email:", $_POST['email']],
            $template
);
   
}else{
       
die('MF003');
   
}


   
if (isset($_POST['message'])) {
        $template
= str_replace(
           
["<!-- #{MessageState} -->", "<!-- #{MessageDescription} -->"],
           
["Message:", $_POST['message']],
            $template
);
   
}


    preg_match
("/(<!-- #{BeginInfo} -->)(.|\n)+(<!-- #{EndInfo} -->)/", $template, $tmp, PREG_OFFSET_CAPTURE);
   
foreach ($_POST as $key => $value) {
       
if ($key != "email" && $key != "message" && $key != "form-type" && !empty($value)){
            $info
= str_replace(
               
["<!-- #{BeginInfo} -->", "<!-- #{InfoState} -->", "<!-- #{InfoDescription} -->"],
               
["", ucfirst($key) . ':', $value],
                $tmp
[0][0]);


            $template
= str_replace("<!-- #{EndInfo} -->", $info, $template);
       
}
   
}


    $template
= str_replace(
       
["<!-- #{Subject} -->", "<!-- #{SiteName} -->"],
       
[$subject, $_SERVER['SERVER_NAME']],
        $template
);


    $mail
= new PHPMailer();
    $mail
->From = $_POST['email'];


   
if (isset($_POST['name'])){
        $mail
->FromName = $_POST['name'];
   
}else{
        $mail
->FromName = "Site Visitor";
   
}


   
foreach ($addresses[0] as $key => $value) {
        $mail
->addAddress($value[0]);
   
}


    $mail
->CharSet = 'utf-8';
    $mail
->Subject = $subject;
    $mail
->MsgHTML($template);


   
if (isset($_FILES['attachment'])) {
       
foreach ($_FILES['attachment']['error'] as $key => $error) {
           
if ($error == UPLOAD_ERR_OK) {
                $mail
->AddAttachment($_FILES['attachment']['tmp_name'][$key], $_FILES['Attachment']['name'][$key]);
           
}
       
}
   
}


    $mail
->send();


   
die('MF000');
} catch (phpmailerException $e) {
   
die('MF254');
} catch (Exception $e) {
   
die('MF255');
}


?>



Found sollution

Now i have found an article how to integrate correctly Google reCAPTCHA
in a RD Mail Form and i have insert the coding accordingly it:


Answere:

No you don't need any library just get recaptcha sending information when you handle form submission.

<?php
$secretKey
= "Your secret key when you register for recaptcha";

if(!empty($_POST['g-recaptcha-response'])){
    $response
= json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=". $secretKey. "&response=" . $_POST['g-recaptcha-response']));

   
if (!$response->success) { // There is a problem implement your logic!
   
}
}

Updated : // standard.html :


<div id="recaptcha" class="g-recaptcha" data-lang="tr"></div>
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" async defer></script>
<script>
    grecaptcha.render('recaptcha', {
      'sitekey' : 'yourSiteKey',
      'theme' : 'light'
    });
</script>


Comments:

A: So do I simply just add the above code to my existing .php file? 
B: Yep and use required js stuff in your form template.
C: Esatoglu Can you please elaborate on "js stuff"? 
B: BrownBag24 please check my answer, I updated and added related view code. 
C: so I added the above code to my existing php file and added the standard html to my contact form,
    but it does not seem to work yet. When I check the reCaptcha checkbox and click the submit button,
    I do receive the email, but with a very long reCaptcha response. And when I don't click the reCaptcha checkbox and then click on the submit button, I still receive the email.      
    Thoughts?



Code snippets sollution 

So I insert this code accordingly and here are the code snippets.

rd-mailfrom.php file (line 3):

<?php


$secretKey
= "6Lddhx0TAAAAAEZEsoSwbPGy81VoIoykpnfKE9BH";


if(!empty($_POST['g-recaptcha-response'])){
    $response
= json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=". $secretKey. "&response=" . $_POST['g-recaptcha-response']));


   
if (!$response->success) {
       
// There is a problem implement your logic!
   
}
}

index.php (line 84-94):

 <!-- RD Mailform -->
            <form class='rd-mailform' method="post" action="
<?php echo base_url();?>bat/rd-mailform.php">
             
<!-- RD Mailform Type -->
             
<input type="hidden" name="form-type" value="contact"/>
             
<!-- END RD Mailform Type -->
             
<fieldset>
               
<label data-add-placeholder>
                 
<input type="text"
                         
name="name"
                         
placeholder="Naam"
                         
data-constraints="@NotEmpty @LettersOnly"/>
               
</label>


               
<label data-add-placeholder>
                 
<input type="text"
                         
name="Achternam"
                         
placeholder="Last Name:"
                         
data-constraints="@NotEmpty @LettersOnly"/>
               
</label>


               
<label data-add-placeholder>
                 
<input type="text"
                         
name="phone"
                         
placeholder="Telefoonnummer"
                         
data-constraints="@NotEmpty @Phone"/>
               
</label>


               
<label data-add-placeholder>
                 
<input type="text"
                         
name="email"
                         
placeholder="E-mailadres:"
                         
data-constraints="@NotEmpty @Email"/>
               
</label>


                 
<label data-add-placeholder>
                 
<select name="select">
                   
<option disabled selected>Kies je BodyCheck brochure(s)</option>
                   
<option value="Preventief Medisch Onderzoek">Preventief Medisch Onderzoek</option>
                   
<option value="Sport Medisch Onderzoek">Sport Medisch Onderzoek</option>
                   
<option value="e-PMO & e-SMO">e-PMO & e-SMO</option>
                 
</select>
               
</label>
               
<br>
               
               
<!-- Google reCAPTCHA -->
               
<div id="recaptcha" class="g-recaptcha" data-lang="tr"></div>
               
<script src="https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit" async defer></script>
               
<script>
                    grecaptcha
.render('recaptcha', {
                     
'sitekey' : '6Lddhx0TAAAAAK2Y_LFjZ-ACBVXkdJMdDFktq7R8',
                     
'theme' : 'light'
                   
});
               
</script>
               
</br>
               
<!-- END Google reCAPTCHA -->
             
               
<p><button class="btn btn-xl btn-primary text-contrast bold" type="submit">Brochure aanvragen</button></p>
               
<div class="mfInfo"></div>
             
</fieldset>
           
</form>
           
<!-- END RD Mailform -->



Case issue

I have the same problem issue as the last comment above:

"C: so I added the above code to my existing php file and added the standard html to my contact form, 
    but it does not seem to work yet. When I check the reCaptcha checkbox and click the submit button, 
    I do receive the email, but with a very long reCaptcha response. And when I don't click the reCaptcha checkbox and then click on the submit button, I still receive the email.      
    Thoughts?"


The reCaptcha appears above the button 'Brochure aanvragen' on the 1st form
on the home page, but sending in the form without selecting the reCAPTCHA
is still possible?!

I receive this mail notification with a long code at the reCAPTCHA response:

Notification
Hi, someone left a message for you at j-test2.nl
A message from your site visitor
Email:ws...@m.nl
Name:dfdf
Achternam:zdf
Phone:0444445242
Select:Kies je BodyCheck brochure(s)
G-recaptcha-response:03AHJ_VuuHmg-3BaleYInMWOhYK6-U1SnD62sCp0xpMID7EMmCVlYWinbFPAS6w-acLSWVjuuGMM62mXHR0s2VIwILsaceC8JA9mQqNa_ZKy_x_NVKwfwMC72mFvUUIO4NGW2goibRJ95fVZ4DtofWPhNf7T5IpAYco1xEe7hq1bmm28epxItDvH_lpn-uP8hTMfJrSHEiQIaWzuBxFSz5ujBxV_eFqSSIKZ5eJJ0ZU1kOYyvF4KN1bnVMLJO6hT16_2vwr8IZ8leytVfuPF3DN0k1MPBvcD0OcZPfoDu4kgwZXWCIg1-kDl--c5XnoyVOy0WhR4g0KPsiKRd0EzMkJF0nwfjC_Alk2rVRDLEcF2Q7WBm9If-7y996V-C2Ji93MiWvwudmah5lg3RtR2QYI_RxEI10YsF0JFhaZSFBopnM6T_X6RFi_JRatdqE3PTLrzgMfwna7xRm0m7wiEtXdUw8Q3nkxJXcR9J4T5vgbr7DjLS_qv_OsbQD-_1DiAqZVC54_BbBAEmor8sloKLnD4Aaq0we2CnjI8sPbsU2XC6ucsxl-n7yxvgisH_d4_Exk_rbyiM2_uHv-EtzXlz_gNme94H0tUzuXBeRLWVPCbU2Oq5RlNb38NGx1ZVHG31np3bSNsBsJHRkyieNEut8Kbdacxm5ESrhMjGAM8FrHbhWRbcah66l5thKTK4sPDyRPJFTQ6lwp-gZKvhDkaKxyz8EK6-skrgoVaO0V5IpPpzFfoNmL2_GDOKv7qBB-Tnd_3NGFgygcZ52W8pd9fP9s_hPZse8MB0w1LKEZDZexzOA7m48suBZzIUV1iRLDAXSeyRQAlePoH5sLqssGpZseWRXxEiztNgwTR0DVR5BzgzxaZw_J_a6mbpm66jM5HocMlulN6GiMPWMO_S0K_zo2FvZeLMViGEJkLoHiSPknAr63e5vgFik70I
This is an automatically generated email, please do not reply.


When i send in the form selecting the reCAPTCHA it send the mail like this:

Notification
Hi, someone left a message for you at j-test2.nl
A message from your site visitor
Email:a...@g.nl
Name:aaa
Achternam:sss
Phone:0612345679
Select:Kies je BodyCheck brochure(s)
This is an automatically generated email, please do not reply.


I am looking forwards to your reply on how in make the reCAPTCHA work right in my website.
Thanks in advance!

Regards,

Joris

TV Antenna and Wall mount Melbourne

unread,
Oct 14, 2016, 12:45:23 AM10/14/16
to reCAPTCHA
Hi Joris

I have the same problem as you explained  above, i need to know have you got any solution for that, if so can you please share
thanks in advance
Paul

school...@gmail.com

unread,
Oct 20, 2016, 8:06:14 PM10/20/16
to reCAPTCHA
This is correct. You need to ensure that the recaptcha is a required field.

Andreas Ropella

unread,
Jan 21, 2017, 3:38:36 AM1/21/17
to reCAPTCHA, school...@gmail.com
Thank you for your answer.

Can you please tell us how?
there are a lot of files to change?


Cause this is exactly the Point and what everybody is looking for :-)

Thank you for your answer

Shane Cahill

unread,
Jun 13, 2017, 8:41:00 PM6/13/17
to reCAPTCHA, school...@gmail.com
Hi guys,

I have the exact same problem. Did anyone get a proper working solution.

Thanks in advance.

Mauricio RM

unread,
Aug 2, 2017, 9:08:56 PM8/2/17
to reCAPTCHA, school...@gmail.com
Have you figured out?

It is a little tricky but It works!!!
Reply all
Reply to author
Forward
0 new messages