File: //home/cafsindia/groups_cafsindia_com/application/controllers/Email_process.php
<?php if ( ! defined('BASEPATH')) exit('No direct script is allowed');
require_once("Action_controller.php");
class Email_process extends Action_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 and prime_category_id !=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;
//template design from employee module only
$table_query = 'select * from cw_print_info where trans_status = 1 and print_info_module_id="employees"';
$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'));
$email_for = $this->input->post('email_for');
$process_month = $this->input->post('process_month');
if($email_for === "message"){
$emp_query = 'select employee_code,emp_name from cw_employees where role in ('.$category.') and trans_status =1';
}else{
$emp_query = 'select employee_code,emp_name from cw_transactions where role in ('.$category.') and trans_status =1 and transactions_month ="'.$process_month.'"';
}
$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 for only employee modules data...
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');
$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,mail_password 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;
$mail_password = $config_result[0]->mail_password;
if($mail_password === $password){
$mail_staus = True;
}else{
$mail_staus = False;
}
$bcc_mail = "";
foreach ($bcc as $bcc_key => $bcc_mails){
$bcc_mail .= "\$mail->AddCC(".$bcc_mails.");<br/>";
}
//checking folder name
$pdf_template_name = ucwords(str_replace("_"," ",$pdf_template));
$print_type_query = 'select * from cw_print_info where print_info_module_id ="employees" and trans_status = 1 and print_info_name ="'.$pdf_template_name.'"';
$print_type_info = $this->db->query("CALL sp_a_run ('SELECT','$print_type_query')");
$print_type_result = $print_type_info->result();
$print_type_info->next_result();
$print_type = $print_type_result[0]->print_type;
//print_type based pdf path is collected based on path type
$print_type_list = array(1=>"Payslip",2=>"Form M",3=>"Offer Letter",4=>"Terminated without salary",5=>"Terminated with salary",6=>"Abscond",7=>"Resigned",8=>"FandF");
$emp_sts_qry ="";
if((int)$print_type === 1){//changed folder name as print type
$folder_start_name = "payslip";//payslip static folder
$folder_path = "./".$folder_start_name."/".$category."/".$process_month."_".$pdf_template."/".$emp_code.".pdf";
$emp_sts_qry = " and cw_transactions.employee_status = 0 ";
}else
if((int)$print_type === 2){//form_m static folder
$folder_start_name = "form_m";
$folder_path = "./".$folder_start_name."/".$process_month."/".$category.".pdf";
$emp_sts_qry = " and cw_transactions.employee_status = 0 ";
}else
if((int)$print_type === 3){//appointment letter in static employees folder
$folder_start_name = "employees";
$folder_path = "./".$folder_start_name."/".$folder_start_name."_".$category."/".$emp_code.".pdf";
$emp_sts_qry = " and cw_transactions.employee_status = 0 ";
}else
if((int)$print_type === 8){//fandf changed folder name as print type
$folder_start_name = "payslip";//fandf slip in static payslip folder
$folder_path = "./".$folder_start_name."/".$category."/".$process_month."_".$pdf_template."/".$emp_code.".pdf";
$emp_sts_qry = " and cw_transactions.employee_status = 1 ";
}else{//resigned,terminated with or without salary,abscond folder in static time_office_mail
$folder_start_name = "time_office_mail";
$folder_path = "./".$folder_start_name."/".$pdf_template."/".$emp_code.".pdf";
$emp_sts_qry = " and cw_transactions.employee_status = 1 ";
}
if($mail_staus){
$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,cw_transactions.emp_name 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.'"'.$emp_sts_qry;
$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;
$emp_name = $emp_email_data->emp_name;
$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 = "./payslip/".$category."/".$process_month."_".$pdf_template."/".$emp_code.".pdf";
$folder = $folder_path;
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>$emp_name</td><td style='color:green !important;'>$msg</td></tr>";
}
}else{
$status = 0;
$msg = "File Not Exist";
$tr_line .= "<tr><td>$emp_code</td><td>$emp_name</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>$emp_name</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>$emp_name</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>$emp_name</td><td style='color:#ff0303 !important;'>$msg</td></tr>";
}
}else{
$status = 0;
$msg = "Email ID is empty, so mail is not send";
$tr_line .= "<tr><td>$emp_code</td><td>$emp_name</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>Employee Name</th>
<th>Mail Status</th>
</tr>
</thead>
<tbody>$tr_line</tbody>
</table>";
echo json_encode(array('success'=>true,'message'=>$msg,"table_data"=>$table_info,"email_sts"=>$status));
}else{
echo json_encode(array('success'=>false,'message'=>"Please Enter the Valid Credentials..."));
}
}
// 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;
}
//collecting pdf template
public function get_pdf_template(){
$email_for = $this->input->post('email_for');
$print_type_qry = "";
if($email_for === "payslip"){
$print_type_qry = " and print_type in (1,8)";
}else
if($email_for === "resign"){
$print_type_qry = " and print_type in (4,5,6,7)";
}else
if($email_for === "offer"){
$print_type_qry = " and print_type in (3)";
}
//email type
$email_for = array(''=> '--- Select Email For ---','payslip'=> 'Payslip','resign'=> 'Resign','offer'=> 'Offer','message'=> 'Only Message');
//print_type based pdf path is collected based on path type
$print_type_list = array(1=>"Payslip",2=>"Form M",3=>"Offer Letter",4=>"Terminated without salary",5=>"Terminated with salary",6=>"Abscond",7=>"Resigned",8=>"FandF");
if($email_for){
$print_type_query = 'select * from cw_print_info where print_info_module_id ="employees" and trans_status = 1 '.$print_type_qry;
$print_type_info = $this->db->query("CALL sp_a_run ('SELECT','$print_type_query')");
$print_type_result = $print_type_info->result();
$print_type_info->next_result();
$print_template_list = "<option value=''>--- Select Template ---</option>";
foreach($print_type_result as $rslt){
$design_name = strtolower(str_replace(" ","_",$rslt->print_info_name));
$print_info_name = $rslt->print_info_name;
$print_template_list .= "<option value='$design_name'>$print_info_name</option>";
}
echo $print_template_list;
}else{
$print_template_list = "<option value=''>--- No template available ---</option>";
echo $print_template_list;
}
}
}
?>