I have tried to get my php scrip to work but it doesn't. can someone maybe help me to get it working please. Here is my script:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>


<?php
if(isset($_POST['mail'])) {
		
	$email_to = "mymail@domain.com";
	$email_subject = "email subject line";
	
	
	function died($error) 
	{		
		echo "sorry, but there were error(s) found with the form you submitted. ";
		echo "These errors appear below.<br /><br />";
		echo $error."<br /><br />";
		echo "Please go back and fix these errors.<br /><br />";
		die();
	}
	
	
	if(!isset($_POST['Title']) ||
		!isset($_POST['Name']) ||
		!isset($_POST['Surname']) ||
		!isset($_POST['Company']) ||
		!isset($_POST['Email']) ||
		!isset($_POST['ConfirmEmail']) ||
		!isset($_POST['Tel']) ||
		!isset($_POST['Fax']) ||
		!isset($_POST['Comments'])) 
		{
		died('We are sorry, but there appears to be a problem with the form your submitted.');		
		}
	
	$Title = $_POST['Title']; // required
	$Name = $_POST['Name']; // required
	$Surname = $_POST['Surname']; // required
	$Company = $_POST['Company']; // not required
	$Email = $_POST['Email']; // required
	$ConfirmEmail = $_POST['ConfirmEmail']; // required
	$Tel = $_POST['Tel']; // required
	$Fax = $_POST['Fax']; // not required
	$Comments = $_POST['Comments']; // required
	
	$error_message = "error";
	$email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
    if(!eregi($email_exp,$email_from)) {
 	$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
	$string_exp = "^[a-z .'-]+$";
  if(!eregi($string_exp,$Title)) {
  	$error_message .= 'The Title you entered does not appear to be valid.<br />';
  }
  if(!eregi($string_exp,$Name)) {
  	$error_message .= 'The Name you entered does not appear to be valid.<br />';
  }  
  if(!eregi($string_exp,$Surname)) {
  	$error_message .= 'The Surname you entered does not appear to be valid.<br />';
  }
  if(!eregi($string_exp,$Company)) {
  	$error_message .= 'The Company you entered does not appear to be valid.<br />';
  }
  if(strlen(Ccomments) < 2) {
  	$error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  $string_exp = "^[0-9 .-]+$";
  if(!eregi($string_exp,$Tel)) {
  	$error_message .= 'The Telphone Number you entered does not appear to be valid.<br />';
  }
  $string_exp = "^[0-9 .-]+$";
  if(!eregi($string_exp,$Fax)) {
	$error_message .= 'The Telphone Number you entered does not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
	died($error_message);
  }
	$email_message = "Form details below.\n\n";
	
	function clean_string($string) {
	  $bad = array("content-type","bcc:","to:","cc:","href");
	  return str_replace($bad,"",$string);
	}
	
	$email_message .= "Title: ".clean_string($Title)."\n";
	$email_message .= "Name: ".clean_string($Name)."\n";
	$email_message .= "Surname: ".clean_string($Surname)."\n";
	$email_message .= "Company: ".clean_string($Company)."\n";
	$email_message .= "Email: ".clean_string($Email)."\n";
	$email_message .= "Confirm Email: ".clean_string($ConfirmEmail)."\n";
	$email_message .= "Tel: ".clean_string($Tel)."\n";
	$email_message .= "Fax: ".clean_string($Fax)."\n";
	$email_message .= "Comments: ".clean_string($Comments)."\n";
	
	

$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?
}
?>
 




<body>
</body>
</html>

Dani AI

Generated

The script posted by has a few clear syntax and logic bugs plus some dated validation that will stop it running reliably on modern PHP. Several small fixes will make it work and avoid common security problems.

Key problems and fixes (concise):

  • Short PHP tags and missing variables: the script reopens PHP with <? (short tag) instead of <?php and uses $email_from without ever setting it. Use <?php and assign a sanitized $email_from = $_POST['Email'] (see header-injection note below).
  • Broken names and logic: strlen(Ccomments) is missing the $ and $error_message is initialized to the literal "error" so the error-check always trips. Initialize $error_message = '' and use if (!empty($error_message)) or strlen($error_message) > 0.
  • Deprecated/incorrect validation: the code uses eregi (removed in PHP 7). Replace it with preg_match or better use filter_var for emails: if (!filter_var($Email, FILTER_VALIDATE_EMAIL)) ... (see filter_var manual and preg_match manual). Use proper regex delimiters and the i flag for case insensitivity.
  • Header injection and mail usage: strip CR/LF from header values (e.g. remove \r, \n, %0a, %0d) before building From/Reply-To. Avoid @mail (suppresses errors); check mail() return and log failures. See the mail() manual for details.
  • Testing steps: enable full error reporting during development (error_reporting(E_ALL); ini_set('display_errors',1);) and ensure the form submit name matches the isset($_POST['mail']) check or use $_SERVER['REQUEST_METHOD'] == 'POST'.

For maintainability and security, consider a well-maintained mail library (for example PHPMailer) to handle headers and SMTP reliably. ’s suggestion about starting a fresh thread is reasonable if significant rewrites are planned; ’s learning resources help for deeper PHP knowledge. For immediate fixes, correct the variable names, replace eregi, sanitize header inputs, and avoid error suppression.

Recommended Answers

All 2 Replies

you might want to start your own new thread cos this is an old one and you might not get as many responses

If you want to learn PHP you can Google PHP mail script or go to www.w3schools.com or buy David Powers book PHP solutions on amazon. It is a very good book and I would recommend it to anyone who fully understands HTML and CSS.

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.