<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Emailexpiry extends CI_Controller {
	public function __construct(){
		parent::__construct();
		checkFront();
	}

	public function index()
	{
		
		$this->load->model('usermaster');
		
		require(APPPATH."phpmailer/PHPMailerAutoload.php");
		

				$mail = new PHPMailer();
	
				$mail->CharSet="utf-8";
				$mail->IsSMTP();                                      // set mailer to use SMTP
				$mail->Host = "mail.spslite.my";  // specify main and backup server
				$mail->SMTPAuth = true;     // turn on SMTP authentication
				$mail->Username = "fad@spslite.my";  // SMTP username
				$mail->Password = "salihin01%"; // SMTP password
			
				//$mail->SMTPDebug = true;
			
				$mail->From = "support@spslite.my";
				$mail->FromName = "Spslite";
				//$mail->AddAddress('hilmi@salihin.com.my', 'Fadhirul');
				$mail->AddAddress('norhijrah@salihin.com.my', 'Hijrah');
			
				$mail->WordWrap = 50;                                 // set word wrap to 50 characters
				$mail->IsHTML(true);                                  // set email format to HTML (true) or plain text (false)
			
				$mail->Subject = "List of expire user ".date("d-m-Y");
				
				$expired = "<table><tr><td></td><td>Email</td><td>Expire Date</td></tr><tr><td></td><td></td><td></td></tr>";
				
				$email = $this->usermaster->usermaster_all();
				
				$dateNextWeek = date("Y-m-d", strtotime("+7 days"));
				//var_dump($dateNextWeek);
				$count = 1;
				
				foreach($email as $row){
					if((date("Y-m-d", strtotime($row->nextExpiracy)) >= date("Y-m-d")) && (date("Y-m-d", strtotime($row->nextExpiracy)) <= date("Y-m-d", strtotime($dateNextWeek)))){
						$expired .= "<tr><td>".$count.".</td><td>".$row->email."</td><td>".date("d-m-Y",strtotime($row->nextExpiracy))."</td></tr>";
						$count++;
					}
					else{
						$expired .= "<tr><td></td><td></td><td></td></tr>";
					}
					
				}
				
				/*$fad = "";
				
				for($i=0;$i<4;$i++){
					$fad .= $expired[$i]."<br />";
				}*/
				
				$mail->Body = $expired;
				//$mail->AltBody = "This is the body in plain text for non-HTML mail clients";    
				//$mail->AddEmbeddedImage('images/logo.png', 'logo', 'logo.png');
				//$mail->addAttachment('files/file.xlsx');
				
				//var_dump($mail);
			
				if(!$mail->Send())
				{
				   echo "Message could not be sent. <p>";
				   echo "Mailer Error: " . $mail->ErrorInfo;
				   exit;
				}
			
				echo "Message has been sent";
		
		
		

	}


	

}

?>
