<?php 
//error_reporting(0);
require 'PHPMailer-master/PHPMailerAutoload.php';
date_default_timezone_set("Asia/Kolkata");
include("../config.php");

$date = date('Y-m-d');
$nextDate = date('Y-m-d',strtotime('+7 days'));

$sqlClientName =  "SELECT * FROM tbl_login WHERE status = 'Active' && expDate BETWEEN '$date' AND '$nextDate' ";
$queryClientName = mysqli_query($con,$sqlClientName);
$numCountClientName = mysqli_num_rows($queryClientName);
$output = '';
if($numCountClientName > 0){
    $output .= '<h3 style="text-align:center"> Pumpmanager Client Expiry Soon</h3>';
        
        $output .= '<table style = "font-family: Arial, Helvetica, sans-serif; border-collapse: collapse; width: 100%;">
            <thead>
                <tr>
                    <th style="padding-top: 12px; padding-bottom: 12px; text-align: center; background-color: #04AA6D; color: white;border: 1px solid #ddd;">Sr.no</th>
                    <th style="padding-top: 12px; padding-bottom: 12px; text-align: center; background-color: #04AA6D; color: white;border: 1px solid #ddd;">Client Name</th>
                    <th style="padding-top: 12px; padding-bottom: 12px; text-align: center; background-color: #04AA6D; color: white;border: 1px solid #ddd;">Client Mobile</th>
                    <th style="padding-top: 12px; padding-bottom: 12px; text-align: center; background-color: #04AA6D; color: white;border: 1px solid #ddd;">Expiry Date</th>
                </tr>
            </thead>
            <tbody>';
    
    $i = 1;
    while($rowClientName = mysqli_fetch_array($queryClientName)){
        $output .= "<tr>";
        $output .= '<td style = "border: 1px solid #ddd; padding: 8px;">' .$i++ .'</td>';
        $output .= '<td style = "border: 1px solid #ddd; padding: 8px;">' .$rowClientName['cName'].'</td>';
        $output .= '<td style = "border: 1px solid #ddd; padding: 8px;">' .$rowClientName['cName'].'</td>';
        $output .= '<td style = "border: 1px solid #ddd; padding: 8px;">' .$rowClientName['expDate'] .'</td>';
        $output .= "</tr>";
    }
    $output .= "</tbody></table>";
    $output .= "<br><br>";
    $ownerEmailArray = [
        ["emp_name" => "Moresh Kashikar", "emp_mail" => "moresh@qispl.com "],
        ["emp_name" => "Yogesh Watile", "emp_mail" => "yogesh@qispl.com "],
        ["emp_name" => "Anjali Tabhane", "emp_mail" => "anjali.tabhane@qispl.com"]
    ];

    $msg = '';
    $msg .= $output ;
    echo $msg ;
    foreach($ownerEmailArray as $value){
        $emp_name = $value['emp_name'];
        $EmailSubject = 'Upcoming Client Expiry Reminder - Pumpmanager';
        $bcc = $value['emp_mail'];
        $cc = "";
        $ToEmail = $value['emp_mail'];
        $mailSuccess = sendmail($ToEmail,$emp_name, $EmailSubject, $msg,$bcc,$cc);
    }
}
function sendmail($to,$nameto,$subject,$message,$bcc,$cc){
    $from  = "imconnectinfo@gmail.com";
    $namefrom = "PUMPMANAGER";
    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->SMTPDebug  = SMTP::DEBUG_OFF;
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 587;
    $mail->SMTPSecure = 'tls';
    $mail->SMTPAuth = true;
    $mail->Username = $from;
    $mail->Password = "lktg iakp fhbc groq";
    $mail->setFrom($from,$namefrom);
    $mail->addReplyTo($from,$namefrom);
    $mail->addAddress($to,$nameto);
    $mail->addBcc($bcc);
    $mail->AddCC($cc);
    $mail->AddCC($cc2);
    $mail->AddCC($cc3);
    $mail->AddCC($cc4);
    $mail->Subject = $subject;
    $mail->msgHTML($message);
    return $mail->send();
}
        
?>