<?php
	include('/var/www/html/uclit/protected/common/login/check.php');
	include('/var/www/html/uclit/protected/common/css_login/css_head2.php');
	include('/var/www/html/uclit/protected/connections/db_work.php'); 
	mysql_select_db($database_workshop, $sqlConn) or die("Could not select database");
	
	if(isset($_POST['mode']) && $_POST['mode'] == 'email')
	{
		$namelist = $_POST['namelist']; // array variable from ins_workshop_detail.php
	
		//sorting
		//sort($namelist);
		//reset($namelist);
		
		 $filename=$_FILES["attachment"]["name"]; 
    $filetype=$_FILES["attachment"]["type"]; 
    $filesize=$_FILES["attachment"]["size"]; 
    $filetemp=$_FILES["attachment"]["tmp_name"]; 


       // echo"File type is $filetype  File Name is $filename and File Size is $filesize and File Temp is $filetemp";
    if($filetype=="application/octet-stream" or $filetype=="application/vnd.ms-powerpoint" or $filetype=="application/vnd.ms-excel" or $filetype=="application/pdf" or $filetype=="text/plain" or $filetype=="application/msword" or $filetype=="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" or $filetype=="application/vnd.openxmlformats-officedocument.wordprocessingml.document" or $filetype=="application/vnd.openxmlformats-officedocument.presentationml.presentation") 
    { 
    
      
    // MAIL SUBJECT 

    $subject = "Mail with file attachment"; 
	$body = $_POST['body'];
    
    // TO MAIL ADDRESS 
    
    
    $to="to@mail.com"; 

/* 
    // MAIL HEADERS 
                        
    $headers  = "MIME-Version: 1.0\n"; 
    $headers .= "Content-type: text/html; charset=iso-8859-1\n"; 
    $headers .= "From: Name <name@name.com>\n"; 

*/ 
  


    // MAIL HEADERS with attachment 

    $fp = fopen($filetemp, "rb"); 
    $file = fread($fp, $filesize); 

    $file = chunk_split(base64_encode($file)); 
    $num = md5(time()); 
    
        //Normal headers 

    $headers  = "From: UL Instruction<ulinstruction@mail.libraries.uc.edu>\r\n"; 
       $headers  .= "MIME-Version: 1.0\r\n"; 
       $headers  .= "Content-Type: multipart/mixed; "; 
       $headers  .= "boundary=".$num."\r\n"; 
       $headers  .= "--".$num."\r\n"; 

        // This two steps to help avoid spam    

    $headers .= "Message-ID: <".$now." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n"; 
    $headers .= "X-Mailer: PHP v".phpversion()."\r\n";          

        // With message 
        
    	$message = "Content-Type: text/html; charset=iso-8859-1\r\n"; 
     	$message .= "Content-Transfer-Encoding: 8bit\r\n"; 
       $message .= "".$body."\n"; 
       $message .= "--".$num."\n";  

        // Attachment headers 

    $headers  .= "Content-Type:".$filetype." "; 
       $headers  .= "name=\"".$filename."\"r\n"; 
       $headers  .= "Content-Transfer-Encoding: base64\r\n"; 
       $headers  .= "Content-Disposition: attachment; "; 
       $headers  .= "filename=\"".$filename."\"\r\n\n"; 
       $headers  .= "".$file."\r\n"; 
       $headers  .= "--".$num."--"; 

    
    
    // SEND MAIL 
        $subject = $_POST['subject'];
		$body = $_POST['body'];
		$count = 0;
		$mail_to = "";
		$mail_subj = $subject;
		$mail_header = "From: ulinstruction@mail.libraries.uc.edu\r\n";
		$mail_header .= "X-Mailer: server\r\n";
		$mail_header .= "Reply To: ulinstruction@mail.libraries.uc.edu\r\n";
		$sendmail_param = "ulinstruction@mail.libraries.uc.edu";
		$mail_text = $body;
		//echo"$mail_text";
		echo "Email to:<br />";
		for($count=0;$count<count($namelist);$count++)
		{
			
			$sqlQuerry = "SELECT reg_email FROM registrants WHERE reg_id='$namelist[$count]'";
			$QuerryResult = mysql_query($sqlQuerry) or die("SELECT query failed from registrants");
			while($row = @mysql_fetch_array($QuerryResult))
			{
				$mail_to = $row['reg_email'];	
				echo "$mail_to<br />";
				mail("$mail_to","$mail_subj","$message","$headers","-f$sendmail_param");
					
			}
			
		}
     
     fclose($fp); 

  echo "Emailed to $count registrant(s) successully<br />
			<a href='workshop_detail.php'>Back to Workshop Detail</a>";
} 

elseif($filetype=="")
	{
	$namelist = $_POST['namelist']; // array variable from ins_workshop_detail.php
	
		//sorting
		//sort($namelist);
		//reset($namelist);
		$subject = $_POST['subject'];
		$body = $_POST['body'];
		$URL = $_POST['url'];
		$survey = $_POST['survey'];
		
		$count = 0;
		$mail_to = "";
		$mail_subj = $subject;
		$mail_header = "From: ulinstruction@mail.libraries.uc.edu\r\n";
		$mail_header .= "X-Mailer: server\r\n";
		$mail_header .= "Reply To: ulinstruction@mail.libraries.uc.edu\r\n";
		$sendmail_param = "ulinstruction@mail.libraries.uc.edu";
		$mail_text = $body;

		echo "Email to:<br />";
		for($count=0;$count<count($namelist);$count++)
		{
			
			$sqlQuerry = "SELECT reg_email FROM registrants WHERE reg_id='$namelist[$count]'";
			$QuerryResult = mysql_query($sqlQuerry) or die("SELECT query failed from registrants");
			while($row = @mysql_fetch_array($QuerryResult))
			{
				$mail_to = $row['reg_email'];	
				echo "$mail_to<br />";
				mail("$mail_to","$mail_subj","$mail_text","$mail_header","-f$sendmail_param");
					
			}
			
		}
		echo "Emailed to $count registrant(s) successully<br />
			<a href='workshop_detail.php'>Back to Workshop Detail</a>";
	}

else 
    { 
        echo '<font style="font-family:Verdana, Arial; font-size:11px; color:#F3363F; font-weight:bold">Wrong file format. Mail was not sent.</font>'; 
      
    } 

		
		
		
		
		
		
	}
	elseif(isset($_POST['mode']) && $_POST['mode'] == 'feedback')
	{
		$namelist = $_POST['namelist']; // array variable from ins_workshop_detail.php
	
		//sorting
		//sort($namelist);
		//reset($namelist);
		$subject = $_POST['subject'];
		$body = $_POST['body'];
		$URL = $_POST['url'];
		$survey = $_POST['survey'];
		
		if ($survey == 'yes')
			{
			$body .= "\n\nPlease visit the following URL to complete a Survey for your workshop:\n\n  $URL";
			}
		$count = 0;
		$mail_to = "";
		$mail_subj = $subject;
		$mail_header = "From: ulinstruction@mail.libraries.uc.edu\r\n";
		$mail_header .= "X-Mailer: server\r\n";
		$mail_header .= "Reply To: ulinstruction@mail.libraries.uc.edu\r\n";
		$sendmail_param = "ulinstruction@mail.libraries.uc.edu";
		$mail_text = $body;

		echo "Email to:<br />";
		for($count=0;$count<count($namelist);$count++)
		{
			
			$sqlQuerry = "SELECT reg_email FROM registrants WHERE reg_id='$namelist[$count]'";
			$QuerryResult = mysql_query($sqlQuerry) or die("SELECT query failed from registrants");
			while($row = @mysql_fetch_array($QuerryResult))
			{
				$mail_to = $row['reg_email'];	
				echo "$mail_to<br />";
				mail("$mail_to","$mail_subj","$mail_text","$mail_header","-f$sendmail_param");
					
			}
			
		}
		echo "Emailed to $count registrant(s) successully<br />
			<a href='workshop_detail.php'>Back to Workshop Detail</a>";
	}
	
	
	else
	{
		echo "Invalid entry in this page.<br />Please return to <a href='workshop_detail.php'>Workshop Detail</a><br />";
	}
	include('/var/www/html/uclit/protected/common/css_login/css_foot2.php');

	?>
