<?php

	use PHPMailer\PHPMailer\PHPMailer;
	use PHPMailer\PHPMailer\Exception;

	require '/var/www/html/v2/PHPMailer-master/src/Exception.php';
	require '/var/www/html/v2/PHPMailer-master/src/PHPMailer.php';
	require '/var/www/html/v2/PHPMailer-master/src/SMTP.php';

function send_mail($to_var, $subject_var, $message_var) {
	
	//--------------------------------------------------------------------------------------------------------
	// initialisations
	//--------------------------------------------------------------------------------------------------------
	$Flag_debug_A = true ;
	$Flag_debug_A = false ;

	$marqueur_temps_maintenant = time();
	$marqueur_temps_maintenant = mktime(0, 0, 0, date("m", $marqueur_temps_maintenant)  , date("d", $marqueur_temps_maintenant) + 1, date("Y", $marqueur_temps_maintenant) );

	//--------------------------------------------------------------------------------------------------------
	// connexion à la base de données
	//--------------------------------------------------------------------------------------------------------
	include ("/var/www/html/v2/_identifiants/sql_identification_sans_base.php");
	$mysql_base = 'AQCM05_main' ;

	$link_MAILS = mysqli_connect($mysql_host,$mysql_user,$mysql_password, $mysql_base);
	
	if (!$link_MAILS) {
		
		$link_AQCM05_OK = false ;
		
		if ($Flag_debug_A) {
			echo "Connexion MySQL : échec !!! (" . PHP_EOL; echo ")<br>";
		}

	}
	else {
		
		$link_AQCM05_OK = true ;
		
		if ($Flag_debug_A) {
			echo( "connexion MySQL : succès..." . "<br>" ) ;
		}
		
	}

	//------------------------------------------------------------------------------------------------------------
	// récupération des paramètres SMTP pour l'envoi du mail dans   AQCM_main.AQCM05_MAILS
	//------------------------------------------------------------------------------------------------------------

	if ( $link_AQCM05_OK ) {

		$query = "SELECT Host,Port,SMTPSecure,Username,Password,setFrom,addBCC,URL_AQCM05 FROM AQCM05_MAILS WHERE ( idx = 1 )";

		if ($Flag_debug_A) {
			echo $query . "<br>" ;
		}

		$result = mysqli_query($link_MAILS, $query); 
		
		if ( $result ) {
			
			if ($Flag_debug_A) {
				echo( "requête : succès..." . "<br>" ) ;
			}

			if (mysqli_num_rows($result) == 1) {	
				
				if ($Flag_debug_A) {
					echo( "Nb d'items : OK..." . "<br>" ) ;
				}
				
				$row = mysqli_fetch_array($result, MYSQLI_BOTH) ;
				$aqcm_Host 		=  $row["Host"] ;
				$aqcm_Port 		=  $row["Port"] ;
				$aqcm_SMTPSecure	=  $row["SMTPSecure"] ;
				$aqcm_Username 	=  $row["Username"] ;
				$aqcm_Password 	=  $row["Password"] ;
				$aqcm_setFrom 	=  $row["setFrom"] ;
				$aqcm_addBCC 		=  $row["addBCC"] ;
				$aqcm_URL_AQCM05	=  $row["URL_AQCM05"] ;

			}
			else {
				if ($Flag_debug_A) {
					echo( "Nb d'items : NOK..." . "<br>" ) ;
				}
			}

		}
		else {
			
			if ($Flag_debug_A) {
				echo( "requête : échec !" . "<br>" ) ;
			}
		
		}

	}
	
	//--------------------------------------------------------------------------------------------------------
	// clôture
	//--------------------------------------------------------------------------------------------------------
	if ( $link_AQCM05_OK ) {
		mysqli_close($link_MAILS);
	}

	//------------------------------------------------------------------------------------------------------------
	// envoi du mail proprement dit
	//------------------------------------------------------------------------------------------------------------
	$mail = new PHPMailer;

	//Server settings
	$mail->isSMTP();                                        //Send using SMTP
	$mail->SMTPDebug  = 0 ;
	$mail->Host       = $aqcm_Host ;                     //Set the SMTP server to send through
	$mail->Port       = $aqcm_Port ;                                //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
	$mail->SMTPSecure = $aqcm_SMTPSecure ;
	$mail->SMTPAuth   = true;                               //Enable SMTP authentication
	$mail->Username   = $aqcm_Username ;               //SMTP username
	$mail->Password   = $aqcm_Password ;                     //SMTP password
	$mail->CharSet    = "UTF-8" ;

	$mail->setFrom( $aqcm_setFrom, ' ' );
	$mail->addAddress( $to_var, ' ' );     					//Add a recipient
	$mail->addBCC( $aqcm_addBCC );

	$mail->Subject = $subject_var ;
	
	$new_message_var = str_replace("URL_AQCM05_CIBLE", $aqcm_URL_AQCM05, $message_var);
	$mail->msgHTML( $new_message_var ); 							//$mail->msgHTML(file_get_contents('contents.html'), __DIR__); 
														//Read an HTML message body from an external file, convert referenced images to embedded,
	$mail->AltBody = $new_message_var ;
															// $mail->addAttachment('images/phpmailer_mini.png'); //Attach an image file

	if ( ! $mail->send() ) {
		return [ "échec !!!", $to_var, false ] ;
	}
	else {
		return [ "succès...", $to_var, true  ] ;
	}

	//------------------------------------------------------------------------------------------------------------

}

function send_multiples_mails($to_var, $subject_var, $message_var) {
	
	//--------------------------------------------------------------------------------------------------------
	// initialisations
	//--------------------------------------------------------------------------------------------------------
	$Flag_debug_A = true ;
	$Flag_debug_A = false ;

	$marqueur_temps_maintenant = time();
	$marqueur_temps_maintenant = mktime(0, 0, 0, date("m", $marqueur_temps_maintenant)  , date("d", $marqueur_temps_maintenant) + 1, date("Y", $marqueur_temps_maintenant) );

	//--------------------------------------------------------------------------------------------------------
	// connexion à la base de données
	//--------------------------------------------------------------------------------------------------------
	include ("/var/www/html/v2/_identifiants/sql_identification_sans_base.php");
	$mysql_base = 'AQCM05_main' ;

	$link_MAILS = mysqli_connect($mysql_host,$mysql_user,$mysql_password, $mysql_base);
	
	if (!$link_MAILS) {
		
		$link_AQCM05_OK = false ;
		
		if ($Flag_debug_A) {
			echo "Connexion MySQL : échec !!! (" . PHP_EOL; echo ")<br>";
		}

	}
	else {
		
		$link_AQCM05_OK = true ;
		
		if ($Flag_debug_A) {
			echo( "connexion MySQL : succès..." . "<br>" ) ;
		}
		
	}

	//------------------------------------------------------------------------------------------------------------
	// récupération des paramètres SMTP pour l'envoi du mail dans   AQCM_main.AQCM05_MAILS
	//------------------------------------------------------------------------------------------------------------

	if ( $link_AQCM05_OK ) {

		$query = "SELECT Host,Port,SMTPSecure,Username,Password,setFrom,addBCC,URL_AQCM05 FROM AQCM05_MAILS WHERE ( idx = 1 )";

		if ($Flag_debug_A) {
			echo $query . "<br>" ;
		}

		$result = mysqli_query($link_MAILS, $query); 
		
		if ( $result ) {
			
			if ($Flag_debug_A) {
				echo( "requête : succès..." . "<br>" ) ;
			}

			if (mysqli_num_rows($result) == 1) {	
				
				if ($Flag_debug_A) {
					echo( "Nb d'items : OK..." . "<br>" ) ;
				}
				
				$row = mysqli_fetch_array($result, MYSQLI_BOTH) ;
				$aqcm_Host 		=  $row["Host"] ;
				$aqcm_Port 		=  $row["Port"] ;
				$aqcm_SMTPSecure	=  $row["SMTPSecure"] ;
				$aqcm_Username 	=  $row["Username"] ;
				$aqcm_Password 	=  $row["Password"] ;
				$aqcm_setFrom 	=  $row["setFrom"] ;
				$aqcm_addBCC 		=  $row["addBCC"] ;
				$aqcm_URL_AQCM05	=  $row["URL_AQCM05"] ;

			}
			else {
				if ($Flag_debug_A) {
					echo( "Nb d'items : NOK..." . "<br>" ) ;
				}
			}

		}
		else {
			
			if ($Flag_debug_A) {
				echo( "requête : échec !" . "<br>" ) ;
			}
		
		}

	}
	
	//--------------------------------------------------------------------------------------------------------
	// clôture
	//--------------------------------------------------------------------------------------------------------
	if ( $link_AQCM05_OK ) {
		mysqli_close($link_MAILS);
	}

	//------------------------------------------------------------------------------------------------------------
	// envoi du mail proprement dit
	//------------------------------------------------------------------------------------------------------------
	$mail = new PHPMailer;

	//Server settings
	$mail->isSMTP();                                        //Send using SMTP
	$mail->SMTPDebug  = 0 ;
	$mail->Host       = $aqcm_Host ;                     //Set the SMTP server to send through
	$mail->Port       = $aqcm_Port ;                                //TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
	$mail->SMTPSecure = $aqcm_SMTPSecure ;
	$mail->SMTPAuth   = true;                               //Enable SMTP authentication
	$mail->Username   = $aqcm_Username ;               //SMTP username
	$mail->Password   = $aqcm_Password ;                     //SMTP password
	$mail->CharSet    = "UTF-8" ;

	$mail->setFrom( $aqcm_setFrom, ' ' );

	for ( $i = 0; $i < sizeof($to_var); $i++ ) {
		
		$mail->addAddress( $to_var[$i], ' ' );     					//Add all recipients
		
	}

	$mail->addBCC( $aqcm_addBCC );

	$mail->Subject = $subject_var ;
	
	$new_message_var = str_replace("URL_AQCM05_CIBLE", $aqcm_URL_AQCM05, $message_var);
	$mail->msgHTML( $new_message_var ); 							//$mail->msgHTML(file_get_contents('contents.html'), __DIR__); 
														//Read an HTML message body from an external file, convert referenced images to embedded,
	$mail->AltBody = $new_message_var ;
															// $mail->addAttachment('images/phpmailer_mini.png'); //Attach an image file

	if ( ! $mail->send() ) {
		return [ "échec !!!", $to_var, false ] ;
	}
	else {
		return [ "succès...", $to_var, true  ] ;
	}

	//------------------------------------------------------------------------------------------------------------

}


?>