<?php
	//require 'mailgun-php/vendor/autoload.php';
	//use Mailgun\Mailgun;

	if (!isset($_POST['accountEmail']))
	{
		header("Location: ../index.php?oubli");
	}
	else
	{
		require 'connexion.php';

		$requete = 'SELECT * FROM isoactif_t_usager_ecole,t_liste_ecoles_08 WHERE u_e_id_fk = ID AND u_courriel = \'' . escapeValue($_POST['accountEmail']) . '\'';
		if(!$resultat = $connexion->query($requete)){ die('There was an error running the query : Retreive info');}
		$nbResults = $resultat->num_rows;

		if ($nbResults == 0)
			header("Location: ../index.php?oubli&error=emailNotFound");
		else
		{

$text = "";

$text .= "Bonjour,

Voici votre nom d'utilisateur et mot de passe pour le formulaire d'évaluation ISO-Actif en ligne.
";

// Possibly multiple accounts with same email
// Allowed and useful primarily for small schools
while ($ligne = $resultat->fetch_assoc())
{
$text .=  "
École : " . $ligne['NOM'] . "

Nom d'utilisateur : " . $ligne['u_login'] . "
Mot de passe : " . $ligne['u_pwd'] . "

Merci

L'équipe ISO-Actif";
}

			$to      = $_POST['accountEmail'];
			$subject = 'ISO-Actif';
			$message = $text;
			$headers = 'From: ne-pas-repondre@rseq.ca' . "\r\n" .
				'Reply-To: multimedia@rseq.ca' . "\r\n" .
				'X-Mailer: PHP/' . phpversion();

			mail($to, $subject, $message, $headers);

			/*
			# Instantiate the client.
			$mgClient = new Mailgun('key-1adc8627d4d1af2f7d5d54c8bedbe384');
			$email = $_POST['accountEmail'];
			$domain = 'mailgun.rseq.ca';
			$subject = 'ISO-Actif';
			$text = $text;

			# Make the call to the client.
			$result = $mgClient->sendMessage($domain, array(
			  'from'    => 'ne-pas-repondre@mailgun.rseq.ca',
			  'to'      => $email,
			  'subject' => $subject,
			  'text'    => $text
			));
			*/

			header("Location: ../index.php?accountMailSent");
		}

		$connexion->close();
	}
?>
