ESH/batch_mail.php
2024-10-23 18:28:06 +05:30

214 lines
5.7 KiB
PHP

<?php
include('includes/config/config.php');
include('log_entry.php');
include('includes/functions.php');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'src/Exception.php';
require 'src/PHPMailer.php';
require 'src/SMTP.php';
function send_mail($toList, $ccList, $subject, $messageBody, $remarks)
{
error_log("email_sender send_mail");
error_log("toList:" . $toList);
error_log("ccList:" . $ccList);
error_log("subject:" . $subject);
error_log("mail_content:" . $messageBody);
$mail = new PHPMailer;
/*$mail_data = explode(',',$str_dat);
//unset("info@ohctech.com");
$mail->SMTPDebug = 2; //enable debugging if failure
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = "mail.ohctech.com"; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = "info@ohctech.com"; // SMTP username
$mail->Password = "Tikora@1234"; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port ="587"; // TCP port to connect to
$mail->isHTML(true);
// Sender info
$mail->setFrom("info@ohctech.com", 'OHC');
$mail->addReplyTo("info@ohctech.com", 'OHC');
$from="info@ohctech.com";
*/
$from = getConfigKey('email_username');
$headers = "From: OHCTECH\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$mail_content = "Welcome to OHC. Your OTP to verify Your Email ID is";
$toList = str_replace(' ', ',', $toList);
$toList = str_replace(';', ',', $toList);
$ccList = str_replace(' ', ',', $ccList);
$ccList = str_replace(';', ',', $ccList);
$toList_arr = preg_split("/\,/", $toList);
$ccList_arr = preg_split("/\,/", $ccList);
try {
$mail->isSMTP(); // Set mailer to use SMTP
$mail->SMTPDebug = 2;
$mail->Host = getConfigKey('email_host'); // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = getConfigKey('email_username'); // SMTP username
$mail->Password = getConfigKey('email_password'); // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = getConfigKey('email_port'); // TCP port to connect to
$mail->isHTML(true);
// Sender info
$mail->setFrom(getConfigKey('email_username'), 'OHC');
$mail->addReplyTo(getConfigKey('email_username'), 'OHC');
foreach ($toList_arr as $toaddr) {
$mail->addAddress($toaddr);
}
foreach ($ccList_arr as $ccaddr) {
$mail->addCC($ccaddr);
}
$style = "<style>
body{
font-size:12px;
}
.table {
width: 100%;
margin-bottom: 1rem;
color: #212529;
background-color: transparent;
}
.table th,
.table td {
padding: 0.75rem;
vertical-align: top;
border-top: 1px solid #dee2e6;
}
.table thead th {
vertical-align: bottom;
border-bottom: 1px solid #dee2e6;
}
.table tbody + tbody {
border-top: 1px solid #dee2e6;
}
.table-sm th,
.table-sm td {
padding: 0.3rem;
}
.table-bordered {
border: 1px solid #dee2e6;
text-align:center;
}
.table-bordered th,
.table-bordered td {
border: 1px solid #dee2e6;
}
.table-bordered thead th,
.table-bordered thead td {
border-bottom-width: 2px;
}
</style>";
$mail->Subject = $subject;
$mail->Body = $style . $messageBody . $remarks;
error_log("email_sender done settings");
if (!$mail->send()) {
// echo "Message could not be sent. Mailer Error: " . $mail->ErrorInfo;
error_log('Message could not be sent. Mailer Error: ' . $mail->ErrorInfo);
} else {
// echo "Message has been sent";
error_log("Message has been sent");
}
error_log('Message has been sent.');
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
}
$currentDate = date('Y-m-d');
$sql = "select * from batch_status where run_date = '$currentDate'";
error_log("query to get batch mail data " . $sql);
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
error_log("data " . $row);
if (trim($row['run_date']) != '0000-00-00' && trim($row['run_date']) != null) {
send_batch_mail($row);
} else {
error_log("No Record Found For Date " . $currentDate);
}
}
function send_batch_mail($row)
{
$issues = $row['error'];
$updates = $row['update_count'];
$inserts = $row['insert_count'];
$run_date = date('d-m-Y', strtotime($row['run_date']));
$status = $row['status'];
$to_list = getBatchToList();
$subject = "Patient Batch Update Details";
$message = "Patient Batch Update Summary - " . $run_date . "\n\n";
$message .= "Total Update Records: " . $updates . "\n\n";
$message .= "Total Insert Records: " . $inserts . "\n\n";
$message .= "Status: " . $status . "\n\n";
if (!empty($issues)) {
$message .= "Issues:\n";
$message .= $issues . "\n";
}
error_log("To list " . print_r($to_list, true));
if ($to_list) {
if (send_mail($to_list, '', $subject, $message, '')) {
error_log("Batch email sent successfully to " . $to_list);
} else {
error_log("Error sending batch email to " . $to_list);
}
} else {
error_log("To list is empty to send mail " . $to_list);
}
}
function getBatchToList()
{
$sql = "select value from config where key_name ='to_list'";
$result = mysqli_query($GLOBALS['conn'], $sql);
$row = mysqli_fetch_assoc($result);
return $row['value'];
}