<?php

      include ("../../commun/connection_bdd.php");
        
        $filename = $_GET['filename'];
	
	$email_site = 'contact@studio-acts.fr';
	
        if ( isset($_POST['nom_groupe']) AND $_POST['nom_groupe'] != "tous_les_contacts"  ) {                                    //ENVOI A UN GROUPE
     
            $cathegorie = $_POST['nom_groupe'];
            
            $reponse = $bdd->query("SELECT * FROM `contact` WHERE cathegorie='{$cathegorie}'") or die(print_r($bdd->errorInfo()));
            
            while ($donnees = $reponse->fetch()) {
                
                if($donnees['abonnee'] === "oui"){
                    
			$to = $donnees['email'];
			$id = $donnees['id'];
		     
			$sujet = "Studio-ACTS";
		       
			$nom   = "Studio-ACTS";
			$email = "contact@studio-acts.fr";
			
			$envoyer = file_get_contents("http://www.studio-acts.fr/admin_12014/news/tmp/".$filename);
			$envoyer = str_replace("[LIEN_DESABONNE]", $id, $envoyer);
		       
			$headers  ='From:'.$nom.'<' . $email_site . '>' . "\n";
			$headers .='Reply-To:' .$email."\n";
			$headers .='Content-Type: text/html; charset=utf-8'."\n";
			$headers .='Content-Transfer-Encoding: 8bit';
			   
		        mail($to,$sujet,$envoyer,$headers);
                }
                
            }
        
            $reponse->closeCursor();
?>
	    <script>
		  //alert ("La Newsletter a bien \351t\351 envoy\351e au groupe <?php echo $cathegorie; ?>");
		  //document.location.href="../archive.php"
	    </script>
<?php
            
        } else {                                    										  //ENVOI A TOUS

            $reponse = $bdd->query("SELECT * FROM `contact`") or die(print_r($bdd->errorInfo()));
            
            while ($donnees = $reponse->fetch()) {
                
                if($donnees['abonnee'] === "oui"){
                    
			$to = $donnees['email'];
			$id = $donnees['id'];
		     
			$sujet = "Studio-ACTS";
		       
			$nom   = "Studio-ACTS";
			$email = "contact@studio-acts.fr";
			
			$envoyer = file_get_contents("http://www.studio-acts.fr/admin_12014/news/tmp/".$filename);
			$envoyer = str_replace("[LIEN_DESABONNE]", $id, $envoyer);
		       
			$headers  ='From:'.$nom.'<' . $email_site . '>' . "\n";
			$headers .='Reply-To:' .$email."\n";
			$headers .='Content-Type: text/html; charset=utf-8'."\n";
			$headers .='Content-Transfer-Encoding: 8bit';
			
		        mail($to,$sujet,$envoyer,$headers);
                }
                
            }
        
            $reponse->closeCursor();
?>
	    <script>
		  //alert ("La Newsletter a bien \351t\351 envoy\351e à tous les contacts");
		  //document.location.href="../../newsletter.php?categorie=voir_aciennes_newsletters&id=<?php echo $id_admin; ?>"
	    </script>
<?php
	    //header('Location: ../../newsletter.php?categorie=voir_aciennes_newsletters&id='.$id_admin);
            
        } 
    
?>
