refined
<?php
#
# Define Vars:
$report_name_interval = "cdr_daily";
// $owner='email from'; //'From' email
// $to_addr='emailto'; //'To' email
// required credentials
require_once '/var/www/html/libs/misc.lib.php';
$root_password = obtenerClaveConocidaMySQL("root", "/var/www/html/");
load_default_timezone();
$dev_logger = true;
$dev_logger_date = new DateTime();
$dev_logger_date = $dev_logger_date->format("Y:m:d H:i:s");
// calc current time
$today = new DateTime();
$today = $today->format("Y:m:d");
// Connect to asteriskcdrdb DB to gather the required data
$ast_servername = "localhost";
$ast_database = "asteriskcdrdb";
$ast_conn = new mysqli($ast_servername, "root", $root_password, $ast_database);
if ($ast_conn->connect_error) {
exit("Connection failed: " . $ast_conn->connect_error);
}
$ast_conn -> set_charset("utf8");
// $sql = "SELECT * FROM cdr WHERE calldate > '2023-01-25 23:03:16' ORDER BY calldate ASC ";
$sql = "SELECT calldate, src, did , duration , disposition FROM cdr WHERE calldate >='$today 00:00:00' AND calldate <='$today 23:59:59' ORDER BY calldate ASC ";
$query = $ast_conn->query($sql);
if ($query->num_rows < 1) { // no new records
$ast_conn->close();
}else{
$mes=''; // init body
$counter = 1;
//Fill e-mail
$headers = "Content-type: text/html; charset=utf8 \r\n";
$headers .= "From: Primatel <$owner>\r\n";
$mes .= '
<table style="max-width: 90%;border-bottom:none; margin: 0 auto;border-spacing: inherit;">
<thead>
<tr style="border-bottom: 2px solid #05477c;">
<th style="padding:10px; color: #fff; max-width: 50px;background:#05477c;">№</th>
<th style="padding:10px; color: #fff; max-width: 250px;background:#05477c;">Date</th>
<th style="padding:10px; color: #fff; max-width: 100px;background:#05477c;">CID</th>
<th style="padding:10px; color: #fff; max-width: 50px;background:#05477c;">DID</th>
<th style="padding:10px; color: #fff; max-width: 250px;background:#05477c;">Duration</th>
<th style="padding:10px; color: #fff; max-width: 50px;background:#05477c;">Status</th>
</tr></thead><tbody>';
while ($row = $query->fetch_assoc()) {
$mes .= '
<tr style="width: 100px;">
<td style="padding:10px; border-bottom: 1px solid #eee; text-align:center;">'.$counter.'</td>
<td style="padding:10px; border-bottom: 1px solid #eee; text-align:center;min-width: 100px;">'.$row['calldate'].'</td>
<td style="padding:10px; border-bottom: 1px solid #eee; text-align:center;min-width: 100px;">'.$row['src'].'</td>
<td style="padding:10px; border-bottom: 1px solid #eee; text-align:center;">'.$row['did'].'</td>
<td style="padding:10px; border-bottom: 1px solid #eee; text-align:center;min-width: 100px;">'.date("H:i:s", mktime(0, 0,$row['duration'])).'</td>
<td style="padding:10px; text-align:center; border-bottom: 1px solid #eee; min-width: 120px; ">'.$row['disposition'].'</td>
</tr>';
$counter = $counter + 1;
}
$mes .='</tbody></table>';
//Send email
mail($to_addr, ''.$report_name_interval.'-'.$today.'', $mes, $headers);
$ast_conn->close();
exit();
}
?>