<?php
ob_start();
include("config.php");
require '../mail/PHPMailerAutoload.php';
$time = time()-3600;
$resultReportMsg = mysqli_query($conn, "SELECT * FROM `history` WHERE `result_json`!='' AND `send_email`='0' AND `api` !='1' AND `date`<='$time' ORDER BY `id` ASC LIMIT 0,20");
$numReportMsg = mysqli_num_rows($resultReportMsg);
if(empty($numReportMsg))
{
	mysqli_close($conn);
	ob_end_flush();
	exit();
}
if(empty($rowEmailData))
{
	mysqli_close($conn);
	ob_end_flush();
	exit();
}
while($rowReportMsg = mysqli_fetch_assoc($resultReportMsg))
{
	$to = $rowEmailData;
	$email = "";
	$mime_boundary = "----Yazeeno----".md5(time());
		# -=-=-=- MAIL HEADERS -=-=-=- #
	$message = "<head>";
	$message.= "</head>";
	$message.= "<body>";
	$message.= "<div align='left'>";
	$message.= "<table border='0' dir='ltr' align='left' width='600' cellpadding='2' cellspacing='0' style='border:1px solid #BCCBE0; color:#6E6E6E; border-collapse: collapse'>";
	$message.= "<tr>";
	$message.= "<td align='left' style='color:#6E6E6E;white-space: nowrap'>Sender : </td>";
	$message.= "<td width='75%' align='left' style='color:#6E6E6E;text-align:left'>".$rowReportMsg['userSender']."</td>";
	$message.= "</tr>";
	$message.= "<tr>";
	$message.= "<td align='left' style='color:#6E6E6E;white-space: nowrap'>Message : </td>";
	$message.= "<td width='75%' align='left' style='color:#6E6E6E;text-align:left'>".$rowReportMsg['msg']."</td>";
	$message.= "</tr>";
	$message.= "<tr>";
	$message.= "<td align='left' style='color:#6E6E6E;white-space: nowrap'>Rejected numbers  : </td>";
	$message.= "<td width='75%' align='left' style='color:#6E6E6E;text-align:left'>".$rowReportMsg['rejectedNumber']."</td>";
	$message.= "</tr>";
	$message.= "<tr>";
	$message.= "<td align='left' style='color:#6E6E6E;white-space: nowrap'>Count of rejected numbers : </td>";
	$message.= "<td width='75%' align='left' style='color:#6E6E6E;text-align:left'>".$rowReportMsg['countNotAccepted']."</td>";
	$message.= "</tr>";
	$message.= "<tr>";
	$message.= "<td align='left' style='color:#6E6E6E;white-space: nowrap'>Delivered numbers : </td>";
	$message.= "<td width='75%' align='left' style='color:#6E6E6E;white-space: nowrap;text-align:left'>".$rowReportMsg['delivered']."</td>";
	$message.= "</tr>";
	$message.= "<tr>";
	$message.= "<td align='left' style='color:#6E6E6E;white-space: nowrap'>Count of delivered numbers : </td>";
	$message.= "<td width='75%' align='left' style='color:#6E6E6E;white-space: nowrap;text-align:left'>".$rowReportMsg['count_delivered']."</td>";
	$message.= "</tr>";
	$message.= "<tr>";
	$message.= "<td align='left' style='color:#6E6E6E;white-space: nowrap'>Not delivered numbers  : </td>";
	$message.= "<td width='75%' align='left' style='color:#6E6E6E;white-space: nowrap;text-align:left'>".$rowReportMsg['not_delivered']."</td>";
	$message.= "</tr>";
	$message.= "<tr>";
	$message.= "<td align='left' style='color:#6E6E6E;white-space: nowrap'>Count of not delivered numbers  : </td>";
	$message.= "<td width='75%' align='left' style='color:#6E6E6E;white-space: nowrap;text-align:left'>".$rowReportMsg['count_not_delivered']."</td>";
	$message.= "</tr>";
	$message.= "<tr>";
	$message.= "<td align='left' style='color:#6E6E6E;white-space: nowrap'>Report for numbers  : </td>";
	$message.= "<td width='75%' align='left' style='color:#6E6E6E;white-space: nowrap;text-align:left'>".$rowReportMsg['result_json']."</td>";
	$message.= "</tr>";
	$message.= "<tr>";
	$message.= "<td align='left' style='color:#6E6E6E;white-space: nowrap'>accepted numbers : </td>";
	$message.= "<td width='75%' align='left' style='color:#6E6E6E;white-space: nowrap;text-align:left'>".$rowReportMsg['accepted']."</td>";
	$message.= "</tr>";
	$message.= "<tr>";
	$message.= "<td align='left' style='color:#6E6E6E;white-space: nowrap'>Count of accepted numbers  : </td>";
	$message.= "<td width='75%' align='left' style='color:#6E6E6E;white-space: nowrap;text-align:left'>".$rowReportMsg['count_accepted']."</td>";
	$message.= "</tr>";
	$message.= "</table>";
	$message.= "</div>";
	$message.= "</body>";
	$message.= "</html>";
	$title="DLR Report For Sent SMS in 1 hour";
	$mail = new PHPMailer;
	$mail->isSMTP();
	$mail->SMTPDebug = 2;
	$mail->Debugoutput = 'html';
	$mail->Host = $host;
	$mail->Port = 25;
	$mail->SMTPAuth = true;
	$mail->Username = $smtpUsername;
	$mail->Password = $smtpPassword;
	$mail->setFrom($From, $FromName);
	$mail->addReplyTo($From, $FromName);
	$mail->addAddress($to, $FromName);
	$mail->Subject = $title;
	$mail->msgHTML($message);
	$mail->AltBody = '';
	if($mail->send())
		$resultUpdate = mysqli_query($conn, "UPDATE `history` SET `send_email`='1' WHERE `id` = '$rowReportMsg[id]'");
	
	
}
mysqli_close($conn);
ob_end_flush();

?>