Location: PHPKode > scripts > GIT-Formmailer > git-formmailer/index.php
<?php
#########################################################################
#		This script ist freeware and can be edited if the	 			#
#		copyright stays intact.								 			#
# 		It is an easy expandable and editable webmailer.  				#
#		Support: mail to support => git-development.com					#
#  		All rights by Global IT Development (Stefan Schwiedel)			#
#  		Version 2.0														#
#		Please send me an email if you find any bugs.				 	#
#		If you have wishes or criticisms post it into our forum: 		#	
#		http://www.git-development.com/global-it-community.htm			#
# 		Thank you!														#
#########################################################################

######################################################################################################
#	Begin of configuration 											   							 #
######################################################################################################

# Fill in the needed fields. You have to use the same syntax, 
# otherwise the script doesnt work. 
# eg. $fields = array("Name" => 1);
# The number behind the fieldname switches the check routine off (0) or on (1). 
# Attention! The content of the field with check routine "2" will be the answer address in the header 
# of the email. You can choose the name of the mailfield of your own.
# 0 for "NO" check				
# 1 for "the field must not empty" 
# 2 for "mailcheck"				
# 3 for " zip code"				
# 4 for "phonenumber" (only numbers are allowed 
#		 $phone_min  und $phone_max for maximal and minimal number ofallowed numbers)
# 5 for "website check" Please check this first.
# 6 The field allows only letters.
# 7 The field allowes only numbers.

class mailer{

	private $fields = array("Name" 			=> 1, 
							"First name" 	=> 1,
							"Address" 		=> 0,
							"email" 		=> 2,
							"Zip" 			=> 0,
							"Phone" 		=> 0,
							"Website" 		=> 0
							);
	
	# the size of input fields
	private $inputsize = 20;
			
	# switch this on (1) or off (0)
	private $send_ip = "0";						// will send you the IP Adress of the sender
	private $f5 = "0"; 							// allow or disallow to reload the page
												// switch this off "0" if you get many doubled mails
												//if you get problems, see our forum
	
	# configure the Zip Code check  (option 3)
	private $zip_len = 5;						// exact length of the common Zip Code 
	private $zip_min = 00000; 					// start of Zip Code area
	private $zip_max = 99999; 					// end of Zip Code area
	
	# configure the check routine of phonenumber (option 4)
	private $phone_max = 16;					// maximale länge der Telefonnummer
	private $phone_min = 3;						//mindestlänge der Telefonnummer
					
	# switch $textarea to 1 if you need a textarea
	# $textarea = off to switch off the textarea or on
	private $textarea = 'on'; 
	
	# configure the textarea
	private $length = 400;						// maximal number of characters in the textarea
	private $textlabel ="message";				// set the textlabel
	private $textheadline = "Your message"; 	// set the textheadline
	private $rows = 5;							// number of rows (height)
	private $cols = 40;							// number of colums (width)
	
	# fill in the receiver adress
	private $receiver = 'hide@address.com';
	private $reference = 'contactform';
	private $from = 'hide@address.com'; 		//if mailcheck is off this address is used as sender
	
	# If you get any errors, try to fill in your mail adress (of your domain).
	# z.B. hide@address.com
	private $add = "";
	
	# define browsertitle
	public $browsertitle = "Contactform";
	
	
	# define backgroundcolor
	public $background = "#ffffff";
	
	# define textcolor
	public $textcolor = "#000000";
	
	# define errorcolor
	public $errorcolor = "#FF0000";
	
	# define the backgroundcolor of the inputfields
	public $inputcolor = "#ffffff";
	
	# define the bordercolor
	public $bordercolor = "#000000";
	
	# define the borderstyle eg. none, dotted, dashed, solid, double, groove, ridge, inset, outset
	public $borderstyle = "dashed";
	
	# define borderwidth
	public $borderpx = "1";
	
	# define the formwidth
	public $borderwidth = "450";
	
	# headline
	private $headline = "<h1>Contactscript<br>Version 2.0</h1>";

	#######################################################################################
	# Begin of the script. Don´t change anything!                                         #
	#######################################################################################
	
	public $error = array();
	
	public $status;
	
	public function displayHead(){
		echo $this->headline;
		echo "<form class=\"formular\" action=\"".$_SERVER['PHP_SELF']."\" method=\"post\" target=\"_self\" accept-charset=\"ISO-8859-1\">\n";
		echo "<div class=\"border\">\n";
		
	}

	public function displayFields(){
		foreach ( $this->fields as $field => $check){
			printf ('<div class="margin">
						<div class="label">
							<label for="%s"> %s </label>
						</div>
						<div><input name="send[%s]" type="text" id="%s" size="%s" value="%s"></div>
					</div>', $field, $field, $field, $field, $this->inputsize, addslashes(htmlspecialchars($_POST['send'][$field])));
		}
	}

	public function displayTextarea(){
		if(strtolower($this->textarea) == 'on'){
			printf ('
			<div class="message"> <br>
					  <label for="%s">%s</label>
					  <br>
					  <textarea name="send[%s]" id="%s" rows="%s" cols="%s">%s</textarea>
			' ,$this->textlabel ,$this->textheadline ,$this->textlabel ,$this->textlabel,$this->rows ,$this->cols ,$_POST['send'][$this->textlabel]);
			echo '</div>';
		}
	}

	public function displayButton($value = 'send', $align = 'center'){
		echo '<div align="'.$align.'"><input type="submit" value="'.$value.'"></div><br>';
	}

	public function displayMessages(){
		echo '<div class="margin">';
		echo '<div class="error">';
		
		if(!empty($this->error) && is_array($this->error)){
			foreach($this->error as $key => $value ){
				echo $value.'<br>';
			}
		}
		
		if(isset($this->status) && !empty($this->status)){
			echo $this->status.'<br>';
		}
		
		if (isset($_GET['status']) && !isset($_POST['send']) && $this->f5=="0"){
			$status = preg_replace("/(_)/", " ", $_GET['status']);	
			echo '<br><h2>'.$status.'</h2>';	
		}
		
		echo "<br></div></div>";
	}

	public function displayFooter(){
		echo '</div></form><div '.
		'class="copyright" align=center>'.
		'Powered by <a '.
		'href="http://www.git-development.com" '. 
		'title="Global IT Development - solutions for business.">git-development.com'.
		'</a>';
		echo '</div>';
	}

	####################################################################################
	# just change the messages, dont change anything else if you dont know what to do  #
	####################################################################################
	public function checkMail(){

		$ip = $this->send_ip == "0" ? "" : getenv('REMOTE_ADDR');
		
		if (strtolower($this->textarea) == "on"){
			
			$messagel = strlen($_POST['send'][$this->textlabel]);
			if (get_magic_quotes_gpc() == "1"){ 
				$_POST['send'][$this->textlabel] = stripslashes($_POST['send'][$this->textlabel]);
			}
		}
		
		if (isset($messagel) && $messagel > $this->length){
			$this->error['message'] = "The maximum length of the message is ".$this->length." character.<br>The message has ".$messagel." character.";
		}
		
		if (isset($_POST['send']) && is_array($_POST['send'])){
			foreach ($this->fields as $var => $check)
			{
					switch ($check){
						case "0":
							
							if (get_magic_quotes_gpc() == "1"){
								$_POST['send'][$var] = stripslashes($_POST['send'][$var]);
							}
							
							$_POST['send'][$var] = htmlspecialchars($_POST['send'][$var]);
							break;
			
						case "1":
								#not empty
								if(empty($_POST['send'][$var])){
									$this->error[$var] = $var.' must not be empty!';
								}else{	
									$_POST['send'][$var] = htmlspecialchars($_POST['send'][$var]);
									
									if (get_magic_quotes_gpc() == "1"){
										$_POST['send'][$var] = stripslashes($_POST['send'][$var]);
									}
								}
							break;
							
						case "2":
								#check mail
								$search = '/^[a-zA-Z0-9\-\.\_]+\@[äöüÄÖÜa-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})$/i';
								if (!preg_match($search,$_POST['send'][$var])){
									$this->error['mail'] = "The E-Mail is invalid!";
								}
							break;
			
						case "3":
							#check zip code
							if (get_magic_quotes_gpc() == "1"){
								$_POST['send'][$var] = stripslashes($_POST['send'][$var]);
							}
								if ( !is_numeric($_POST['send'][$var])  || ( strlen(strval($_POST['send'][$var])) != $this->zip_len )){
									$this->error['zip'] = "The zip code is invalid!" ;
								}
								elseif ( ( $_POST['send'][$var] < $this->zip_min ) || ( $_POST['send'][$var] > $this->zip_max )){   
									$this->error['zip'] = "The zip code is out of the allowed range!" ;
								}
							break;
							
							case "4":
							#check phonenumber
							if (get_magic_quotes_gpc() == "1"){
								$_POST['send'][$var] = stripslashes($_POST['send'][$var]);
							}
								if ( !is_numeric($_POST['send'][$var])  || ( strlen(strval($_POST['send'][$var])) > $this->phone_max ) || ( strlen(strval($_POST['send'][$var])) < $this->phone_min )){
									$this->error['phone'] = "The phone number is invalid!" ;
								}
							break;
							
						case "5":
							#check website 
							if (get_magic_quotes_gpc() == "1"){
								$_POST['send'][$var] = stripslashes($_POST['send'][$var]);
							}
							
							$search = '/^((http|https|ftp)\:\/\/)?([äöüÄÖÜa-zA-Z0-9\-\.]+\.)?[äöüÄÖÜa-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})$/i';

							if (!preg_match($search,$_POST['send'][$var])){
								$this->error['web'] = "The domain is invalid!";
							}
						break;
						
						case "6":
							#check only letters
							if (get_magic_quotes_gpc() == "1"){
								$_POST['send'][$var] = stripslashes($_POST['send'][$var]);
							}
								$search = '/[A-Z]+$/i';
								if (!preg_match($search,$_POST['send'][$var])){
									$this->error[$var] = $var.' must contain only letters!';
								}
							break;
							
						case "7":
							#check nur Zahlen
							if (get_magic_quotes_gpc() == "1"){
								$_POST['send'][$var] = stripslashes($_POST['send'][$var]);
							}
							
							$search = '/[0-9]+$/i';	
								
							if (!preg_match($search,$_POST['send'][$var])){
								$this->error[$var] = $var.' must contain only numbers!';
							}
						break;
					}
			}
		}
		#############################################  END of check  #########################################
		
		######################################################################################################
		#	Please dont change anything if you dont know what u do.											 #
		#   The following code is to format the mail														 #
		######################################################################################################
		if (strtoupper(substr(PHP_OS,0,3)=='WIN')){
			$ret="\r\n";
		} 
		 elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')){
			$ret="\r";
		}
		 else{
			$ret="\n";
		}
		
		################################## format the headers #########################################
		foreach ($this->fields as $field => $value){
			if ($value == 2){
				$reply_val = $field;
			}
		}
		
		$reply = isset($_POST['send'][$reply_val]) ? $_POST['send'][$reply_val] : $this->from; 
		
		$header  = 'From: '.$reference.'<\''.$reply.'\'>'.$ret;												
		$header .= 'Reply-To: '.$reply.$ret;
		$header .= 'X-Mailer: PHP/ '.phpversion().$ret;
		$header .= 'MIME-Version: 1.0'.$ret;
		$header .= 'Content-type: text/plain; charset=ISO-8859-1; format=flowed'.$ret;
		$header .= 'Content-Transfer-Encoding: 8bit';
																																																 #
		#####################################################################################################
		#						Check variables																#
		#####################################################################################################
		
		if (isset($_POST['send']) && is_array($_POST['send']) && empty($this->error)){
			
			$content = 'You got a message from '.$reply.'.'.$ret;
			
			foreach ($_POST['send'] as $key => $value){
		   				$content .= $key.": ".htmlspecialchars($value).$ret;
					}
		
		######################################################################################################
		#   send the content																	 #
		######################################################################################################
					
				if(empty($this->add)){
					
					if (@mail($this->receiver, $this->reference, "The IP adress of the sender is: ".$ip.$ret.$content, $header)){
						$this->status = "<h2>The message was sent!</h2>";
						unset($_POST['send']); 
					}else{
						$this->status = "<h2>Error! The mail could not be sent!</h2>"; 
					}
				} else{	
						if (@mail($this->receiver, $this->reference, "The IP adress of the sender is: ".$ip.$ret.$content, $header, "-f $this->add")){
							$this->status = "<h2>The message was sent!</h2>";
							unset($_POST['send']); 
						}else{
							$this->status = "<h2>Error! The mail could not be sent!</h2>";
						}
					}
		}
		
		if ($this->f5 == "0" && stristr($this->status, "<h2>The message was sent!</h2>")){
			header("Location: $PHP_SELF?status=The_message_was_sent!");
		}
	}
}
?>
<?php $mailer = new mailer();
if(isset($_POST['send'])){
	$mailer->checkMail();
}
##################################################################################
# END of the script                                                              #
##################################################################################
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title><?php echo $mailer->browsertitle; ?></title>
<meta name="author" content="Stefan Schwiedel www.git-development.com">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<!--Begin of the stylesheet!-->
<style type="text/css"><!--

body {
	 font-family:Verdana,sans-serif;
	 font-size:12px;
	 color: <?php echo $mailer->textcolor; ?>;
	 background-color:<?php echo $mailer->background; ?>;
	 text-align:center;
	}

a:link, a:visited, a:active a:hover {
	text-decoration:none;
	font-weight:bold;
	color:#000000;
	font-size:10px;
}

h1 {
	font-size:15px;
	margin-top:30px;
}

h2 {
	font-size:12px;
}

.formular {
	margin:0px auto;
	width:<?php echo $mailer->borderwidth."px"; ?>;
	border:<?php echo $mailer->borderpx."px ".$mailer->borderstyle." ".$mailer->bordercolor; ?>;
	text-align:left;
}

.error {
	color:<?php echo $mailer->errorcolor; ?>;
	text-align:left;
}
.border {
	margin-top:10px;
}

.margin {
 margin-left:105px;
}

.label {
	float:left;
	text-align:left;
	width:9.5em;
	line-height:25px;
}

input{
	margin-top:5px;
	background-color:<?php echo $mailer->inputcolor;?>;
}

.message {
	clear:both;
	text-align:center;
}

.copyright {
	font-size:10px;
	color:#000000;
	position:relative;
	top:5px;
 }
//--></style>

<!--End of Stylesheets!-->

</head>
<body>

<?php 
######################################################################################################
#   Begin of the form																				 #
######################################################################################################
?>

<?php $mailer->displayHead(); ?>

<?php $mailer->displayFields(); ?>
<!-- a sample for a checkbox -->
<!-- the name of any additional fields must begin with e.g. name="send[test] -->

<div class="margin">
	<div class="label">
		<label for="checkbox"> checkbox </label>
	</div>
	<div><input type="checkbox" name="send[check]"></div>
</div>

<?php $mailer->displayTextarea();?>

<?php $mailer->displayButton('submit','center'); /* you can change the value and the alignment*/?>

<?php $mailer->displayMessages(); ?>

<?php $mailer->displayFooter(); ?>


</body>
</html>

Return current item: GIT-Formmailer