<HTML><BODY onload="document.MyForm.Password.focus()">


<!-- 
Solution of Assignment 3
-->

<?php

 echo <<< Begin
        <center>
        <H2>Sending Email</H2>
        </center>
        <p>
        <CENTER>
        <FORM ACTION="{$_SERVER['PHP_SELF']}" NAME="MyForm" METHOD="POST">
        <CENTER>
        <B>Password:</B> <input name="Password" type="password" size=30>
        <P>
        <TABLE BGCOLOR=lightgrey BORDER=1 BORDERCOLOR=gray>
        <TR>
        <TD>
        <B>To:</B></TD>
        <TD>
        <TABLE>
        <TR>
        <TD>
        <INPUT NAME="C1" TYPE="checkbox" VALUE="C1">List 1
        </TD>
        <TD>
        <INPUT NAME="C2" TYPE="checkbox" VALUE="C2">List 2
        </TD>

        </TR>
        </TABLE>
        </TD>
        </TR>

        <TR>
        <TD><B>Cc:</B></TD>
        <TD><INPUT NAME="Cc" SIZE=50></TD>
        </TR>
	
	 <TR>
        <TD><B>From:</B></TD>
        <TD><INPUT NAME="From" SIZE=50></TD>
        </TR>

        <TR>
        <TD><B>Your Email:</B></TD>
        <TD><INPUT NAME="Email" SIZE=50></TD>
        </TR>

        <TR>
        <TD><B>Subject:</B></TD>
        <TD><INPUT NAME="Subject" SIZE=50></TD>
        </TR>

        <TR>
        <TD><B>Message:</STRONG></TD>
        <TD>
        <TEXTAREA NAME="Message" ROWS=16 COLS=75 WRAP="virtual"></TEXTAREA>
        </TD>
        </TR>

        <input name='submitted' value='TRUE' type='hidden'>

        </TABLE>

        <TABLE BGCOLOR=lightgrey BORDER=1 BORDERCOLOR=gray>
        <TR>
        <TD><INPUT TYPE="submit" VALUE="SEND">
        </TD>
        <TD>
        <INPUT TYPE="reset" VALUE="RESET">
        </TD>
        </TR>
        </TABLE>

        </FORM>
Begin;

if (isset($_POST['submitted'])) // If the form is submitted
{
	$errors = array();
        $password = stripslashes(trim($_POST['Password']));
        if ($password == "fenton")
        {
		$from = stripslashes(trim($_POST['From']));
             	$email = trim($_POST['Email']);
		if (empty($email))
			$errors[] = "You have to enter your email address.";
		if (!isset($_POST['C1']) && (!isset($_POST['C2'])))
		{
			$errors[]= "You have to choose an email list.<BR>\n";
		}
		if (!empty($errors))
		{
			echo "<h1 align=center>ERROR</h1><center>\n";
			foreach ($errors as $e)
				echo $e, "<P>\n";
			echo "</center>\n";
		}
		else
		{
			if (isset($_POST['C1']))
			{
				@ $f = fopen('/home/abu-jeib/temp/C1.txt','rb');
				if (!$f)
				{
        				echo "ERROR: file can't be opened.<P></body></html>\n";
        				exit;
				}
		
				$s = trim(fgets($f));
				while (!feof($f))
				{ 
				    if (!empty($s))
				    {
					$cc = trim($_POST['Cc']);
                			$subject = stripslashes(trim($_POST['Subject']));
                			$body = stripslashes(trim($_POST['Message']));
                			$headers = "From: $from <$email>\r\n";
	            			$to = $s;
					//$headers .= "To: $to\r\n";
					if (!empty($cc))
	        	    			$headers .= "CC: $cc\r\n";
	        	    		mail($to,$subject,$body, $headers);
        	       			echo "<P align=center>Your email has been sent to $to<BR>\n";
				    }
				    $s = trim(fgets($f));
				}// while
				fclose($f);
			} // if
		
			if (isset($_POST['C2']))
			{
				@ $f = fopen('/home/abu-jeib/temp/C2.txt' /*file*/,'rb' /*file mode*/);
				if (!$f)
				{
        				echo "ERROR: file can't be opened.<P></body></html>\n";
        				exit;
				}
				$s = trim(fgets($f));
				while (!feof($f))
				{
				   if (!empty($s))
				   { 
					$cc = trim($_POST['Cc']);
                			$subject = stripslashes(trim($_POST['Subject']));
                			$body = stripslashes(trim($_POST['Message']));
                			$headers = "From: $from <$email>\r\n";
            				$to = $s;
					//$headers .= "To: $to\r\n";
					if (!empty($cc))
	            				$headers .= "CC: $cc\r\n";
	           			mail($to,$subject,$body, $headers);
					echo "<P align=center>Your email has been sent to $to<BR>\n";

				   }
					$s = trim(fgets($f));
				}// while
				fclose($f);
			} // if
		
        	}// else (empty errors)
	} // if (password not wrong)
	else 
		echo "<P align=center>Wrong Password<P>\n";
} // if (form submitted)

?>
</BODY></HTML>

