Index of /home/htscrtms/newcrm.htshah.com/hslider
<?php
ob_start();
include('lib/db_connect.php');
include('lib/auth.php');
require 'lib/phpmailer/PHPMailer.php';
require 'lib/phpmailer/SMTP.php';
require 'lib/phpmailer/Exception.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
require 'lib/vendor/autoload.php';
date_default_timezone_set("Asia/Kolkata");
$date = date('d-m-Y');
$client_id = $_REQUEST['client_id'];
$selc = "SELECT * FROM `tabl_client` WHERE id='$client_id'";
$qryc = mysqli_query($con, $selc) or die(mysqli_error($con));
$rowc = mysqli_fetch_array($qryc);
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle("Export Report");
$headers = [
"Sr.", "HTSHAH Job", "Client", "Shipping bill no", "Supplier", "PKGs",
"Shipping Co. & Cont. No.", "Empty D.o.", "Factory Stuffing",
"Cut off to cont. no", "Carting", "Exam", "Stuffing",
"Sailing", "Handover of S/Bill", "Shipping Bill Rels",
"Acd/eu decl", "Remarks"
];
$sheet->fromArray($headers, NULL, 'A1');
$rowNum = 2;
$count = 1;
$hasRows = false; // MARK AT LEAST ONE ROW EXISTS
$sel = "SELECT * FROM `tabl_exportentry`
WHERE DELFLAG='N' AND client_id='$client_id'
ORDER BY id DESC";
$qry = mysqli_query($con, $sel) or die(mysqli_error($con));
while ($row = mysqli_fetch_array($qry)) {
// RESTRICT BLANK DATA ROWS
if (
empty($row['htshah_no']) &&
empty($row['thoka_no']) &&
empty($row['supplier']) &&
empty($row['pkgs']) &&
empty($row['vessel'])
) {
continue;
}
$hasRows = true; // VALID ROW FOUND
$sheet->fromArray([
$count++,
$row['htshah_no'],
$rowc['userName'],
$row['thoka_no'],
$row['supplier'],
$row['pkgs'],
$row['vessel'],
$row['empty_doc_recd_dt'],
$row['factory_stuffing_datetime'],
$row['cutoff_cont_no'],
$row['carting'],
$row['exam'],
$row['stuffing'],
$row['sailing'],
$row['handover_of_sb'],
$row['shipping_bill_release_dt'],
$row['acdDeclFiled_dt'],
$row['remark']
], NULL, 'A' . $rowNum++);
}
/* STOP EMAIL IF NO ROWS */
if ($hasRows === false) {
echo '<script>
alert("No valid data found. Email not sent.");
window.location.href="exportReport.php";
</script>';
exit;
}
$filename = 'exportreport.xlsx';
$writer = new Xlsx($spreadsheet);
$writer->save($filename);
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = 'mail.htshah.com';
$mail->Username = '_mainaccount@htshah.com';
$mail->Password = 'rQ9LhUA5R9gp8q3wTW4d';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
$mail->Port = 465;
$mail->setFrom('cse@htshah.com', 'HTSHAH Reports');
$emails = explode(',', $rowc["email"]);
foreach ($emails as $email) {
$mail->addAddress(trim($email));
}
$mail->addCC('htshahcomum@hotmail.com');
$mail->Subject = "Export Report as of " . $date;
$mail->Body = "Please find the attached Export Report.";
$mail->AltBody = "Please find the attached Export Report.";
$mail->addAttachment($filename);
if ($mail->send()) {
echo '<script>
alert("Mail sent successfully.");
window.location.href="exportReport.php";
</script>';
} else {
echo '<script>alert("Mail sending failed.");</script>';
}
} catch (Exception $e) {
echo "Mail could not be sent. PHPMailer Error: {$mail->ErrorInfo}";
}
?>