Remove G-recaptcha from email

I’m using a php form and was wondering what can be done to not show the

G-recaptcha-response: 03AEMEkEmHd5Aa1c2w2mYgaC4NIgM2dVw_q6CpR0ZjFDpZw…

in the actual email that is sent. I just want the regular form fields to show up in the email.

I posted the same question here with the code:

https://stackoverflow.com/questions/51604904/g-recaptcha-response-shows-up-in-message

Any help would be appreciated.

Well, just look at the code where it builds the email and remove the section that displays the code.
For us to help you, you would need to place the email creation code into a blockquote here so we can review the code and show you where it is displayed. Should be a simple fix…

I pasted the code here since it’s too long for this site:

https://codeshare.io/an3wZv

We don’t need the PHPMailer class, we need the code you use to send it. I’m also not fond of just a link to another forum. We have our own forum for a reason.

@astonecipher L O L Yep! I asked him for how he builds the email and he sends us the library…

Blazer, somewhere you call the PHPMailer class. You take your inputs and build the email and send it using the library. Post just that part of your code here so we can review it and help you. Help us to help you!

Sorry guys, i’m clueless when it comes to this lol

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/*
Tested working with PHP5.4 and above (including PHP 7 )

 */
require_once './vendor/autoload.php';

use FormGuide\Handlx\FormHandler;


$pp = new FormHandler(); 

$validator = $pp->getValidator();
$validator->fields(['Name','Email'])->areRequired()->maxLength(50);
$validator->field('Email')->isEmail();
$validator->field('Message')->maxLength(6000);


$pp->requireReCaptcha();
$pp->getReCaptcha()->initSecretKey('key#');

$mailer = $pp->getMailer();

//Using SMTP account
$mailer->IsSMTP();
$mailer->SMTPAuth   = true;
$mailer->SMTPSecure = false;		
$mailer->SMTPAutoTLS = false;
$mailer->Host       = "localhost";
$mailer->Username   = "[email protected]";
$mailer->Password   = "pwd";

$mailer->setFrom('[email protected]', 'Admin');


$pp->sendEmailTo('[email protected]'); // ← Your email here

echo $pp->process($_POST);

There’s also another file formhandler.php, which I think might have what you need:

<?php
namespace FormGuide\Handlx;
use FormGuide\PHPFormValidator\FormValidator;
use PHPMailer;
use FormGuide\Handlx\Microtemplate;
use Gregwar\Captcha\CaptchaBuilder;

/**
 * FormHandler 
 *  A wrapper class that handles common form handling tasks
 *  	- handles Form validations using PHPFormValidator class
 *  	- sends email using PHPMailer 
 *  	- can handle captcha validation
 *  	- can handle file uploads and attaching the upload to email
 *  	
 *  ==== Sample usage ====
 *   $fh = FormHandler::create()->validate(function($validator)
 *   		{
 *   	 		$validator->fields(['name','email'])
 *   	 				  ->areRequired()->maxLength(50);
 *   	       	$validator->field('email')->isEmail();
 *   	       	
 *           })->useMailTemplate(__DIR__.'/templ/email.php')
 *           ->sendEmailTo('[email protected]');
 *           
 *   $fh->process($_POST);
 */
class FormHandler
{
	private $emails;
	public $validator;
	private $mailer;
	private $mail_template;
	private $captcha;
	private $attachments;
	private $recaptcha;

	public function __construct()
	{
		$this->emails = array();
		$this->validator = FormValidator::create();
		$this->mailer = new PHPMailer;
		$this->mail_template='';

		$this->mailer->Subject = "Contact Form Submission ";

		$host = isset($_SERVER['SERVER_NAME'])?$_SERVER['SERVER_NAME']:'localhost';
        $from_email ='forms@'.$host;
   		$this->mailer->setFrom($from_email,'Contact Form',false);  

   		$this->captcha = false;   

   		$this->attachments = [];

   		$this->recaptcha =null;


	}

	/**
	 * sendEmailTo: add a recipient email address
	 * @param  string/array $email_s one or more emails. If more than one emails, pass the emails as array
	 * @return The form handler object itself so that the methods can be chained
	 */
	public function sendEmailTo($email_s)
	{
		if(is_array($email_s))
		{
			$this->emails =array_merge($this->emails, $email_s);
		}
		else
		{
			$this->emails[] = $email_s;	
		}
		
		return $this;
	}

	public function useMailTemplate($templ_path)
	{
		$this->mail_template = $templ_path;
		return $this;
	}

	/**
	 * [attachFiles find the file uplods and attach to the email]
	 * @param  array $fields The array of field names
	  */
	public function attachFiles($fields)
	{
		$this->attachments = array_merge($this->attachments, $fields);
		return $this;
	}

	public function getRecipients()
	{
		return $this->emails;
	}

	/**
	 * [validate add Validations. This function takes a call back function which receives the PHPFormValidator object]
	 * @param  function $validator_fn The funtion gets a validator parameter using which, you can add validations 
	 */
	public function validate($validator_fn)
	{
		$validator_fn($this->validator);
		return $this;
	}

	public function requireReCaptcha($config_fn=null)
	{
		$this->recaptcha = new ReCaptchaValidator();
		$this->recaptcha->enable(true);
		if($config_fn)
		{
			$config_fn($this->recaptcha);	
		}
		return $this;
	}
	public function getReCaptcha()
	{
		return $this->recaptcha;
	}

	public function requireCaptcha($enable=true)
	{
		$this->captcha = $enable;
		return $this;
	}

	public function getValidator()
	{
		return $this->validator;
	}

	public function configMailer($mailconfig_fn)
	{
		$mailconfig_fn($this->mailer);
		return $this;
	}

	public function getMailer()
	{
		return $this->mailer;
	}

	public static function create()
	{
		return new FormHandler();
	}

	public function process($post_data)
	{
		if($this->captcha === true)
		{
			$res = $this->validate_captcha($post_data);
			if($res !== true)
			{
				return $res;
			}
		}
		if($this->recaptcha !== null &&
		   $this->recaptcha->isEnabled())
		{
			if($this->recaptcha->validate() !== true)
			{
				return json_encode([
				'result'=>'recaptcha_validation_failed',
				'errors'=>['captcha'=>'ReCaptcha Validation Failed.']
				]);
			}
		}

		$this->validator->test($post_data);

		//if(false == $this->validator->test($post_data))
		if($this->validator->hasErrors())
		{
			return json_encode([
				'result'=>'validation_failed',
				'errors'=>$this->validator->getErrors(/*associative*/ true)
				]);
		}

		if(!empty($this->emails))
		{
			foreach($this->emails as $email)
			{
				$this->mailer->addAddress($email);
			}
			$this->compose_mail($post_data);

			if(!empty($this->attachments))
			{
				$this->attach_files();
			}

			if(!$this->mailer->send())
			{
				return json_encode([
					'result'=>'error_sending_email',
					'errors'=> ['mail'=> $this->mailer->ErrorInfo]
					]);			
			}
		}
		
		return json_encode(['result'=>'success']);
	}

	private function validate_captcha($post)
	{
		@session_start();
		if(empty($post['captcha']))
		{
			return json_encode([
						'result'=>'captcha_error',
						'errors'=>['captcha'=>'Captcha code not entered']
						]);
		}
		else
		{
			$usercaptcha = trim($post['captcha']);

			if($_SESSION['user_phrase'] !== $usercaptcha)
			{
				return json_encode([
						'result'=>'captcha_error',
						'errors'=>['captcha'=>'Captcha code does not match']
						]);		
			}
		}
		return true;
	}


	private function attach_files()
	{
		
		foreach($this->attachments as $file_field)
		{
			if (!array_key_exists($file_field, $_FILES))
			{
				continue;
			}
			$filename = $_FILES[$file_field]['name'];

    		$uploadfile = tempnam(sys_get_temp_dir(), sha1($filename));

    		if (!move_uploaded_file($_FILES[$file_field]['tmp_name'], 
    			$uploadfile))
    		{
    			continue;
    		}

    		$this->mailer->addAttachment($uploadfile, $filename);
		}
	}

	private function compose_mail($post)
	{
		$content = "Form submission: \n\n";
		foreach($post as $name=>$value)
		{
			$content .= ucwords($name).":\n";
			$content .= "$value\n\n";
		}
		$this->mailer->Body  = $content;
	}
}

Seems to me it’s running a loop that sees every form field and spits it out in the email. How do we exclude the g-recaptcha from this loop?

That is your issue.

So, tracking the code we have,

 $pp->process($_POST);

That ends up calling
compose_mail($post)

which iterates through the posted values and builds the body.

What I would recommend is a removal array. You list out the keys you don’t want included and then add an if statement to check if the value is allowable. Or the reverse, add the fields that you do want so only they are included. Something like,

$remove = [
    'submit',
   'G-recaptcha-response'
];

foreach($post as $name=>$value) {
    if(!in_array($name, $remove)){
        $content .= ucwords($name).":\n";
        $content .= "$value\n\n"; 
    }
}
1 Like

That worked! (except I had to make g-recaptcha-response in lower case :slight_smile:
Thank you!

Sponsor our Newsletter | Privacy Policy | Terms of Service