<?php
session_start();
 error_reporting(E_ERROR);
  //*$ruta = '/opt/bcoexlib/lib/clases/comunes/phpmailer/';
  //*require_once('phpmailer/class.phpmailer.php'); 
  //*$mail = new PHPMailer();
  //*$mail ->SetLanguage("es", '/opt/bcoexlib/lib/clases/comunes/phpmailer/language/');
  //*$body = 'Prueba';
  //*$mail->SetFrom('name@yourdomain.com', 'First Last');
  //*$mail->AddReplyTo("name@yourdomain.com","First Last");
  //*$address = "sirjcbg1@hotmail.com";
  //*$mail->AddAddress($address, "Andres Garcia");
  //*$mail->Subject    = "PHPMailer Test Subject via mail(), basic";
  //*$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
  //*$mail->MsgHTML($body);
  //*if(!$mail->Send()) {
    //*echo "Mailer Error: " . $mail->ErrorInfo;
  //*} else {
    //*echo "Message sent!";
  //*}
  require_once('html/ClsGeneradorHtml.class.php'); 
  require_once('Connections/dbms.class.php');
  require_once('Connections/dbmy.class.php');
  require_once('clsObjetodeDatos.class.php');
  require_once('phpmailer/class.phpmailer.php');
  extract ($_POST);
  extract($_GET);
  $bd =new dbms('mysql','no'); 
  $clsObjetodeDatos = new clsObjetodeDatos($bd,$_POST,$_GET);    
  $ClsGeneradorHtml=new ClsGeneradorHtml("","");    

if (empty($boton)){ 
	echo '
	<link rel="stylesheet" href="css/estilos1.css" type="text/css">
	<script src="jquery.min.js" type="text/javascript"></script>
	<script src="js/email_controller.js" type="text/javascript"></script>
	<script type="text/javascript" src="tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
	<script type="text/javascript">
	tinyMCE.init({
			// General options
			mode : "textareas",
			language : "es",
			theme : "advanced",
			plugins : "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",

			// Theme options
			theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
			theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
			theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
			theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,

			// Skin options
			skin : "o2k7",
			skin_variant : "silver",

			// Example content CSS (should be your site CSS)
			content_css : "tinymce/examples/css/examples.css",

			// Drop lists for link/image/media/template dialogs
			template_external_list_url : "js/template_list.js",
			external_link_list_url : "js/link_list.js",
			external_image_list_url : "js/image_list.js",
			media_external_list_url : "js/media_list.js",

			// Replace values for the template plugin
			template_replace_values : {
					username : "Some User",
					staffid : "991234"
			}
	});
	</script>';

		$grupo=array("a"=>"Alumnos","d"=>"Docentes","p"=>"Administrativo");
		$forma= $ClsGeneradorHtml->Pagina_Top("Gestiona Horarios","","");
		echo $forma;
		echo $ClsGeneradorHtml->Pagina_Inicio_Body();
		echo $ClsGeneradorHtml->htmlTable("","","","100%","center","","bd");
		echo $ClsGeneradorHtml->htmlTd("","","","","hr");
		echo $ClsGeneradorHtml->htmlH("2","left");
		echo "Notificaciones";
		echo $ClsGeneradorHtml->htmlFinH("2");
		echo $ClsGeneradorHtml->htmlFinTd();
		echo $ClsGeneradorHtml->htmlFinTable();
		echo $ClsGeneradorHtml->htmlTable("5","0","0","100%","center","","","border:1px solid #ccc;");
		echo $ClsGeneradorHtml->htmlInicioFormulario("POST","frmCorreo");
		echo $ClsGeneradorHtml->htmlTr();
		echo $ClsGeneradorHtml->htmlTd("2%").$ClsGeneradorHtml->htmlEtiqueta('Asunto:').$ClsGeneradorHtml->htmlFinTd();
		echo $ClsGeneradorHtml->htmlTd("1%").$ClsGeneradorHtml->htmlCajaTexto('txtAsunto',$txtAsunto,'',100,false,100,'txtAsunto').$ClsGeneradorHtml->htmlFinTd();
		echo $ClsGeneradorHtml->htmlFinTr();
		echo $ClsGeneradorHtml->htmlTr();
		echo $ClsGeneradorHtml->htmlTd("20%").$ClsGeneradorHtml->htmlEtiqueta('Para:').$ClsGeneradorHtml->htmlFinTd();
		echo $ClsGeneradorHtml->htmlTd().$ClsGeneradorHtml->htmlSelectArray($grupo,"cmbgrupo","","","si").$ClsGeneradorHtml->htmlFinTd();
		echo $ClsGeneradorHtml->htmlFinTr();
		echo $ClsGeneradorHtml->htmlFinTable();
		echo $ClsGeneradorHtml->htmlTable("5","0","0","100%","center","","","border:1px solid #ccc;");
		echo $ClsGeneradorHtml->htmlTd("2%").'<textarea id="contenido" name="contenido" style="width:100%"></textarea>'.$ClsGeneradorHtml->htmlFinTd();
		echo $ClsGeneradorHtml->htmlFinTable();
		echo $ClsGeneradorHtml->htmlTable("5","0","0","100%","center","","","");
		echo $ClsGeneradorHtml->htmlTd("25%","left").$ClsGeneradorHtml->htmlBoton("Enviar", "cmdEnviar").$ClsGeneradorHtml->htmlFinTd();
		echo $ClsGeneradorHtml->htmlFinTable();		
		echo $ClsGeneradorHtml->htmlSaltolinea();
		echo $ClsGeneradorHtml->htmlSaltolinea();
		echo $ClsGeneradorHtml->htmlSaltolinea();
		echo $ClsGeneradorHtml->htmlCenter().$ClsGeneradorHtml->htmlDiv("MENSAJES","","","MENSAJES").$ClsGeneradorHtml->htmlFinCenter();
		echo $ClsGeneradorHtml->htmlFinDiv();
		
		echo $ClsGeneradorHtml->htmlFinFormulario();
		echo $ClsGeneradorHtml->Pagina_Fin_Body();
		echo $ClsGeneradorHtml->Pagina_Fin_Html();
}else{
	$mail = new PHPMailer();
    $mail ->SetLanguage("es", 'phpmailer/language/');
    if(empty($content)){
	   echo "<TABLE align=center width=100%  BORDER=0 CELLSPACING=0 CELLPADDING=0><TD COLSPAN=2 ALIGN='CENTER'><INPUT TYPE='image' src='imagenes/informacion.gif'><FONT class=font ><STRONG> Registre el cuerpo del Mensaje</STRONG></FONT></TD></TABLE>";	
	   return;	
	}
	if(empty($txtAsunto)){
	   echo "<TABLE align=center width=100%  BORDER=0 CELLSPACING=0 CELLPADDING=0><TD COLSPAN=2 ALIGN='CENTER'><INPUT TYPE='image' src='imagenes/informacion.gif'><FONT class=font ><STRONG> Registre el Asunto</STRONG></FONT></TD></TABLE>";	
	   return;	
	}
	if(empty($cmbgrupo)){
	   echo "<TABLE align=center width=100%  BORDER=0 CELLSPACING=0 CELLPADDING=0><TD COLSPAN=2 ALIGN='CENTER'><INPUT TYPE='image' src='imagenes/informacion.gif'><FONT class=font ><STRONG> Registre el Destinatario</STRONG></FONT></TD></TABLE>";	
	   return;	
	}
	$grupo = explode("&",$cmbgrupo);
	for ($i=0;$i<count($grupo);$i++){
		$para= substr( $grupo[$i] , (strpos($grupo[$i],"=",1)+1) , 1 );
		if ($para=="a"){
		   $sql="select nombres,apellidos,sexo,email from alumnos where email is not null";  	
		   $bd->EjecutarQuery($sql);	    
		   $datos=$bd->DatosArray();
		   foreach($datos as $valor){
			  //echo $valor["nombres"]."<BR>";   
			  $body = 'Prueba';
			  $mail->SetFrom('controlestudio@enahp.net', 'Escuela Nacional de Administraci&oacute;n y Hacienda P&uacute;blica');
			  //$mail->AddReplyTo("name@yourdomain.com","First Last");
			  // $address = $valor["email"];
			  $mail->AddAddress($valor["email"], $valor["nombres"]);
			  $mail->Subject    = $txtAsunto;
			  //*$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
			  $mail->MsgHTML($content);
				if(!$mail->Send()) {
					$cantidadcorreonoenviado++;
				} else {
					$cantidadcorreoenviado++;

				}
 
		   }
		}elseif ($para=="d"){
		   $sql="select nombres,apellidos,sexo,email from docentes where email is not null";  	
		   $bd->EjecutarQuery($sql);	    
		   $datos=$bd->DatosArray();
		   foreach($datos as $valor){
			  //echo $valor["nombres"]."<BR>";   
			  $mail->SetFrom('controlestudio@enahp.net', 'Escuela Nacional de Administraci&oacute;n y Hacienda P&uacute;blica');
			  //$mail->AddReplyTo("name@yourdomain.com","First Last");
			  // $address = $valor["email"];
			  $mail->AddAddress($valor["email"], $valor["nombres"]);
			  $mail->Subject    = $txtAsunto;
			  //*$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
			  $mail->MsgHTML($content);
				if(!$mail->Send()) {
					$cantidadcorreonoenviado++;
				} else {
					$cantidadcorreoenviado++;
				}
 
		   }
		}elseif ($para=="p"){
			
		   $sql="select nombres,apellidos,sexo,email from prueba_email where email is not null";  	
		   $bd->EjecutarQuery($sql);	    
		   $datos=$bd->DatosArray();
		   foreach($datos as $valor){
			  //echo $valor["nombres"]."<BR>";   
			  $mail->SetFrom('controlestudio@enahp.net', 'Escuela Nacional de Administraci&oacute;n y Hacienda P&uacute;blica');
			  //$mail->AddReplyTo("name@yourdomain.com","First Last");
			  // $address = $valor["email"];
			  $mail->AddAddress($valor["email"], $valor["nombres"]);
			  $mail->Subject    = $txtAsunto;
			  //*$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
			  $mail->MsgHTML($content);
				if(!$mail->Send()) {

					$cantidadcorreonoenviado++;
				} else {
					$cantidadcorreoenviado++;
				}
 
		   }
		}   

	 }
		echo "<TABLE align=center width=100%  BORDER=0 CELLSPACING=0 CELLPADDING=0>
				<TR>";
					if(!empty($cantidadcorreoenviado)){
					 echo  "<TD COLSPAN=2 ALIGN='CENTER'><INPUT TYPE='image' src='imagenes/emailenviado.jpg'><FONT class=font color=#04B404><STRONG> Correos Enviados".$cantidadcorreoenviado."</STRONG></FONT></TD>";
					}	
		echo	"</TR>
				<TR>";
				   if(!empty($cantidadcorreonoenviado)){
					 echo " <TD COLSPAN=2 ALIGN='CENTER'><INPUT TYPE='image' src='imagenes/email_rechazado.gif'><FONT class=font color=#04B404><STRONG> Correos no Enviados".$cantidadcorreonoenviado."</STRONG></FONT></TD>";
				   }
		echo	"</TR>
			  </TABLE>";	


	

		
}

?>
