<?php

ob_start();
session_start();





/*
 * required files
 */
require_once ('./models/PHP-MySQLi-Database-Class/MysqliDb.php');
require_once("./models/config.php"); // connect with db and load site info
require_once("./models/functions.php"); // site functions




/*
 * send emails
 */
// $db->setTrace(true);
$db->orderBy("timestamp", "desc");
$db->where('is_sent', 0);
$edit_infos = $db->get('emails', $smtp_cron);
// print_r($db->trace);
if ($db->count > 0) {
  require "./models/phpmailer/class.phpmailer.php"; // include the class name
  foreach ($edit_infos as $edit_info) {

    /*
     * send email
     */

    $subject = $edit_info['subject'] . ' | ' . $site_name;

    $mail = new PHPMailer; // call the class


    if ($smtp_status == 1) {

      $mail->IsSMTP(); // enable SMTP
      $mail->SMTPDebug = false; // debugging: 1 = errors and messages, 2 = messages only

      if ($smtp_auth == 1) {
        $mail->SMTPAuth = true; // authentication enabled
      } else {
        $mail->SMTPAuth = false; // authentication disabled
      }


      $mail->SMTPSecure = $smtp_secure; // secure transfer enabled REQUIRED for GMail
      $mail->Host = $smtp_host;
      $mail->Port = $smtp_port; // or 587
      $mail->Username = $smtp_user;
      $mail->Password = $sms_password;
    }

    $mail->IsHTML(true);
    $mail->SetFrom($site_email, $site_name); //From address of the mail
    // put your while loop here like below,
    $mail->Subject = $subject; //Subject od your mail
    $mail->AddAddress($edit_info['email'], ''); //To address who will receive this email
    $mail->MsgHTML($edit_info['body']); //Put your body of the message you can place html code here
    //$mail->AddAttachment("images/asif18-logo.png"); //Attach a file here if any or comment this line,
    // $send = $mail->Send(); //Send the mails
    if ($mail->Send()) {


      /**
       * update status
       */
      $data = Array(
          'is_sent' => 1,
      );
      $db->where('id', $edit_info['id']);
      $db->update('emails', $data);
    }
  }
}