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

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';	



### Togo Termine anlegen

function gototerminanlegung() {

	global $pdo;

	$statementtermin = $pdo->prepare("SELECT * FROM togo_terminvorgaben");	
	$statementtermin->execute();	
	foreach ($statementtermin as $row) {
		$togovorgabenid = $row["vorgabenid"];
		$togodate = $row["date"];
		$togostart = $row["start"];
		$togoende = $row["ende"];
		$standort = $row["standort"];
		$error = $row["error"];
		$warning = $row["warning"];
		$date = new DateTime();
		if($togodate == "Mo"){
			$date->modify('next monday');
		
		}elseif($togodate == "Di"){
			$$date->modify('next tuesday');
			#$nextday = date('Y-m-d', $nextTuesday);
		}elseif($togodate == "Mi"){
			$date->modify('next wednesday');
			#$nextday = date('Y-m-d', $nextTuesday);
		}elseif($togodate == "Do"){
			$date->modify('next thursday');
			#$nextday = date('Y-m-d', $nextTuesday);
		}elseif($togodate == "Fr"){
			$date->modify('next friday');
			#$nextday = date('Y-m-d', $nextTuesday);
		}elseif($togodate == "Sa"){
			$date->modify('next saturday');
			#$nextday = date('Y-m-d', $nextTuesday);
		}elseif($togodate == "so"){
			$date->modify('next sunday');
			#$nextday = date('Y-m-d', $nextTuesday);
		}else{
			$ausgabedate = "error";
		}
		$nextday = $date->format('Y-m-d');
		
		$i=0;
		while($i < 4){

			if($i!=0){
				#$nextday = date('Y-m-d', strtotime($nextday .' +7 day'));
				// Create a new DateTime object
				$currentDate = new DateTime($nextday);
				$datetempz = $currentDate->add(new DateInterval('P7D'));
				//Get yesterday date
				$nextday = $datetempz->format('Y-m-d');
				

			}
			$statementuser = $pdo->prepare("SELECT terminid FROM togo_termin WHERE  date=:date AND  start=:start AND  end=:end AND  standort=:standort");
			$statementuser->execute(array('date' => $nextday , 'start' => $togostart, 'end' => $togoende , 'standort' => $standort ));
			
			$count = $statementuser->rowCount();
			if($count == 0){
				$insert = $pdo->prepare("INSERT INTO togo_termin (date, start, end,standort,error,warning) VALUES (:date, :start, :end,:standort, :error, :warning)");
				$insert->execute(array('date' => $nextday , 'start' => $togostart, 'end' => $togoende , 'standort' => $standort, 'error' => $error , 'warning' => $warning ));	
				$userid = $pdo->lastInsertId();
			}		

			$i++;
		}


	
	}
	
	
}




function SendMailMessageSilent($con, $empfaenger, $betreff, $body){

	// LOGIN CONFIG AUSLESEN
	$queryconfig = mysqli_query($con, "Select * FROM config");
	$rowconfig = mysqli_fetch_assoc($queryconfig);
	
	if($queryconfig->num_rows == 1){
		$row = mysqli_fetch_assoc($queryconfig);
		$userid = $row["mailserver"];
		echo $userid;
	}
	$mailserver = $rowconfig["mailserver"];
	$mailUsername = $rowconfig["mailUsername"];
	$mailPassword = $rowconfig["mailPassword"];
	$mailPort = $rowconfig["mailPort"];
	$mailFrom = $rowconfig["mailFrom"];
	$mailFromName = $rowconfig["mailFromName"];
	$mailSMTPSecure = $rowconfig["mailSMTPSecure"];
	
	
	$body = iconv('UTF-8', 'CP1252//IGNORE', $body);
	$betreff = iconv('UTF-8', 'CP1252//IGNORE', $betreff);
	//$mail = new PHPMailer(true);   	


	$mail = new PHPMailer(true); 
	try {
		//Server settings
		$mail->SMTPDebug = 0;                                 // Enable verbose debug output
		$mail->isSMTP();                                      // Set mailer to use SMTP
		//$mail->Host = 'vwp0583.webpack.hosteurope.de';  // Specify main and backup SMTP servers
		$mail->Host = $mailserver;
		$mail->SMTPAuth = true;                               // Enable SMTP authentication
		//$mail->Username = 'wp1085322-creutzburg';                 // SMTP username
		//$mail->Password = 'praxis.cr';  					// SMTP password
		$mail->Username = $mailUsername;
		$mail->Password = $mailPassword;
		$mail->SMTPSecure = $mailSMTPSecure;                            // Enable TLS encryption, `ssl` also accepted
		//$mail->Port = 587;                                    // TCP port to connect to
		$mail->Port = $mailPort; 
		//Recipients
		$mail->setFrom($mailFrom , $mailFromName);
		#$mail->addAddress('ccreutzburg@live.de', 'Joe User');     // Add a recipient
		$mail->addAddress($empfaenger);               // Name is optional
		//$mail->addReplyTo('kontakt@praxis-creutzburg.de', 'Praxis Creutzburg');
		//$mail->addBCC('Arzt@praxis-creutzburg.de');
		//$mail->addBCC("praxis@balanceacademie.de"); 
		//Attachments
		#$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
		#$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

		//Content
		$mail->isHTML(true);                                  // Set email format to HTML
		$mail->Subject = $betreff;
		$mail->Body    = $body;
		$mail->AltBody = $body;
		//$mail->charSet = "UTF-8"; 
		//$mail->Encoding  = 'base64';
		
		$mail->send();
		
	} catch (Exception $e) {
		
	}

	

}

	
	


?>

