<?php
error_reporting(0);
require_once('db_connect.php');

?>

<html>
<title>Automatic Email</title>
<body>

<?php
/* PHP EMAIL CODE */
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require_once __DIR__ . '/vendor/phpmailer/src/Exception.php';
require_once __DIR__ . '/vendor/phpmailer/src/PHPMailer.php';
require_once __DIR__ . '/vendor/phpmailer/src/SMTP.php';
//----------------------------------------------
// Send an e-mail. Returns true if successful 
//
//   $to - destination
//   $nameto - destination name
//   $subject - e-mail subject
//   $message - HTML e-mail body
//   altmess - text alternative for HTML.
//----------------------------------------------
function sendmail($from,$to,$nameto,$subject,$message,$altmess)  {
// passing true in constructor enables exceptions in PHPMailer
$mail = new PHPMailer(true);

try {
    // Server settings
    $mail->SMTPDebug = SMTP::DEBUG_SERVER; // for detailed debug output
    $mail->isSMTP();
    $mail->Host = 'smtp.gmail.com';
    $mail->SMTPAuth = true;
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port = 587;

    $mail->Username = 'wildnettask@gmail.com'; // YOUR gmail email
    $mail->Password = 'xnvhaglrmvfpgrmq'; // YOUR gmail password

    // Sender and recipient settings
    //$mail->setFrom('tyagianshu20@gmail.com', 'Anshu Tyagi');
	$mail->setFrom($from,$namefrom);
    //$mail->addAddress('anshu.tyagi@wildnettechnologies.com', 'Receiver Name');
	$mail->addAddress($to, $nameto);
    $mail->addReplyTo('tyagianshu20@gmail.com', 'Sender Name'); // to set the reply to

    // Setting the email content
    $mail->IsHTML(true);
    //$mail->Subject = "Send email using Gmail SMTP and PHPMailer";
	$mail->Subject  = $subject;
    $mail->Body = 'HTML message body. <b>Gmail</b> SMTP email body.';
	$mail->Body = $message;
    //$mail->AltBody = 'Plain text message body for non-HTML email client. Gmail SMTP email body.';
	$mail->AltBody  = $altmess;
	
    $mail->send();
    echo "Email message sent.";
} catch (Exception $e) {
    echo "Error in sending email. Mailer Error: {$mail->ErrorInfo}";
}
}



/* SQL quuery to get the user who have enterted the task */	
$this_day		= date('Y-m-d');
$threeDaysAgo	= date('Y-m-d', strtotime('-2 days')).'<br/>';
$twoDaysAgo		= date('Y-m-d', strtotime('-3 days')).'<br/>';
$sevenDaysAgo	= date('Y-m-d', strtotime('-6 days'));
$sqlCommand		= "SELECT t.user_ids,t.start_date, u.firstname,u.lastname,u.email,u.manager_id FROM task_list as t inner join users as u WHERE t.user_ids = u.id and t.start_date= CURDATE()-1" ;

$noTasks	= $conn->prepare($sqlCommand);
$noTasks->execute();
$result_tasks	= $noTasks->get_result();
 while ($row_user = $result_tasks->fetch_assoc()) {
	$user_id = $row_user['user_ids'];
	
	/* Getting the Users who have not enterted the task */
	
		$usersQuery = $conn->prepare("SELECT * FROM users WHERE id != $user_id");

		$usersQuery->execute();
		$allUsers = $usersQuery->get_result();

		while ($row_user_data = $allUsers->fetch_assoc()) {
			echo "<pre>";
				//echo $row_user_data['id'];
			echo "</pre>";		
			$userEmail = $row_user_data['email'];
			$userName = $row_user_data['firstname'] .' ' . $row_user_data['lastname'];
			$managerID = $row_user_data['manager_id'];
			if($managerID > 0):
			$managerData = $conn->prepare(("SELECT email FROM users WHERE id=$managerID"));
			$managerData->execute();
			$mgrResult = $managerData->get_result();
			while ($row_mgr = $mgrResult->fetch_assoc()) {
				$mgrEmail = $row_mgr['email'];
			}
			endif;
			// Email Data
			$to =  'anshu.tyagi@wildnettechnologies.com';
			$from = 'tyagianshu20@gmail.com';
			$nameto = $userName;
			$subject = 'Reminder 1';
			$message = 'Dear ' . $userName . ',' . "<br/>" .'
			Hope this message finds you well. Just a friendly reminder to submit your daily timesheet for '.$twoDaysAgo.' at your earliest convenience/ before the end of the day Your prompt attention to this matter is greatly appreciated.';

			$altmess ='Send email for task reminder';
			
			
			
			
			$start_date	=	date_create($twoDaysAgo);
			$today		=	date_create($this_day);
			$diff		=	date_diff($start_date,$today);
			$day_diff	=	$diff->format("%a");
			
			if($day_diff % 2 == 0){
			sendmail($from,$to,$nameto,$subject,$message,$altmess);
			} 
			elseif($day_diff % 3 == 0){
			sendmail($from,$to,$nameto,$subject,$message,$altmess);
		
			}
			elseif($day_diff % 6 == 0)
			{
			sendmail($from,$to,$nameto,$subject,$message,$altmess);
			}
			

		}	
}
?>