Recaptcha V2

I have a contact form that isnt working, it has Recaptcha V2, I am very new to websites and cant work out where to put the Recapthcha code to make it work ! my php page is below, any help is much appreciated

<?php 


	/* ==========================  Define variables ========================== */

	#Your e-mail address
	define("__TO__", "MY EMAIL ADDRESS");


	#Message subject
	define("__SUBJECT__", "Enquiry FROM MY WEBSITE");

	#Success message
	define('__SUCCESS_MESSAGE__', "Your message has been sent. Thank you!");

	#Error message 
	define('__ERROR_MESSAGE__', "Error, your message hasn't been sent");

	#Messege when one or more fields are empty
	define('__MESSAGE_EMPTY_FIELDS__', "Please fill out  all fields");

	/* ========================  End Define variables ======================== */

	//Send mail function
	function send_mail($to,$subject,$message,$headers){
		if(@mail($to,$subject,$message,$headers)){
			echo json_encode(array('info' => 'success', 'msg' => __SUCCESS_MESSAGE__));
		} else {
			echo json_encode(array('info' => 'error', 'msg' => __ERROR_MESSAGE__));
		}
	}

	//Check e-mail validation
	function check_email($email){
		if(!@eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
			return false;
		} else {
			return true;
		}
	}

	//Get post data
	if(isset($_POST['name']) and isset($_POST['mail']) and isset($_POST['comment'])){
		$name 	 = $_POST['name'];
		$mail 	 = $_POST['mail'];
		$phone  = $_POST['phone'];
		$comment = $_POST['comment'];

		if($name == '') {
			echo json_encode(array('info' => 'error', 'msg' => "Please enter your name."));
			exit();
		} else if($mail == '' or check_email($mail) == false){
			echo json_encode(array('info' => 'error', 'msg' => "Please enter valid e-mail."));
			exit();
		} else if($comment == ''){
			echo json_encode(array('info' => 'error', 'msg' => "Please enter your message."));
			exit();
		} else {
			//Send Mail
			$to = __TO__;
			$subject = __SUBJECT__ . ' ' . $name;
			$message = '
			<html>
			<head>
			  <title>Mail from '. $name .'</title>
			</head>
			<body>
			  <table style="width: 500px; font-family: arial; font-size: 14px;" border="1">
				<tr style="height: 32px;">
				  <th align="right" style="width:150px; padding-right:5px;">Name:</th>
				  <td align="left" style="padding-left:5px; line-height: 20px;">'. $name .'</td>
				</tr>
				<tr style="height: 32px;">
				  <th align="right" style="width:150px; padding-right:5px;">E-mail:</th>
				  <td align="left" style="padding-left:5px; line-height: 20px;">'. $mail .'</td>
				</tr>
				<tr style="height: 32px;">
				  <th align="right" style="width:150px; padding-right:5px;">Phone:</th>
				  <td align="left" style="padding-left:5px; line-height: 20px;">'. $phone .'</td>
				</tr>
				<tr style="height: 32px;">
				  <th align="right" style="width:150px; padding-right:5px;">Message:</th>
				  <td align="left" style="padding-left:5px; line-height: 20px;">'. $comment .'</td>
				</tr>
			  </table>
			</body>
			</html>
			';

			$headers  = 'MIME-Version: 1.0' . "\r\n";
			$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
			$headers .= 'From: ' . $mail . "\r\n";

			send_mail($to,$subject,$message,$headers);
		}
	} else {
		echo json_encode(array('info' => 'error', 'msg' => __MESSAGE_EMPTY_FIELDS__));
	}
	
	
	
 ?>
1 Like

define not working. More info is better than a code dump and a generic statement.

Aston apologies this is all fairly new to me, we have a contact page on the website which was working some time ago but now does not, i didnt build the site or set it up but have been left trying to keep things rolling.
the contact page no longer sends the contact details through, having done some research I think the Recapthcha has probably never worked although we were getting messages at some point in the past, having looked at the Html contact page and PHP code I cant see the site private key for the Recaptcha ? but unfortunately this is all new to me and am not sure where it should go and exactly what version of the code is required ? hence my code dump (sorry)
The web site URL is
http://www.burgin-air-conditioning.co.uk/

I have been looking on the web but not managed to comer up with a solution thats how I found this site.

To clarify, doesn’t send at all? The mail function will routinely fail or messages will end up in spam/ junk folders.

No it just doesnt send at all, I spoke to the hosting team for the site and they say everything seems fine with our site.
This was working say 6- 9 months ago which is a mystery as we havent touched the site to my knowledge

first thing first, comment out the section that cares about the recaptcha and test to see if it works again. Isolate the actual issue so you know what needs fixing.

The posted php code doesn’t contain any logic to test for a successful captcha, so it will process any post data submitted to it.

The posted code contains two immediate issues that will prevent it from working -

  1. Uses the obsolete/removed eregi() statement. This would cause a fatal runtime error and the client-side ajax code will either receive the error output or a http 500 response. What does occur in the browser when you submit the form?
  2. Except perhaps during testing, these emails are NOT being sent From the email address that someone enters in the form. They are being sent from the mail server at your web hosting and the From: email address must be related to your sending mail server. This would result in either your sending mail server or your receiving mail server, that corresponds to the To: address, to produce an error, putting the emails into the spam/junk folder, and eventually black-listing the sending mail server.

Here’s the response you are getting back in the browser’s developer/network tab -

<br />
<b>Fatal error</b>:  Uncaught Error: Call to undefined function eregi() in /home/deltatel/public_html/burgin-air-conditioning.co.uk/contact.php:34
Stack trace:
#0 /home/deltatel/public_html/burgin-air-conditioning.co.uk/contact.php(51): check_email('[email protected]...')
#1 {main}
  thrown in <b>/home/deltatel/public_html/burgin-air-conditioning.co.uk/contact.php</b> on line <b>34</b><br />

You can replace the eregi call with a call to php’s filter_var() function using the FILTER_VALIDATE_EMAIL validate filter.

Thanks for this, how long ago did this become obsolete ? as this may be when we stopped receiving emails ?

From the php.net documentation -

Warning
This function was DEPRECATED in PHP 5.3.0, and REMOVED in PHP 7.0.0.

Php 5.3 was released on - 30 June 2009
Php 7.0 was released on - 3 December 2015

Your web host probably recently updated the php version to 7+.

Thanks phdr i just tried changing the php version in my control panel and now its working again, just need to make the code change to then upgrade to the newest version, theres an update tomorrow i beleive to the newest version of php. at least its working thanks for your help i appreciate it :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service