Location: PHPKode > scripts > CosmicFormMail > cosmicformmail/email.php
<!--
##-----------------------------------------------------------------##
##                                                                 ##
##                                                                 ##
## © Copyright Stephen Heylings 2003. All rights reserved. No part ##
## of this or any of the attached documents shall be               ##
## reproduced/stored in any way whatsoever without written         ##
## permission from the Copyright holder.                           ##
##   The Copyright holder holds no responsibility for errors or    ##
## omissions. No liability is assumed in any way for damages       ##
## resulting from the use of this document/program.                ##
##                                                                 ##
## Have a nice day.                                                ##
##                                                                 ##
##                                                                 ##
##-----------------------------------------------------------------##
-->

<?

include('config.php');

global $emailfield;	
global $required;
global $sentmessage;
global $toemail;
global $fields;
global $subject;

function checkEmail()
{
	global $emailfield;	
	if (!eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$", $_POST[$emailfield])) 
	{
		return "Invalid email address entered.";
	}
	else
	{
		return "";
	}
}

function checkRequired()
{
	global $required;
	foreach ($required as $value) 
	{
		if($_POST[$value]=="")
		{
			if(!$rtn)
			{
				$rtn=$value;
			}
			else
			{
				$rtn="$rtn, $value";
				$s="s";
			}
		}
	}
	if($rtn) $rtn = "You must enter values for the following field$s: $rtn";
	return $rtn;
}

if($emailfield)
{
	$emailError = checkEmail();
}
if($required)
{
	$requiredError = checkRequired();
}

if($emailError)
{
	$sentmessage = $emailError;
}
if($requiredError)
{
	$error = $requiredError;
	if($error!="")
	{
		$sentmessage = $error;
	}
}
if($emailError || $requiredError)
{
	$sentmessage = $sentmessage."<br>Go <a href='javascript:history.back(1)'>back</a> to form.";
}

$msg = array($name, $email, $message);
	$name = 'name';
	$email = 'email';
	$message = 'message';
		 
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

$filename = "email.htm";
$handle = fopen ($filename, "r");
	$contents = fread ($handle, filesize ($filename));
fclose ($handle);

foreach ($fields as $field)
{
	$entry = $_POST[$field];
	if (!$entry) $entry = "No data submitted";
	$contents = str_replace ("::$field::",$entry,$contents);
}

mail($toemail, $subject, $contents, $headers);

$filename = "sent.htm";
$handle = fopen ($filename, "r");
	$contents = fread ($handle, filesize ($filename));
fclose ($handle);

$contents = str_replace ("::SentMessage::",$sentmessage,$contents);

echo $contents;
?> 
Return current item: CosmicFormMail