I'm trying to get this PHP mail form to work correctly and I keep getting an error:

PHP Parse error:  syntax error, unexpected T_STRING

from this line

mail($formproc["email"],$emsubj,$message,"From: $emfrm");

from this mail form PHP file

<?php

if (isset($_POST["formdata"])) {

	$formdata = $_POST["formdata"];

	foreach ($formdata as $key => $value) {

		$formresults .= sprintf("$key: $value\n");

	}

}

/*if data variables are not set properly send to error page*/

else {

	header("Location: http://www.site.edu/mail-form/error.html?error=data");

	exit("error");

}

//$missing = "";

//initialize found value

$foundblank = 0;

foreach ($formdata as $key => $value) {

	//check to see if there are excludes

	if (!isset($_POST["formproc"]["exclude"])) {

		//no excludes

		if ($value == "") {

			//field not filled in

			//update foundblank

			$foundblank = 1;

			//add to list

			$missing .= $key . ", ";

		}

	}

	else {

		//there's an exclude list

		$exclude = $_POST["formproc"]["exclude"];

		if (strpos($exclude, $key) === FALSE) {

			//if the field isn't in the exclude list

			if ($value == "") {

				//field not filled in

				//update foundblank

				$foundblank = 1;

				//add to list

				$missing .= $key . ", ";

			}

		}

		//no else needed - if its excluded don't check it

	}

}

if ($foundblank==1) {

//remove the last two characters since ther're a comma and space

$missing = substr($missing, 0, strlen($missing) - 2);

?>

	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

	<?php 

		ini_set('include_path', $_SERVER["DOCUMENT_ROOT"]);

		include_once('includes/scripts/class.breadcrumb.inc.php');$breadcrumb = new breadcrumb; 

	?>

	<html>

	<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

	<meta http-equiv="Content-Language" content="en-us" />

	<title>Site - Form Error</title>

	<meta name="keywords" content="" />

	<meta name="description" content="" />

	<script language="JavaScript" src="/includes/scripts/chooseCSS.js" type="text/JavaScript"></script><noscript><link 

type="text/css"href="/includes/styles.css" rel="StyleSheet" /></noscript>

	</head>

	<body>

	<?php include("includes/header1-openbody.html"); ?>

	<span class = "ouTitlepreview">

	<?php if ($showDescription<>"") { ?>

	</span>

	<table border="0" cellpadding="10" cellspacing="0" class="SectionTitle<?php if ($navBG<>'') { ?>2<?php } ?>" width="100%">

	<tr>

	<td class="SectionTitle<?php if ($navBG<>'') { ?>2<?php } ?>">Form Error</td>

	</tr>

	</table>

	<?php } ?>

	<div id="divMainBody">

	<?php include("includes/breadcrumb.html") ?>

	<table border="0" cellpadding="" cellspacing="15" class="BodyCopy" width="100%">

	<tr>

	<td>

	<table border="0" cellpadding="0" cellspacing="0" width="100%" ID="Table2">

	<tr>

	<td class="Header">Please Correct The Following Errors</td>

	</tr>

	<tr>

	<td bgcolor="#000000"><img alt="" border="0" height="1" width="1" src="/images/display/blank.gif" /></td>

	</tr>

	<tr>

	<td><img alt="" border="0" height="5" width="1" src="/images/display/blank.gif" /></td>

	</tr>

	<tr>

	<td class="SubHeader">Use Your Browsers Back Button to Resubmit</td>

	</tr>

	</table>

	</td>

	</tr>

	<tr>

	<td class="BodyCopy">

	<p>The Following Fields are empty and need to be corrected:<br/>

	<i><?php print($missing); ?></i>

	</p>	

	</td>

	</tr>

	</table>

	</div>

	<?php include("includes/footer.html") ?>

	</td></tr></table>

	<center><table width="731"><tr><td>

	</td></tr></table></center>

	</body>

	</html>

<?php

exit;

}//end $foundblank if

/*check for the success variable if it's not set issue a error message.*/

if (!isset($_POST["formproc"]["success"])) {

	header("Location: http://www.site.edu/mail-form/error.html?error=success");

	exit("error");

}

else {

	/* should be emailed - if not set send to error page */

	if (isset($_POST["formproc"]["email"])) {

		$formproc=$_POST["formproc"];

		/*must send to and site.edu address - don't allow otherwise*/

		if (strpos($formproc["email"], "site.edu") !== FALSE) {

			//Sender that shows up is web-form@site.edu

			$emfrm = "web-form";

			//default subject is Web Form

			$emsubj = "Web Form";

			/*Allow for customized subject: appends : <customized subject> to default subject Ex: 

			Web Form: For Something Specific*/
			
			if (isset($formproc["subject"])) $emsubj = $emsubj . ": " . $formproc["subject"];
			
			$message = '<html><head></head><body>$formresults</body></html>'

			//Sends email

			mail($formproc["email"],$emsubj,$message,"From: $emfrm");

			

			//After email - success page or results echo page

			$success = $_POST["formproc"]["success"];

			//Check success varaible - if not a URL

			//print out the echo page

			//since http:// is at position 0 it would evaluate to false

			//therefore leave off the h

			if (strpos($success, "http://") === FALSE) {

				//print out echo page of what the user entered

				foreach ($formdata as $key => $value) {

					$htmlresults .= sprintf("<strong>$key</strong>:<br/> $value<br/><br/>\n");

				}

				if (isset($formproc["contact"])) {

					$contact = $formproc["contact"];

					//if the contact variable is a URL

					//since http:// is at position 0 it would evaluate

					//to false - leave off the h

					if (strcasecmp($contact, "back") == 0) {

						$contacthtml = sprintf("If the above data is not what you wished to submit you may make corrections by pressing the back button in your browser and changing the necessary information then resubmitting the form.\n");

					}



					else if (strpos($contact, "ttp://") == TRUE) {

						$contacthtml = sprintf("If the above data is not what you wished to submit you may make <a href=\"$contact\">corrections</a>\n");

					}

					//if the contact variable is a email address

					else if (strpos($contact, "@") == TRUE) {

						$contacthtml = sprintf("If the above data is not what you wished to submit you may make <a href=\"mailto:$contact\" subject=\"Mail form corrections\">corrections</a> by indicating the necessary changes in your email.\n");

					}

					else {

						$contacthtml = sprintf("$contact\n");

					}

				}

				?>

					<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

					<?php 

						ini_set('include_path', $_SERVER["DOCUMENT_ROOT"]);

						include_once('includes/scripts/class.breadcrumb.inc.php');$breadcrumb = new breadcrumb; 

					?>

					<html>

					<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

					<meta http-equiv="Content-Language" content="en-us" />

					<title>Site - Form Results</title>

					<meta name="keywords" content="" />

					<meta name="description" content="" />

					<script language="JavaScript" src="/includes/scripts/chooseCSS.js" type="text/JavaScript"></script><noscript><link 

				type="text/css"href="/includes/styles.css" rel="StyleSheet" /></noscript>

					</head>

					<body>

					<?php include("includes/header1-openbody.html"); ?>

					<span class = "ouTitlepreview">

					<?php if ($showDescription<>"") { ?>

					</span>

					<table border="0" cellpadding="10" cellspacing="0" class="SectionTitle<?php if ($navBG<>'') { ?>2<?php } ?>" width="100%">

					<tr>

					<td class="SectionTitle<?php if ($navBG<>'') { ?>2<?php } ?>">Form Results</td>

					</tr>

					</table>

					<?php } ?>

					<div id="divMainBody">

					<?php include("includes/breadcrumb.html") ?>

					<table border="0" cellpadding="" cellspacing="15" class="BodyCopy" width="100%">

					<tr>

					<td>

					<table border="0" cellpadding="0" cellspacing="0" width="100%" ID="Table2">

					<tr>

					<td class="Header">You Have Successfully Submitted the Form</td>

					</tr>

					<tr>

					<td bgcolor="#000000"><img alt="" border="0" height="1" width="1" src="/images/display/blank.gif" /></td>

					</tr>

					<tr>

					<td><img alt="" border="0" height="5" width="1" src="/images/display/blank.gif" /></td>

					</tr>

					<tr>

					<td class="SubHeader">The Information You Have Submitted is Below</td>

					</tr>

					</table>

					</td>

					</tr>

					<tr>

					<td class="BodyCopy">

					<p>

					<?php print $htmlresults; ?>

					<?php if (isset($formproc["contact"])) print $contacthtml; ?>

					</p>	

					</td>

					</tr>

					</table>

					</div>

					<?php include("includes/footer.html") ?>

					</td></tr></table>

					<center><table width="731"><tr><td>

					</td></tr></table></center>

					</body>

					</html>

				<?php

				exit;

			}

			else {

				//the user specified a success page - send them to it

				//We checked the header above - show success after mail is sent

				header("Location: " . $success);

			}

		}

		else {

			header("Location: http://www.site.edu/mail-form/error.html?error=oswego");

			exit("error");

		}

	}

	else {

		header("Location: http://www.site.edu/mail-form/error.html?error=mail");

		exit("error");

	}

}

?>

I'm pretty new with PHP, i did a google search but didn't really find anything related. This form was working fine, but my boss wants it to submit the form to an email address with HTMl instead of plain text, to make it pretty. I add this line

$message = '<html><head></head><body>$formresults</body></html>'

and changed the '$formresults' variable in the line mentioned in the error to '$message'. Any and all help is much appreciated!!!! Thank you!

add a semicolon after this line

$message = '<html><head></head><body>$formresults</body></html>'

I did, and the form worked! But it didn't appear as HTML in my email, it appeared exactly as $message:

<html><head></head><body>$formresults</body></html>


Do you know how I can format this to show up as HTML in the email that is sent to me?

Thanks!!!

try changing this line:

mail($formproc["email"],$emsubj,$message,"From: $emfrm");

to this:

mail($formproc["email"],$emsubj,$message,'Content-type: text/html; charset=iso-8859-1' . "\r\n" . "From: $emfrm" . "\r\n");

Hey
I had a problem a while back with this, some emailing sites are now filtering out html from the emails. It would be worth checking it.

Its something like 1/20 so its probably nothing to do with it, but its worth a shot ;D

Hope you get it sorted

Well, I do get the email back in HTML format, but it doesn't send the form data, it sends whatever is between the <html> tags. For example:

<$php echo "$formdata[NAME]">;
"$formdata[NAME]";
$formresults

Is there a way I can get the form data to display between the tags?

Thanks everyone.

Well, I do get the email back in HTML format, but it doesn't send the form data, it sends whatever is between the <html> tags. For example:

<$php echo "$formdata[NAME]">;
"$formdata[NAME]";
$formresults

Is there a way I can get the form data to display between the tags?

Thanks everyone.

That's because you are using single quotes here

$message = '<html><head></head><body>$formresults</body></html>';

make those double quotes like this

$message = "<html><head></head><body>$formresults</body></html>";

or use single quotes like this

$message = '<html><head></head><body>' . $formresults . '</body></html>';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.