Location: PHPKode > scripts > E-mail sender with custom mandatory fields > e-mail-sender-with-custom-mandatory-fields/index.php
<?php
/* ***************************************************************

E-mail sender with custom mandatory fields and e-mail address validation W3C Compliant

PHP programming: Javier Valderrama

Developed for Jaxolotl Design - www.jaxolotl-design.com

Fully customizable web based e-mail system with mandatory fields checking without loosing fields content
Secure e-mail sending by destroying tags an sending e-mail in HTML format
Custom error messages
Custom mandatory fields
E-mail address validation 
W3C Compliant HTML code

*************************************************************** */
############################################### CREATE AN STANDARD TABLE TO CONTAIN ERROR MESSAGES
$alert_open ="<table width=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"0\" style=\"border:1px solid #CC0000;\"><tr><td bgcolor=\"#CC0000\"><strong style=\"color:#ffffff;\">ALERT ! </strong></td></tr><tr><td bgcolor=\"#FFFFFF\">";
$alert_close ="</td></tr></table><br>";

##########################################  VALIDATE MANDATARY FIELDS
function validate_required(){
	global $alert_open,$alert_close ;
	$allow = true;

	if(	// add or change this fields to make them MANDATORY
	(trim($_POST['name']))
	&& (trim($_POST['email']))

	){
		$allow = true;
	}
	else{
		// Error message when mandatory fields are empty

		echo $alert_open . "Fields marked with <strong>*</strong> are mandatory.<br>
		Please amend and retry.<br>".$alert_close;
		$allow = false;
	}

	if($allow==true){
		return true;
	}
	else{
		return false;
	}
}

###############################################  RETURNS DATA FOR TEXTAREA
function return_textarea_text($string,$allow_html){
	switch($allow_html):

	case'html':
	$patterns = array(
	'#<\s{0,}textarea#is', 
	'#</\s{0,}textarea\s{0,}>#is'
	);
	$substitutions = array('[textarea','[/textarea]');
	return preg_replace($patterns,$substitutions,double_escape(trim(stripslashes($string))));
	break;

	case'plain':
	$patterns = array ('"' , ">" , "<");
	$substitutions = array("&quot;" , "&gt;" , "&lt;");

	return double_escape(str_replace($patterns,$substitutions,trim(stripslashes($string))));
	break;

	endswitch;
}
###############################################  RETURNS DATA FOR INPUT TEXT FIELD
function return_input_text($string){
	$patterns = array ('"' , ">" , "<");
	$substitutions = array("&quot;" , "&gt;" , "&lt;");
	return double_escape(str_replace($patterns,$substitutions,strip_new_line(trim(stripslashes($string)))));
}
###############################################  PROCES PLAIN TEXT TO KILL HTML
function process_plain_text($string){//process_input_text
	$patterns = array ("'", '"' , ">" , "<","“","”","’");
	$substitutions = array("\'", "&quot;" , "&gt;" , "&lt;","&quot;","&quot;","\'");
	$output_string = str_replace($patterns,$substitutions,trim(stripslashes($string)));

	return $output_string;
}
###############################################
function strip_new_line($string){
	$string = ereg_replace("[\n\r\t]", "&nbsp;", $string);
	return $string;
}
############################################### DOUBLE ESCAPES
function double_escape($string){
	$string = str_replace("&","&amp;",$string);
	return $string;
}
###############################################
function translate_breaks($string){
	if(preg_match('#\<.*?\>#s',$string)){
		return $string;
	}
	else{
		return nl2br(trim($string));
	}
}
############################################### RETURNS AVAILABLE
function available_posted_rowed($posted,$rowed){
	if($posted){
		return $posted;
	}
	else{
		return $rowed;
	}
}
###############################################
function validate_email($email_string) {
	// check if e-mail is a valid string
	global $alert_open,$alert_close ;
	if($email_string){
		if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email_string)){
			return true;
		}
		else{
			// error message
			echo $alert_open . "<strong class=\"red\">\"".$email_string."\"</strong><br>This e-mail address is not a valid e-amil address.<br> Please amend and retry.".$alert_close."<br>";
			return false;
		}
	}
	else{
		return true;
	}
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
		<meta name="COPYRIGHT" content="Copyright (c) 2006 ADW Group - www.jaxolotl-design.com">
		<meta name="Author" content="Programming:Javier Valderrama">
		<title>E-mail FORM</title>
		<style type="text/css">
			<!--
			body {
			margin: 4px;
			padding: 4px;
			}
			body, table, tr, td {
			font-family: Verdana, Arial, Helvetica, sans-serif;
			font-size: 11px;
			}
			input {
			font-family: Verdana, Arial, Helvetica, sans-serif;
			font-size: 11px;
			}
			textarea {
			font-family: Verdana, Arial, Helvetica, sans-serif;
			font-size: 11px;
			}
			-->
		</style>
	</head>
	<body>
		<table cellpadding="0" cellspacing="0" border="0" width="400">
			<tr>
				<td>
					<?

					if(
					(array_key_exists("send_email",$_POST))
					&&(validate_required())
					&&(validate_email(trim($_POST['email'])))
					)
					{
						$personal_information = "
						<br><strong>Name:</strong> ".process_plain_text($_POST['name'])."
						<br><br><strong>Phone:</strong>  ".process_plain_text($_POST['phone'])."
						<br><br><strong>Fax:</strong>  ".process_plain_text($_POST['fax'])."
						<br><br><strong>Address:</strong>  ".process_plain_text($_POST['address'])."
						<br><br><strong>Comment:</strong><br>
						".translate_breaks(process_plain_text($_POST['comment']))."	";

						/// Your e-mail address here
						$email_to = "hide@address.com";

						/// The e-mail subject here
						$email_subject = "Subject";

						/// sender's e-mail
						$headers = "From: ".trim($_POST['email']);


						$semi_rand = md5(time());
						$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

						$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
						$email_message = "This is a multi-part message in MIME format.\n\n"
						. "--{$mime_boundary}\n"
						. "Content-Type:text/html; charset=\"iso-8859-1\"\n"
						. "Content-Transfer-Encoding: 8bit\n\n"

						/// E-mail message content
						."\n".$personal_information
						."<br><br>" . "\n\n";


						/// Send e-mail
						$sending_ok = @mail($email_to, $email_subject, $email_message, $headers);


						if($sending_ok){
							// If e-mail is sended successfully

							echo "<h2>Your e-mail has been send successfully with the following information:</h2>"
							. $personal_information
							. "<br><br>
							Thank you for contact us.
							<br>  <br>

							Do you want to send another e-mail?<br>
							<a href=\"".$_SERVER['PHP_SELF']."\"><strong>Yes</strong></a>

							&nbsp;&nbsp;-&nbsp;&nbsp;
							<a href=\"javascript:close(opener=0);window.open('','_parent','');window.close();\"><strong>No</strong></a>
							<br><br>";
						}
						else{
							// e-mail sending error message 
							echo $alert_open
							."<strong>ERROR in mail server</strong><br>We apologize, your e-mail has not been send.<br><br>Please try later.<br>
							<a href=\"".$_SERVER['PHP_SELF']."\"><strong>Back</strong></a>"
							.$alert_close;
						}
					}
					else{
						?>
						<form action="" name="contact_us" method="POST">
							<input type="hidden" name="send_email" value="send_email">
							<table cellpadding="2" cellspacing="0" border="0" width="400">
								<tr>
									<td width="90">
										Name *
									</td>
									<td>
										<input type="text" name="name" value="<?php echo return_input_text(available_posted_rowed($_POST['name'],"")); ?>" style="width:300px;border:1px solid #cccccc;">
									</td>
								</tr>
								<tr>
									<td>
										E-mail *
									</td>
									<td>
										<input type="text" name="email" value="<?php echo return_input_text(available_posted_rowed($_POST['email'],"")); ?>" style="width:300px;border:1px solid #cccccc;">
									</td>
								</tr>
								<tr>
									<td>
										Phone
									</td>
									<td>
										<input type="text" name="phone" value="<?php echo return_input_text(available_posted_rowed($_POST['phone'],"")); ?>" style="width:300px;border:1px solid #cccccc;">
									</td>
								</tr>
								<tr>
									<td>
										Fax
									</td>
									<td>
										<input type="text" name="fax" value="<?php echo return_input_text(available_posted_rowed($_POST['fax'],"")); ?>" style="width:300px;border:1px solid #cccccc;">
									</td>
								</tr>
								<tr>
									<td>
										Address
									</td>
									<td>
										<input type="text" name="address" value="<?php echo return_input_text(available_posted_rowed($_POST['address'],"")); ?>" style="width:300px;border:1px solid #cccccc;">
									</td>
								</tr>
								<tr>
									<td colspan="2">
										<br>
										Comment:
										<br>
										<textarea name="comment" cols="10" rows="6" style="width:99%;border:1px solid #cccccc;"><?php echo return_textarea_text(available_posted_rowed($_POST['comment'],""),"html"); ?></textarea>
									</td>
								</tr>
							</table>
							<input type="submit" value="Send e-mail">
							<input type="reset"  onclick="window.location.href='<?php echo $_SERVER['PHP_SELF']; ?>';">
						</form>
						<?php
					}
					?>
				</td>
			</tr>
		</table>
	</body>
</html>
Return current item: E-mail sender with custom mandatory fields