<?php
require_once 'log.php';
require_once 'fonctions.inc.php';


function courrielEnvoi( $db )
{
	$dicoExpe = getLang( $db->expeLang );
	$dicoDest = getLang( $db->destLang );
	$expediteurIndex = "Notification_denvoi_Expediteur";
	//print "envoi du courriel" . EOL;

	//	Message à l'expéditeur
	$indexObjet = "Objet_" . $expediteurIndex;
	$result =  replaceVariables($db, $dicoExpe[$indexObjet], $dicoExpe);
	log_write("indexObjet" .EOLH . print_r($result, true));
	$objetMail = $result['text'];
	$result = replaceVariables($db, $dicoExpe[$expediteurIndex], $dicoExpe) ;
	log_write("indexObjet" .EOLH . print_r($result, true));
	$mailText = $result['text'];
	$html = $result['html'];

	saveMail( $db, $db->expeMail, $objetMail, $mailText, $html);

	//log_write(print_r($db,true));

	// message au destinataire
	if ( $db->expeKnown == 'true' )
	{
		$destinataireIndex = "Notification_denvoi_Destinataire_ExpediteurConnu";
		//log_write("Expéditeur connu => ");
	}else
	{
		$destinataireIndex = "Notification_denvoi_Destinataire_ExpediteurAnonyme";
		//log_write("Expéditeur inconnu => ");
	}

	$indexObjet = "Objet_" . $destinataireIndex;
	$result = replaceVariables($db, $dicoDest[$indexObjet], $dicoDest);
	log_write("indexObjet" .EOLH . print_r($result, true));
	$objetMail = $result['text'];
	$result = replaceVariables($db, $dicoDest[$destinataireIndex], $dicoDest);
	log_write("indexObjet" .EOLH . print_r($result, true));
	$mailText = $result['text'];
	$html = $result['html'];
	if ( $html )
	{
		$message = "<html><head></head>\n<body>" . $message . "</body></html>";
	}
	saveMail( $db, $db->destMail, $objetMail, $mailText, $html);
}

function saveMail( $db, $destinataire, $objet, $text, $html=false)
{
	$query = "INSERT INTO courriels VALUES ('0', '" . $db->protect($destinataire) . "', '" . $db->protect($objet) .  "', '" . $db->protect($text) . "', '" . (int)$html .  "')";
	$db->query($query);
}

function envoiMail($destinataire, $sujet, $text, $html=false, $cc='', $bcc='')
{
	require_once 'include/swiftmailer/autoload.php';
	//require_once 'include/swiftmailer/swiftmailer/lib/swift_init.php';
	$transport = (new Swift_SmtpTransport('smtpauth.online.net', 465, 'ssl'))
		->setUsername('contact@avion-poe.me')
		->setPassword('AvionPoème*27juillet');
	$mailer  = new Swift_Mailer($transport);
	$message = (new Swift_Message($sujet))
		->setFrom(["contact@avion-poe.me"])
		->setTo([$destinataire])
		->setCharset('utf-8');
	$type = $message->getHeaders()->get('Content-Type');
	if ($html)
	{
		// setParameters() takes an associative array
		$type->setValue('text/html');
		$type->setParameter('charset', 'utf-8');
		$str = nl2br($text);
		$text = "<html><head></head>\n<body>" . $str . "</body></html>";
		log_write(__FILE__ . EOL . __LINE__ .  EOL . wordwrap($text, 1000, "\r\n"), INFO);
	}else
	{
		$type->setValue('text/plain');
		$type->setParameter('charset', 'utf-8');
		$text = str_replace("\n","\r\n", $text);
	}
	$message->setBody($text);
	//add date header
	$headers = $message->getHeaders();
	$headers->addDateHeader('Your-Header', new DateTimeImmutable('3 days ago'));
	if (!$mailer->send($message, $failures))
	{
		echo "Failures:";
		print_r($failures);
		log_write(__FILE__ . EOL . __LINE__ .  EOL . "Le courriel n'est pas parti:" . $destinataire . EOL . $sujet . EOL . print_r($failure, true) . EOL . wordwrap($text, 1000 , "\r\n"), ERROR);
		return false;
	}
	return true;
}
?>
