<?php

include "../root.class.php";


$db = new db_safeguard();

// get events
$events_res = $db->query("events_manager", "SELECT * FROM events_manager WHERE `sent` = 0");
if ($events_res->num_rows > 0) {
    $email = new email();
    $event = $events_res->fetch_assoc();
    echo "<br> EMAIL: <br>";
    $emails_string = $event['email_accounts'];
    $names_string = $event['email_names'];
    $subject = $event['subject'];
    $message = str_replace("*", "'", $event['message_html']);
    $message = str_replace("~", '"', $message);

    $email_array = explode(",", $emails_string);
    echo "<br>NAMES: <br>";

    $name_array = explode(",", $names_string);

    for ($i = 0; $i < count($email_array); $i++) {
        if ($name_array[$i] != '') {
            echo "ACCOUNT : " . $email_array[$i] . " NAME: " . $name_array[$i] . "<br>";
            $email->add_address($email_array[$i], $name_array[$i]);
        }
    }
    $email->subject($subject);
    $email->body($message);
    $names_string = '';
    $emails_string = '';
    $email_array = [];
    $name_array = [];
    $subject = '';
    $message = '';
    echo "<br>";
    if ($email->send()) {
        $db->query("events_manager", "UPDATE `events_manager` SET `sent` = 1 WHERE `record_id` = {$event['record_id']}");

    }

}else{
    echo "NONE FOUND FOR NOW";
}