<?php 
session_start();
$iduser=$_SESSION['user_idd'];
$date = new DateTime("now", new DateTimeZone('Africa/Nairobi') );
$timenow=$date->format('Y-m-d H:i:s');
$timenow2=$date->format('Y-m-d');
$month=$date->format('M');
//$finaldate='Today: '.$date->format('l d M Y - H:i:s');

include('../include/config.php');
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$query=mysqli_query($con,"select * from companydata");
while($row=mysqli_fetch_array($query))
{
$emma=htmlentities($row['cemail']);  
$pasw=htmlentities($row['cpassword']);
}
 
 
 if(isset($_POST["id"]))
 { 
 $id=addslashes($_POST["id"]);
 $sub=addslashes($_POST["sub"]);
 $mess=addslashes($_POST["mess"]);
 $rece=addslashes($_POST["rece"]);
 $sess=addslashes($_POST["sess"]);

$email='';
$sql = "SELECT * FROM Staffs where id='$id'";
$result = $con->query($sql);
while($row = $result->fetch_assoc()) 
{
 $email=$row['Email'];   
}

$sql=mysqli_query($con,"INSERT INTO emails(sid,Subject,Msg,Email,Dat,User,Sessionn) values ('$id','$sub','$mess','$email','$timenow2','$iduser','$sess')");
if($sql)
{
//$msgg='added tour amount';
//$sql3=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month)values('$iduser','$msgg','$timenow','$timenow2','update','$month')");
//if($sql3)
//{ } 
 $mail = new PHPMailer(true);
 $mail->IsSMTP();
 $mail->Host = 'smtp.gmail.com';
 $mail->SMTPAuth= true;
 $mail->Username =$emma;
 $mail->Password = $pasw;
 $mail->SMTPSecure = 'tls';
 $mail->Port = 587;
 
 $mail->setFrom($emma);
 $mail->addAddress($email);
 $mail->isHTML(true);
 $mail->Subject = $sub;
 $mail->Body = "<img src='https://bishopkairoeducationcentre.sc.ke/QB-Beta/images/logo.png' class='img-circle elevation-2' /> <br /> Greetings, <hr />".$mess." <br /> This message has been sent to you through school's <strong style='color:red'>Online Quickbook System </strong>. <hr /> For any enquiry kindly reply or send your email to: <strong> cdmbishopkairo@gmail.com </strong>";
// $mail->Body = "Following is  your new password on BKEC Accounting system. <br />"." Password: ".$code."<br /> Kindly make sure you have not shared your login credentials with anybody. <br />"."Click this link to visit the website: bishopkairoeducationcentre.sc.ke/QB-Beta/";
 //$mail->Body = "New Passsword ";
 $mail->send();
 
 $sql=mysqli_query($con,"UPDATE emails SET ESent='Yes',Dat='$timenow' where Email='$email' AND Sessionn='$sess'"); 
 //$msgg='added tour amount';
//$sql3=mysqli_query($con,"INSERT INTO activity(user,Action,Tim,Dat,shortmsg,Month)values('$iduser','$msgg','$timenow','$timenow2','update','$month')");
//if($sql3)
//{ } 

 echo 'success';
}else
{
echo "Error in sending some emails";
}
 }
?>