<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<?php
define("MYSQLSERVER","127.0.0.1");
define("MYSQLLOGIN","webbike");
define("MYSQLPASSWORD","honda");
define("BASE_DE_DONNEES","webbike");


header('Content-Type: text/html; charset=utf-8');
$today = date("Y-m-d");
if(isset($_REQUEST['actlist'])) $actlist=filter_var($_REQUEST['actlist'], FILTER_SANITIZE_STRING); else $actlist='email';
if(isset($_REQUEST['imgjoin'])) $imgjoin=filter_var($_REQUEST['imgjoin'], FILTER_SANITIZE_STRING); else $imgjoin='';
if(isset($_REQUEST['ModeTest'])) $ModeTest=filter_var($_REQUEST['ModeTest'], FILTER_SANITIZE_STRING); else $ModeTest='ModeTestOn';
if(isset($_REQUEST['promotext'])) $promotext=$_REQUEST['promotext']; else $promotext='';
if(isset($_REQUEST['txtSubjct'])) $txtSubjct=filter_var($_REQUEST['txtSubjct'], FILTER_SANITIZE_STRING); else $txtSubjct='Info Centre Honda Genève';

$promofile = fopen("promotext.txt", "w") or die("Unable to open file!");
fwrite($promofile, $promotext);
fclose($promofile);

switch ($actlist){
    case 'upload':
        include 'upload.php';
        break;
    case 'email':
    	if ($ModeTest == 'ModeTestOff'){
	   		require 'PHPMailer/PHPMailerAutoload.php';

			$mail = new PHPMailer;

			$mail->isSMTP();                       // Set mailer to use SMTP
			$mail->Host = 'smtp.hondageneve.com';  // Specify main and backup SMTP servers
			$mail->SMTPAuth = true;                // Enable SMTP authentication
			$mail->SMTPKeepAlive = true;			// le serveur ne racroche pas pour chaque email
			$mail->Username = 'boutique@hondageneve.com';   // SMTP username
			$mail->Password = 'pF37MS47C5';        // SMTP password
			$mail->SMTPSecure = 'tls';             // Enable TLS encryption, `ssl` also accepted
			$mail->Port = 587;                     // TCP port to connect to

			$mail->From = 'boutique@hondageneve.com';
			$mail->FromName = 'Boutique';

			$mail->addReplyTo('boutique@hondageneve.com', 'Boutique');
			$mail->isHTML(true);                                  // Set email format to HTML
			$mail->Subject = utf8_decode($txtSubjct);

			$myfile = fopen("promotext.txt", "r") or die("Unable to open file!");
			$promotext = fread($myfile,filesize("promotext.txt"));
			fclose($myfile);

	        $resultat = Client::getClientPromo() or die('Erreur SQL !');
			$numrows=count($resultat);
			print $numrows;
			$cnter='1';
	        for($i=0; $i < $numrows;$i++){ 
	        	if($cnter == '300')break;
	    		$ligne = $resultat[ $i ];
	    		$email = $ligne["Email"];
	    		$NoClient = $ligne['NoClient'];	        

				$mail->addAddress( $email );
				$message = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
							<html>
							<head>
							  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
							</head>
							<body>
							<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 11px;">

							  <p>'.utf8_decode($promotext).'</p>

							</div>
							</body>
							</html>';
				error_log($email);
				// Create Image From Existing File
			   /* $jpg_image = imagecreatefromjpeg($imgjoin);

			    $ext = pathinfo($imgjoin, PATHINFO_EXTENSION);
			    $filename = basename($imgjoin, ".".$ext);
			      
			    // Allocate A Color For The Text
			    $black = imagecolorallocate($jpg_image, 0, 0, 0);

			    $text = "N° de client: ".$NoClient;
			    // Set Path to Font File
			    $font = './OpenSans-Bold.ttf';

			    // Print Text On Image
			    imagettftext($jpg_image, 25, 0, 600, 250, $black, $font, $text );

			    // Send Image to Browser
			    imagejpeg($jpg_image,$filename);
			      
				$file = file_get_contents($filename);
				  
				// Send email
				$mail->addAttachment($filename,$filename.'.jpg');         // Add attachments
				*/
				$mail->Body = $message;

				if(!$mail->send()) {
				    echo 'Message could not be sent.';
				    echo 'Mailer Error: ' . $mail->ErrorInfo;
				} else {
				    echo 'Message has been sent';
				    $qury="INSERT INTO `promotions_suivis` (`P_NoClient`, `P_comment`,`P_status`,`P_date`) VALUES( '".$NoClient."', '".$email."','sent','".$today."')";
				    $resultats = Connection::executeRawQuery($qury) or die('Erreur SQL !<br>'.$qury.'<BR>');error_log($qury);
				}
				// Clear Memory
				//imagedestroy($jpg_image);
				$mail->clearAddresses();
		    	$mail->clearAttachments();
		    	$cnter++;
		    }
		}
		else {

			require 'PHPMailer/PHPMailerAutoload.php';

			$mail = new PHPMailer;

			//$mail->SMTPDebug = 3;                               // Enable verbose debug output

			$mail->isSMTP();                                      // Set mailer to use SMTP
			$mail->Host = 'smtp.hondageneve.com';  // Specify main and backup SMTP servers
			$mail->SMTPAuth = true;                               // Enable SMTP authentication
			$mail->SMTPKeepAlive = true; 
			$mail->Username = 'boutique@hondageneve.com';                 // SMTP username
			$mail->Password = 'pF37MS47C5';                           // SMTP password
			$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
			$mail->Port = 587;                                    // TCP port to connect to

			$mail->From = 'boutique@hondageneve.com';
			$mail->FromName = 'Boutique';

			$mail->addReplyTo('boutique@hondageneve.com', 'Boutique');
			$mail->isHTML(true);                                  // Set email format to HTML
			$mail->Subject = utf8_decode($txtSubjct);

			$mail->addAddress( 'f.grand@hondageneve.com' );     // Add a recipient
			$mail->addAddress( 'genuix@genuix.org' );
			$message = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
						<html>
						<head>
						  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
						</head>
						<body>
						<div style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 11px;">

						  <p>'.utf8_decode($promotext).'</p>

						</div>
						</body>
						</html>';
			print utf8_decode($message);
/*
			// Create Image From Existing File
		    $jpg_image = imagecreatefromjpeg($imgjoin);

		    $ext = pathinfo($imgjoin, PATHINFO_EXTENSION);
		    $filename = basename($imgjoin, ".".$ext);
		      
		    // Allocate A Color For The Text
		    $black = imagecolorallocate($jpg_image, 0, 0, 0);

		    $text = "N° de client: 1926";
		    // Set Path to Font File
		    $font = './OpenSans-Bold.ttf';

		    // Print Text On Image
		    imagettftext($jpg_image, 25, 0, 600, 250, $black, $font, $text );

		    // Send Image to Browser
		    imagejpeg($jpg_image,$filename);
		      
			$file = file_get_contents($filename);
			  
			// Send email
			$mail->addAttachment($filename,$filename.'.jpg');         // Add attachments
*/			
			$mail->Body = $message;
			
			if(!$mail->send()) {
			    echo 'Message could not be sent.';
			    echo 'Mailer Error: ' . $mail->ErrorInfo;
			} else {
			    echo 'Message has been sent';
			}
			print "<br><img src='".$filename."'><br>";
			// Clear Memory
			//imagedestroy($jpg_image);
			$mail->clearAddresses();
	    	$mail->clearAttachments();

	    	echo '<br><form action="action_promo.php" id="promoform">';
	    	echo '<input type="hidden" name="imgjoin" value="'.$imgjoin.'">';
	    	echo '<input type="hidden" name="promotext" value="'.htmlspecialchars($promotext).'">';
	    	echo '<input type="hidden" name="actlist" value="email">';
	    	echo '<input type="hidden" name="ModeTest" value="ModeTestOff">';
	    	echo "<input type='submit' name='Envoyer' value='sendall'>";
	    	echo '</form>';
			
		}
        break;
    default:
       echo "pas d'option choisie";
}
?>