<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once('./include/phpmailer/src/Exception.php');
require_once('./include/phpmailer/src/PHPMailer.php');
require_once('./include/phpmailer/src/SMTP.php');

// tuto phpMailer sur https://alexwebdevelop.com/phpmailer-tutorial/

$mail_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'mail_method'"));
if($mail_info['valeur']!='php') {
	$smtp_infos = explode(",",$mail_info['valeur']);
	$smtp_check = $smtp_infos[0];
	if(isset($smtp_infos[1])) { $smtp_hote = $smtp_infos[1]; }
	if(isset($smtp_infos[2])) { $smtp_port = $smtp_infos[2]; }
	if(isset($smtp_infos[3])) { $smtp_auth = $smtp_infos[3]; }
	if(isset($smtp_infos[4])) { $smtp_user = $smtp_infos[4]; }
	if(isset($smtp_infos[5])) { $smtp_pass = $smtp_infos[5]; }
	if(isset($smtp_infos[6])) { $smtp_mode = $smtp_infos[6]; }
}
else {
	$smtp_check = 'php';
}

/* Create a new PHPMailer object. */
$mail = new PHPMailer();

$mail->CharSet = 'UTF-8';
$mail->Encoding = 'base64';

if ($exp_mail!="") {
	/* Set the mail sender. */
	$mail->setFrom("$exp_mail", "$exp_login InfodocLog");
	$mail->addReplyTo("$exp_mail", "$exp_login InfodocLog");
}

foreach($mails as $adresse) {
	/* Add a recipient. */
	$mail->addAddress("$adresse");
}

/* Specific mail */
if(isset($mails_plus)) {
	$mail->addCC("$adresse_cc");
	$mail->addBCC("$adresse_bcc");
}

/* Set the subject. */
$mail->Subject = "$objet";

/* Set the mail message body. */
$mail->isHTML(TRUE);
$mail->Body = "$body";
// $mail->AltBody = 'There is a great disturbance in the Force.';

/* Set an attachment */
if(isset($mail_file_edtpdf)) { 
	$mail->addStringAttachment($mail_file_edtpdf, 'EDT_CDI_'.$_POST['week'].'.pdf', 'base64', 'application/pdf');
}
if(isset($mail_file_seqpdf)) { 
	$mail->addStringAttachment($mail_file_seqpdf, 'Sequence_'.$_POST['seq_id'].'.pdf', 'base64', 'application/pdf');
}
if(isset($mail_file_evalpdf)) { 
	$mail->addStringAttachment($mail_file_evalpdf, 'Evaluation_'.$_POST['concerned'].'.pdf', 'base64', 'application/pdf');
}

if($smtp_check=='smtp') {
	/* SMTP parameters. */
	// TESTER smtp.gmail.com 587 true tls
	   /* Tells PHPMailer to use SMTP. */
   	$mail->isSMTP();
   	/* SMTP server address. */
   	$mail->Host = $smtp_hote;
   	/* Set the SMTP port. */
   	$mail->Port = $smtp_port;
   
   	if($smtp_auth==1) {
	   	/* Use SMTP authentication. */
	   	$mail->SMTPAuth = TRUE;
	   	/* SMTP authentication username. */
	   	$mail->Username = $smtp_user;
	   	/* SMTP authentication password. */
	   	$mail->Password = $smtp_pass;
		/* Set the encryption system. */
	}
   	if(isset($smtp_mode)) { $mail->SMTPSecure = $smtp_mode; }
   	else { $mail->SMTPSecure = 'tls'; }
     
   	/* Disable some SSL checks. */
   	$mail->SMTPOptions = array(
      	'ssl' => array(
      	'verify_peer' => false,
      	'verify_peer_name' => false,
      	'allow_self_signed' => true
      	)
   	);
   	/* Enable SMTP debug output. */
   	$mail->SMTPDebug = 4;
}

/* Finally send the mail. */
if (!$mail->send()) {
   /* PHPMailer error. */
   echo $mail->ErrorInfo;
}
	
?>