Help with a PHP contact form please

Hi,

I have been using the below code for my contact form. However I need to make two ammendments if possible please but I can’t figure out how to do it myself. Lots of google’ing but I didn’t get a working solution with all the things I tried :frowning:

I want to add in a check box below the message box and before the submit button which if a user ticks the box it will send a copy of the enquiry to them in a seperate email addressed just to them from with the subject line of “Your website enquiry” but continue to send the current email I get to me.

Also presently when the user presses submit the thank you message is displayed just above the form however the form isn’t cleared. Could the form be cleared on successful submission?

I would be most grateful for any help and even more so if you could bold the code you have added in for me to learn from.

Thanks in advance, Aaron

[php]<?php
// OPTIONS - PLEASE CONFIGURE THESE BEFORE USE!

$yourEmail = "[email protected]"; // the email address you wish to receive these mails through
$yourWebsite = “domain.com”; // the name of your website
$thanksPage = ‘’; // URL to ‘thanks for sending mail’ page; leave empty to keep message on the same page
$maxPoints = 4; // max points a person can hit before it refuses to submit - recommend 4
$requiredFields = “name,comments”; // names of the fields you’d like to be required as a minimum, separate each field with a comma
date_default_timezone_set(“Europe/London”);

// DO NOT EDIT BELOW HERE
$error_msg = null;
$result = null;

$requiredFields = explode(",", $requiredFields);

function clean($data) {
$data = trim(stripslashes(strip_tags($data)));
return $data;
}
function isBot() {
$bots = array(“Indy”, “Blaiz”, “Java”, “libwww-perl”, “Python”, “OutfoxBot”, “User-Agent”, “PycURL”, “AlphaServer”, “T8Abot”, “Syntryx”, “WinHttp”, “WebBandit”, “nicebot”, “Teoma”, “alexa”, “froogle”, “inktomi”, “looksmart”, “URL_Spider_SQL”, “Firefly”, “NationalDirectory”, “Ask Jeeves”, “TECNOSEEK”, “InfoSeek”, “WebFindBot”, “girafabot”, “crawler”, “www.galaxy.com”, “Googlebot”, “Scooter”, “Slurp”, “appie”, “FAST”, “WebBug”, “Spade”, “ZyBorg”, “rabaz”);

foreach ($bots as $bot)
	if (stripos($_SERVER['HTTP_USER_AGENT'], $bot) !== false)
		return true;

if (empty($_SERVER['HTTP_USER_AGENT']) || $_SERVER['HTTP_USER_AGENT'] == " ")
	return true;

return false;

}

if ($_SERVER[‘REQUEST_METHOD’] == “POST”) {
if (isBot() !== false)
$error_msg .= "No bots please! UA reported as: ".$_SERVER[‘HTTP_USER_AGENT’];

// lets check a few things - not enough to trigger an error on their own, but worth assigning a spam score.. 
// score quickly adds up therefore allowing genuine users with 'accidental' score through but cutting out real spam :)
$points = (int)0;

$badwords = array("all", "bad", "words", "here");

foreach ($badwords as $word)
	if (
		strpos(strtolower($_POST['comments']), $word) !== false || 
		strpos(strtolower($_POST['name']), $word) !== false
	)
		$points += 2;

if (strpos($_POST['comments'], "http://") !== false || strpos($_POST['comments'], "www.") !== false)
	$points += 2;
if (isset($_POST['nojs']))
	$points += 1;
if (preg_match("/(<.*>)/i", $_POST['comments']))
	$points += 2;
if (strlen($_POST['name']) < 3)
	$points += 1;
if (strlen($_POST['comments']) < 15 || strlen($_POST['comments'] > 1500))
	$points += 2;
// end score assignments

foreach($requiredFields as $field) {
	trim($_POST[$field]);
	
	if (!isset($_POST[$field]) || empty($_POST[$field]))
		$error_msg .= "Please fill in all the required fields and submit again.\r\n";
}

if (!preg_match("/^[a-zA-Z-'\s]*$/", stripslashes($_POST['name'])))
	$error_msg .= "The name field must not contain special characters.\r\n";
if (!preg_match('/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])(([a-z0-9-])*([a-z0-9]))+' . '(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i', strtolower($_POST['email'])))
	$error_msg .= "That is not a valid e-mail address.\r\n";
if (!empty($_POST['url']) && !preg_match('/^(http|https):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i', $_POST['url']))
	$error_msg .= "Invalid website url.\r\n";

if ($error_msg == NULL && $points <= $maxPoints) {
	$subject = "New website enquiry";
	
	$message = "You received this e-mail message through your website: \n\n";
	foreach ($_POST as $key => $val) {
		$message .= ucwords($key) . ": " . clean($val) . "\r\n";
	}
	$message .= "\r\n";
	$message .= 'IP: '.$_SERVER['REMOTE_ADDR']."\r\n";
	$message .= 'Browser: '.$_SERVER['HTTP_USER_AGENT']."\r\n";
	$message .= 'Points: '.$points;

	if (strstr($_SERVER['SERVER_SOFTWARE'], "Win")) {
		$headers   = "From: $yourEmail\n";
		$headers  .= "Reply-To: {$_POST['email']}";
	} else {
		$headers   = "From: $yourWebsite <$yourEmail>\n";
		$headers  .= "Reply-To: {$_POST['email']}";
	}

	if (mail($yourEmail,$subject,$message,$headers)) {
		if (!empty($thanksPage)) {
			header("Location: $thanksPage");
			exit;
		} else {
			$result = 'Your information was successfully sent and we will be in contact shortly. We aim to respond to all queries within 24 hours.';
			$disable = true;
		}
	} else {
		$error_msg = 'We are sorry your information not be sent this time. Please try again or alternativley phone or e-mail us. ['.$points.']';
	}
} else {
	if (empty($error_msg))
		$error_msg = 'Your message looks too much like spam, and could not be sent this time. ['.$points.']';
}

}
function get_data($var) {
if (isset($_POST[$var]))
echo htmlspecialchars($_POST[$var]);
}
?>[/php]

I made some changes on your mail function

[php]
//i made some changes here
$emailcopy = $_POST[‘email’];
if (mail($yourEmail,$subject,$message,$headers))
{
//will send an extra message if the checkbox was checked
//make sure you change everything according to your need
if isset(($_POST[‘CheckBoxName’])){
(mail($emailcopy ,“Your website enquiry”,$message,$headers));
}
if (!empty($thanksPage))
{
header(“Location: $thanksPage”);
exit;
} else {
$result = ‘Your information was successfully sent and we will be in contact shortly. We aim to respond to all queries within 24 hours.’;
$disable = true;
}
} else {
$error_msg = ‘We are sorry your information not be sent this time. Please try again or alternativley phone or e-mail us. [’.$points.’]’;
}

[/php]

to send a different copy to the customers you need to have different variables to store what you gonna send if checkbox was checked. in your mail function you can see that i checked to see if the checkbox was checked and then sendanother message, keep doing that for the rest you need a new variable to store message so that you dont send your original message which contains ip and everything whicn dont need to see

Hi,

Many thanks for your reply. Really appreciate your time.

I am lost in so much that in Dreamweaver I inserted a checkbox with an ID of “EmailClientCopy” and changed [php]if isset(($_POST[‘CheckBoxName’])){[/php] to [php]if isset(($_POST[‘EmailClientCopy’])){[/php]

and pasted the code in. It didn’t work, I think I may have posted to code in the wrong place or I’ve missed something. Would you be so kind to insert your code into the original full length document with comments before and after like you done below saying it’s the new additions so i can see where?

The only fields I wish to email the client are the a subject line which I can customise and in the body a bit of text that says “Your message you submitted:” and on the line below there message from the messagebox if that makes sense?Please excuse my ignorance, I’m new to PHP

Many thanks for your help so far! And I’d really appreciate it if you could help me further :slight_smile: Aaron

this what i have
[php]

<?php // OPTIONS - PLEASE CONFIGURE THESE BEFORE USE! $yourEmail = "[email protected]"; // the email address you wish to receive these mails through $yourWebsite = "domain.com"; // the name of your website $thanksPage = ''; // URL to 'thanks for sending mail' page; leave empty to keep message on the same page $maxPoints = 4; // max points a person can hit before it refuses to submit - recommend 4 $requiredFields = "name,comments"; // names of the fields you'd like to be required as a minimum, separate each field with a comma date_default_timezone_set("Europe/London"); // DO NOT EDIT BELOW HERE $error_msg = null; $result = null; $requiredFields = explode(",", $requiredFields); function clean($data) { $data = trim(stripslashes(strip_tags($data))); return $data; } function isBot() { $bots = array("Indy", "Blaiz", "Java", "libwww-perl", "Python", "OutfoxBot", "User-Agent", "PycURL", "AlphaServer", "T8Abot", "Syntryx", "WinHttp", "WebBandit", "nicebot", "Teoma", "alexa", "froogle", "inktomi", "looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory", "Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot", "crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz"); foreach ($bots as $bot) if (stripos($_SERVER['HTTP_USER_AGENT'], $bot) !== false) return true; if (empty($_SERVER['HTTP_USER_AGENT']) || $_SERVER['HTTP_USER_AGENT'] == " ") return true; return false; } if ($_SERVER['REQUEST_METHOD'] == "POST") { if (isBot() !== false) $error_msg .= "No bots please! UA reported as: ".$_SERVER['HTTP_USER_AGENT']; // lets check a few things - not enough to trigger an error on their own, but worth assigning a spam score.. // score quickly adds up therefore allowing genuine users with 'accidental' score through but cutting out real spam :) $points = (int)0; $badwords = array("all", "bad", "words", "here"); foreach ($badwords as $word) if ( strpos(strtolower($_POST['comments']), $word) !== false || strpos(strtolower($_POST['name']), $word) !== false ) $points += 2; if (strpos($_POST['comments'], "http://") !== false || strpos($_POST['comments'], "www.") !== false) $points += 2; if (isset($_POST['nojs'])) $points += 1; if (preg_match("/(<.*>)/i", $_POST['comments'])) $points += 2; if (strlen($_POST['name']) < 3) $points += 1; if (strlen($_POST['comments']) < 15 || strlen($_POST['comments'] > 1500)) $points += 2; // end score assignments foreach($requiredFields as $field) { trim($_POST[$field]); if (!isset($_POST[$field]) || empty($_POST[$field])) $error_msg .= "Please fill in all the required fields and submit again.\r\n"; } if (!preg_match("/^[a-zA-Z-'\s]*$/", stripslashes($_POST['name']))) $error_msg .= "The name field must not contain special characters.\r\n"; if (!preg_match('/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])(([a-z0-9-])*([a-z0-9]))+' . '(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i', strtolower($_POST['email']))) $error_msg .= "That is not a valid e-mail address.\r\n"; if (!empty($_POST['url']) && !preg_match('/^(http|https):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i', $_POST['url'])) $error_msg .= "Invalid website url.\r\n"; if ($error_msg == NULL && $points <= $maxPoints) { $subject = "New website enquiry"; $message = "You received this e-mail message through your website: \n\n"; foreach ($_POST as $key => $val) { $message .= ucwords($key) . ": " . clean($val) . "\r\n"; } $message .= "\r\n"; $message .= 'IP: '.$_SERVER['REMOTE_ADDR']."\r\n"; $message .= 'Browser: '.$_SERVER['HTTP_USER_AGENT']."\r\n"; $message .= 'Points: '.$points; if (strstr($_SERVER['SERVER_SOFTWARE'], "Win")) { $headers = "From: $yourEmail\n"; $headers .= "Reply-To: {$_POST['email']}"; } else { $headers = "From: $yourWebsite <$yourEmail>\n"; $headers .= "Reply-To: {$_POST['email']}"; } //i made some changes here $emailcopy = $_POST['email']; if (mail($yourEmail,$subject,$message,$headers)) { //will send an extra message if the checkbox was checked //make sure you change everything according to your need if isset(($_POST['CheckBoxName'])){ (mail($emailcopy ,"Your website enquiry",$message,$headers)); } if (!empty($thanksPage)) { header("Location: $thanksPage"); exit; } else { $result = 'Your information was successfully sent and we will be in contact shortly. We aim to respond to all queries within 24 hours.'; $disable = true; } } else { $error_msg = 'We are sorry your information not be sent this time. Please try again or alternativley phone or e-mail us. ['.$points.']'; } } else { if (empty($error_msg)) $error_msg = 'Your message looks too much like spam, and could not be sent this time. ['.$points.']'; } } function get_data($var) { if (isset($_POST[$var])) echo htmlspecialchars($_POST[$var]); } ?>

[/php]

By the way i did not test this and is not yet complete. you need a different message for the copy email

Thanks :slight_smile:

I will try and work out how to do some of the other bits I’m after. It’s giving me an error on this line though even when I change the check box name to the one i’ve inserted in dreamweaver?

[php]if isset(($_POST[‘CheckBoxName’])){[/php]

sorry my mistake

try
[php]
if (isset($_POST[‘CheckBoxName’])){
[/php]

NOTE: CheckBoxName means you should put the name you put on your checkbox

Thanks!

Any the line below, when I comment it out it sends the email to just me, when I leave it active it only sends the message to the user? Any ideas?

[php]$emailcopy = $_POST[‘email’];[/php]

you should not comment it
that line gets the email to whom send the copy to.

so its only sending one email ?

umm insteresting , do you have skype so i can help you better?

Hi,

Thanks for your reply. Yeah sure, my skype username is aaronp91

Thank you for this! Appreciate it! Look forward to your call. Aaron

Hii,

I added you call me when you get on skype

Thanks for all your help Wilson!

Had a Skype call today with a remote desktop session and all resolved. Top guy and a credit to this forum. Thanks.

Haha Thank you

glad i could help

Very glad to meet everybody here,I am just a new member of this forum,I want to make more friends here,I hope you can like me.


share love,share happy

Sponsor our Newsletter | Privacy Policy | Terms of Service