MOON
Server: Apache
System: Linux nserver.cafsindia.com 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: cafsindia (1002)
PHP: 8.2.30
Disabled: NONE
Upload Files
File: /home/cafsindia/rms.cafsjobs.com/application/controllers/Email_process.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Base_controller.php");
class Email_process  extends Base_controller{	
	public function __construct(){
		parent::__construct('email_process');
		if(!$this->Appconfig->isAppvalid()){
			redirect('config');
		}
		$this->collect_base_info();
	}	
	// LOAD PAGE WITH TABLE DATA
	public function index(){
		$data['table_headers']     = '';
		$role_info   = $this->db->query("CALL sp_a_run ('SELECT','SELECT * FROM `cw_category` where trans_status = 1')");
		$role_result = $role_info->result();
		$role_info->next_result();
		$category_list[""] = "---- Select Category ----";
		foreach($role_result as $for){
			$role_id   = $for->prime_category_id;
			$category_name = $for->category_name;
			$category_list[$role_id] = $category_name;
		}
		$data['category_list'] = $category_list;
		$config_info   = $this->db->query("CALL sp_a_run ('SELECT','SELECT * FROM `cw_mail_configurations` where trans_status = 1 and mail_status = 1')");
		$config_result = $config_info->result();
		$config_info->next_result();
		$configuration[""] = "---- Select Configuration ----";
		foreach($config_result as $config){
			$prime_mail_configurations_id   = $config->prime_mail_configurations_id;
			$configuration_name = $config->configuration_name;
			$configuration[$prime_mail_configurations_id] = $configuration_name;
		}
		$data['configuration'] = $configuration;

		$table_query = 'SELECT * FROM cw_print_info WHERE trans_status = 1';
		$table_info   = $this->db->query("CALL sp_a_run ('SELECT','$table_query')");
		$table_result = $table_info->result();
		$table_info->next_result();

		$table_list[""] = "---- Select Table ----";
		foreach($table_result as $table){
			$design_name        = strtolower(str_replace(" ","_",$table->print_info_name));
			$print_info_name = $table->print_info_name;
			$table_list[$design_name] = $print_info_name;
		}
		$data['pdf_template'] = $table_list;
		$this->load->view("$this->control_name/manage",$data);
	}	
	//Get All the Employees by Category
	public function get_employee_list(){
		$category   = implode(",",$this->input->post('category'));
		$emp_query  = 'SELECT * FROM cw_employees WHERE role in ('.$category.') and trans_status =1';
		$emp_info   = $this->db->query("CALL sp_a_run ('SELECT','$emp_query')");
		$emp_result = $emp_info->result();
		$emp_info->next_result();
		$emp_list   = "<option value=''>---- Select Employee ----</option>";
		foreach($emp_result as $result){
			$id        = $result->employee_code;
			$name      = ucwords($result->emp_name);
			$emp_code  = $result->employee_code;
			$emp_list .= "<option value='$id'>$emp_code - $name</option>";
		}
		echo $emp_list;
	}
	//Send Email to Employees
	public function send_email(){
		$email_for      = $this->input->post('email_for');
		$configuration  = $this->input->post('configuration');
		$category       = $this->input->post('category');
		$employees      = $this->input->post('employees');
		$email          = $this->input->post('email');
		$process_month  = $this->input->post('process_month');
		$subject        = $this->input->post('subject');
		//$title          = $this->input->post('title');
		$content        = $this->input->post('content');
		$password       = $this->input->post('password');
		$pdf_template   = $this->input->post('pdf_template');
		if((int)$email === 1){
			$email = "company_email_id";
		}else{
			$email = "personal_email_id";
		}
		require('./phpmailer/class.phpmailer.php');
		//Get Email Configuration data
		$config_query  = 'SELECT smtp_server,sender_name,bcc,port_no,sender_email,mail_username FROM cw_mail_configurations WHERE prime_mail_configurations_id ="'.$configuration.'" and trans_status = 1  and mail_status = 1';
		$config_info   = $this->db->query("CALL sp_a_run ('SELECT','$config_query')");
		$config_result = $config_info->result();
		$config_info->next_result();
		$smtp_server   = $config_result[0]->smtp_server;
		$sender_name   = $config_result[0]->sender_name;
		$bcc           = explode(",",$config_result[0]->bcc);
		$port_no       = $config_result[0]->port_no;
		$sender_email  = $config_result[0]->sender_email;
		$username      = $config_result[0]->mail_username;	
		$to_email      = "bsathishkumarmca16@gmail.com";	//Test Email
		$bcc_mail = "";
		foreach ($bcc as $bcc_key => $bcc_mails){
			$bcc_mail .= "\$mail->AddCC(".$bcc_mails.");<br/>";
		}
		//$success = $this->check_test_mail($smtp_server,$port_no,$username,$password,$sender_email,$sender_name,$to_email);
		if(true){
			$email_line_name = $this->unique_code($code="ECAMP");
			$insert_query = 'INSERT into cw_email_line (email_line_name,mail_configuration,email_for,process_month,pdf_template,trans_created_by,trans_created_date) values ("'.$email_line_name.'","'.$configuration.'","'.$email_for.'","'.$process_month.'","'.$pdf_template.'","'.$this->logged_id.'","'.date('Y-m-d H:i:s').'")';
			$insert_info        = $this->db->query("CALL sp_a_run ('INSERT','$insert_query')");
			$insert_result      = $insert_info->result();
			$insert_info->next_result();
			$insert_id = $insert_result[0]->ins_id;
			$email_query  = 'SELECT '.$email.',category_name,cw_transactions.employee_code FROM cw_employees join cw_transactions on cw_transactions.employee_code = cw_employees.employee_code inner join cw_category on cw_category.prime_category_id = cw_employees.role WHERE cw_employees.trans_status = 1 and cw_employees.role != 1 and cw_transactions.trans_status = 1 and cw_transactions.process_month = '.$process_month.' group by cw_transactions.employee_code';
			$email_info   = $this->db->query("CALL sp_a_run ('SELECT','$email_query')");
			$email_result = $email_info->result();
			$email_info->next_result();
			$email_result = array_map(function($emp){
				$emp_array['employee_data'] = $emp;
				$emp_array['employee_code'] = $emp->employee_code;
			    return $emp_array;
			}, $email_result);
			$email_result = array_column($email_result,'employee_data','employee_code');
			foreach ($employees as $key => $emp_code){
				$emp_email_data = $email_result[$emp_code];
				$to_email       = $emp_email_data->$email;
				$category       = strtolower(str_replace(" ", "_", $emp_email_data->category_name));
				if($to_email !== ""){
					$mail = new PHPMailer();				
					try{
						$mail->SMTPDebug = 3;
						$mail->IsSMTP();
						$mail->Host = $smtp_server; // Your SMTP PArameter
						$mail->Port = $port_no; // Your Outgoing Port
						$mail->SMTPAuth = true; // This Must Be True
						$mail->Username = $username; // Your Email Address
						$mail->Password = $password; // Your Password
						$mail->SMTPSecure = 'tls'; // Check Your Server's Connections for TLS or SSL
						$mail->From     = $sender_email;
						$mail->FromName = $sender_name;
						if($to_email){		
							$mail->AddAddress($to_email);
						}	
						if((int)count($bcc) > 0){
							foreach ($bcc as $bcc_key => $bcc_mails){
								$mail->AddCC($bcc_mails);
							}
						}
						$mail->IsHTML(true);
						$mail->Subject = $subject;
						$mail->Body    = $content;
						//Attachment
						if($email_for !== 'message'){ 
							$folder = "./".$pdf_template."/".$category."/".$process_month."_".$pdf_template."/".$emp_code.".pdf";
							//echo "BSK $folder"; die;
							if(file_exists($folder)){
							    $mail->addAttachment($folder); // Add attachments
							    $mail= $mail->Send();
							    if($mail){
							    	$status = 1;
							    	$msg = "Mail Sent Successfully";
									$tr_line .= "<tr><td>$emp_code</td><td style='color:green !important;'>$msg</td></tr>";
								}							
							}else{
								$status = 0;
								$msg = "File Not Exist";
								$tr_line .= "<tr><td>$emp_code</td><td style='color:green !important;'>$msg</td></tr>";
							}
						}else{
							$mail= $mail->Send();
							if($mail){
								$status = 1;
								$msg = "Mail Sent Successfully";
								$tr_line .= "<tr><td>$emp_code</td><td style='color:green !important;'>$msg</td></tr>";
							}
						}						
					}catch(phpmailerException $e){
						$status = 0;
						$msg = "Mail Not Sent";
						$tr_line .= "<tr><td>$emp_code</td><td style='color:#ff0303 !important;'>$msg</td></tr>";
					}catch(Exception $e){
						$status = 0;
						$msg = "Mail Not Sent";
						$tr_line .= "<tr><td>$emp_code</td><td style='color:#ff0303 !important;'>$msg</td></tr>";
					}
				}else{
					$status = 0;
					$msg = "EMail ID is Empty";
					$tr_line .= "<tr><td>$emp_code</td><td style='color:#ff0303 !important;'>$msg</td></tr>";
				} 	
				$insert_log_values .= '("'.$insert_id.'","'.$to_email.'","'.$emp_code.'","'.$status.'","'.$msg.'"),';
			}
			$insert_log_values = rtrim($insert_log_values,',');
			$email_log_query = 'INSERT into cw_email_log (email_line_id,email_id,employee_code,status,message) values '.$insert_log_values.'';
			$email_log_info        = $this->db->query("CALL sp_a_run ('INSERT','$email_log_query')");
			$email_log_result      = $email_log_info->result();
			$email_log_info->next_result();	
			$table_info = "<table class='table table-bordered'>
						<thead><tr><th>Employee Code</th><th>Result</th></tr></thead>
						<tbody>$tr_line</tbody></table>";
			echo json_encode(array('success'=>true,'message'=>"Mail Successfully Sent","table_data"=>$table_info));
		}else{
			echo json_encode(array('success'=>false,'message'=>"Please Enter the Valid Credentials..."));
		}	
	}
	public function check_test_mail($smtp_server,$port_no,$username,$password,$sender_email,$sender_name,$to_email){		
		$test_mail = new PHPMailer();
		try{
			$test_mail->SMTPDebug = 3; 
			$test_mail->IsSMTP();
			$test_mail->Host = $smtp_server; // Your SMTP PArameter
			$test_mail->Port = $port_no; // Your Outgoing Port
			$test_mail->SMTPAuth = true; // This Must Be True
			$test_mail->Username = $username; // Your Email Address
			$test_mail->Password = $password; // Your Password
			$test_mail->SMTPSecure = 'tls'; // Check Your Server's Connections for TLS or SSL
			$test_mail->From     = $sender_email;
			$test_mail->FromName = $sender_name;
			if($to_email){		
				$test_mail->AddAddress($to_email);
			}		
			$test_mail->IsHTML(true);
			$test_mail->Subject="Test Mail";
			$test_mail->Body="Test Content";			
			$test_mail= $test_mail->Send();
			if($test_mail){
				return true;
			}else{
				return false;
			}
		}catch(phpmailerException $e){
			return false;
		}catch(Exception $e){
			return false;
		}
	}
	// return unique code
	public function unique_code($code="UN"){
		$YearMap = array('2019'=>'A','2020'=>'B','2021'=>'C','2022'=>'D','2023'=>'E','2024'=>'F','2025'=>'G','2026'=>'H','2027'=>'I','2028'=>'J','2029'=>'K','2030'=>'L');
		$MonthMap = array('1'=>'J','2'=>'K','3'=>'L','4'=>'M','5'=>'N','6'=>'O','7'=>'P','8'=>'Q','9'=>'R','10'=>'S','11'=>'T','12'=>'U');
		$DayMap = array('1'=>'A','2'=>'B','3'=>'C','4'=>'D','5'=>'E','6'=>'F','7'=>'G','8'=>'H','9'=>'I','10'=>'J','11'=>'K','12'=>'L','13'=>'M','14'=>'N','15'=>'O','16'=>'P','17'=>'Q','18'=>'R','19'=>'S','20'=>'T','21'=>'U','22'=>'V','23'=>'W','24'=>'X','25'=>'Y','26'=>'Z','27'=>'1','28'=>'2','29'=>'3','30'=>'4','31'=>'5');
		$HourMap = array('0'=>'A','1'=>'B','2'=>'C','3'=>'D','4'=>'E','5'=>'F','6'=>'G','7'=>'H','8'=>'I','9'=>'J','10'=>'K','11'=>'L','12'=>'M','13'=>'N','14'=>'O','15'=>'P','16'=>'Q','17'=>'R','18'=>'S','19'=>'T','20'=>'U','21'=>'V','22'=>'W','23'=>'X');
		
		$loctime = time();
		$unq4 = substr($loctime,-4,4);
		$unq2 = mt_rand(10,99);
		$xunq2 = mt_rand(10,99);
		$year  = $YearMap[ltrim(date('Y'), '0')];
		$month = $MonthMap[ltrim(date('n'), '0')];
		$day   = $DayMap[ltrim(date('j'), '0')];
		
		$hr = ltrim(date('H'), '0');
		if($hr == "")
			$hr = "0";
		$hour  = $HourMap[$hr];

		$uniqueCode = $code. $year . $month . $day . $hour. $unq4 . $unq2 . $xunq2;
		return $uniqueCode;
	}
}
?>