<head>
<title>eMail Form</title>
</head>
<body bgcolor=lightyellow>
<h1><center>eMail Using PHP</center></h1>

<?php

    include "email.php";

    if ($_POST)       			# if data was collected in $_POST array
    {
        validate();
    }
    display();

//=============================================================================
function validate()
{
        global $from, $to, $cc, $bcc, $replyTo, $date, $subject, $message, $format, $msg;
        global $from_error, $to_error, $subject_error, $message_error;

    	$from    = $_POST['from'];		#get HTML form entry fields
    	$to      = $_POST['to'];
    	$cc      = $_POST['cc'];
    	$bcc     = $_POST['bcc'];			
    	$replyTo = $_POST['replyTo'];		
    	$date    = $_POST['date'];
    	$format  = $_POST['format'];
    	$subject = $_POST['subject'];
    	$message = $_POST['message'];

    	$msg;						#create empty variables
    	$from_error;
    	$to_error;
    	$subject_error;
    	$message_error;

	if ($to == '') {
	    $msg      = 'error';
	    $to_error = '**';
	}
	if ($subject == '') {
	    $msg           = 'error';
	    $subject_error = '**';
	}
	if ($message == '') {
	    $$msg          = 'error';
	    $message_error = '**';
	}
	if (strtolower($format) != 'plain' &&  strtolower($format) != 'html') {
	    $format  = 'plain';
	}
	if ($msg == 'error')
	    $msg  = 'Please enter required field(s) above!';
	else
	{
	    $ok = email($from, $to, $cc, $bcc, $replyTo, $date, $subject, $message, $format);

	    if ($ok)
	        $msg  = "Email Sent <br/>";
	    else 
	        $msg  = "Email failed <br/>";
	}
}
    
//=============================================================================
function display()
{
	global $from, $to, $cc, $bcc, $replyTo, $date, $subject, $message, $format, $msg;
	global $from_error, $to_error, $subject_error, $message_error;

	if ($format == '') $format = 'plain';
	
 	print "<form method=POST action=$_SERVER[PHP_SELF]>";				//itself
	print "<table bgcolor=tan border=3 outset>";
	print "<tr>";
    print "<td><b>From: <font color=red> $from_error </font>";
    print "<td><input type=text name=from    value='$from'    size='40'>";
	print "<tr>";
    print "<td><b>To:  <font color=red> *$to_error </font>";
    print "<td><input type=text name=to      value='$to'      size='40'> (multiple recipients separated by commas)";
	print "<tr>";
    print "<td><b>CC:";
    print "<td><input type=text name=cc      value='$cc'      size='40'> (multiple recipients separated by commas)";
	print "<tr>";
    print "<td><b>BCC:";
    print "<td><input type=text name=bcc     value='$bcc'     size='40'> (multiple recipients separated by commas)";
	print "<tr>";
    print "<td><b>Reply To:";
    print "<td><input type=text name=replyTo value='$replyTo' size='40'> (multiple recipients separated by commas)";
	print "<tr>";
    print "<td><b>Date:";
    print "<td><input type=text name=date    value='$date'    size='40'> (format: Day, dd Mon yyyy hh:mm:ss -h:00)";
	print "<tr>";
    print "<td><b>Subject: <font color=red> *$subject_error </font>";
    print "<td><input type=text name=subject value='$subject' size='84'>";
	print "<tr>";
    print "<td><b>Message: <font color=red> *$message_error </font>";
    print "<td><textarea name=message rows=10 cols=65>$message</textarea>";
	print "<tr>";
	print "<td width=75>";
    print "<input type=submit value='      Send      '>";
    print "<td>   format:";
    print "    <input type=text name=format value='$format'   size='1'> (plain or html)";
	print "<tr>";
	print "</table>";

    print "<font color=red> $msg </font>";
    print "<br/>";
    print "</form>";
}

?>
<?php include "../../include.php"; ?>		<!-- hyperlink to see the code -->
</body>
</html>