Hello all,

The following captcha script works fine, but only with $name, $email and $message.

As you can see I have added $bname, $add, $postcode etc.

My Question is, how/where do I amend the script to include those new variables in the email that has sent?

I have played about with the line

mail("paul.white1978@hotmail.com","Email Subject ","Name: $name\n Email: $email\n\n Message:$message","From: $email");

, but have had no joy.

Here is the full script:

<?php 
	session_start();
?>
<?php
 	if (isset($_POST['submit'])) {
		if (isset($_SESSION['captcha_keystring']) && $_SESSION['captcha_keystring'] ==  $_POST['keystring']) {
			 // Grab the form vars
			 $name = (isset($_POST['name'])) ? $_POST['name'] : '' ;			 
			 $email = (isset($_POST['email'])) ? $_POST['email'] : '' ;
			 $org = (isset($_POST['org'])) ? $_POST['org'] : '' ;
			 $bname = (isset($_POST['bname'])) ? $_POST['bname'] : '' ;
			 $add1 = (isset($_POST['add1'])) ? $_POST['add1'] : '' ;
			 $add2 = (isset($_POST['add2'])) ? $_POST['add2'] : '' ;
			 $county = (isset($_POST['county'])) ? $_POST['county'] : '' ;
			 $pcode = (isset($_POST['pcode'])) ? $_POST['pcode'] : '' ;
			 $message = (isset($_POST['message'])) ? $_POST['message'] : '' ;
			 
			 // Check for email injection
			 if (has_emailheaders($email)) {
			  die("Possible email injection occuring");
			 }
			 
			 mail("paul.white1978@hotmail.com","Email Subject ","Name: $name\n Email: $email\n\n Message:$message","From: $email");


 		    $message = 'Your message has been recieved';
    	} else {
    		$message = 'The word verification was not correct.  Please press back and resubmit.';
    	}
	} else {
		$message = 'Please use the contact form for communication';
	}
	
function has_emailheaders($text) {
	return preg_match("/(%0A|%0D|\n+|\r+)(content-type:|to:|cc:|bcc:)/i", $text, $org);
}
?>
</head>
<body>
 <div style="text-align:center">
  <h1>Your message has been sent</h1>
  <h2><?php echo $message; ?></h2>
 </div>
</body></html>

Replace with this code should work:

<?php 
	session_start();
?>
<?php
 	if (isset($_POST['submit'])) {
		if (isset($_SESSION['captcha_keystring']) && $_SESSION['captcha_keystring'] ==  $_POST['keystring']) {
			 // Grab the form vars
			 $name = (isset($_POST['name'])) ? $_POST['name'] : '' ;			 
			 $email = (isset($_POST['email'])) ? $_POST['email'] : '' ;
			 $org = (isset($_POST['org'])) ? $_POST['org'] : '' ;
			 $bname = (isset($_POST['bname'])) ? $_POST['bname'] : '' ;
			 $add1 = (isset($_POST['add1'])) ? $_POST['add1'] : '' ;
			 $add2 = (isset($_POST['add2'])) ? $_POST['add2'] : '' ;
			 $county = (isset($_POST['county'])) ? $_POST['county'] : '' ;
			 $pcode = (isset($_POST['pcode'])) ? $_POST['pcode'] : '' ;
			 $message = (isset($_POST['message'])) ? $_POST['message'] : '' ;
			 
			 // Check for email injection
			 if (has_emailheaders($email)) {
			  die("Possible email injection occuring");
			 }
			 
			 $msgBody="Name: $name\n BNameEmail: $bname\nAdd: $add\n $email\n\n Message:$message"
			 mail("paul.white1978@hotmail.com","Email Subject ", $msgBody, "From: $email");


 		    $message = 'Your message has been recieved';
    	} else {
    		$message = 'The word verification was not correct.  Please press back and resubmit.';
    	}
	} else {
		$message = 'Please use the contact form for communication';
	}
	
function has_emailheaders($text) {
	return preg_match("/(%0A|%0D|\n+|\r+)(content-type:|to:|cc:|bcc:)/i", $text, $org);
}
?>
</head>
<body>
 <div style="text-align:center">
  <h1>Your message has been sent</h1>
  <h2><?php echo $message; ?></h2>
 </div>
</body></html>

To change the email edit $msgBody, use \n for a new line and just add the variable name to insert it into the email

Hi,

thanks for your help on this.

Im getting the following error message:

Parse error: syntax error, unexpected T_STRING in (location) on line 24

line 24 is

mail("paul.white1978@hotmail.com","Email Subject", "From: $email", $msgBody);

I have tried $msgbody with and without brackets.

Thanks

Paul

I'd use phpmailer or Zend Framework's Zend_Mail.
Your mail script could be (ab)used by spammers by injecting additional parameters e.g. "To: ", "Bcc: " etc !

Hi,

thanks for your help on this.

Im getting the following error message:

Parse error: syntax error, unexpected T_STRING in (location) on line 24

line 24 is

mail("paul.white1978@hotmail.com","Email Subject", "From: $email", $msgBody);

I have tried $msgbody with and without brackets.

Thanks

Paul

Please correctly read my code, the lines added were

$msgBody="Name: $name\n BNameEmail: $bname\nAdd: $add\n $email\n\n Message:$message"			 mail("paul.white1978@hotmail.com","Email Subject ", $msgBody, "From: $email");

not

mail("paul.white1978@hotmail.com","Email Subject", "From: $email", $msgBody);
This article has been dead for over six months. Start a new discussion instead.