<?php
include("config.php");
include("functions.php");
include("libraries/phpmailer/PHPMailerAutoload.php");

$mysqli = new mysqli($config['db_host'], $config['db_user'], $config['db_pass'], $config['db_name']);
mysqli_set_charset($mysqli, "utf8");

/*
$emails = "SELECT e.email, u.strName as name, u.strTel as tel
FROM `".CRMADATBAZIS."`.`nonprofit` n
LEFT JOIN `".CRMADATBAZIS."`.`u_emails` e ON n.nUserID = e.nUserID
LEFT JOIN `".CRMADATBAZIS."`.`users` u ON n.nUserID = u.nUserID
WHERE LEFT(n.expire_datetime, 10) = CURDATE() AND hlID LIKE '94%' AND e.newsletter = 1";
*/

$emails = "SELECT e.email, u.strName as name, u.strTel as tel
FROM `" . CRMADATBAZIS . "`.`nonprofit` n
LEFT JOIN `" . CRMADATBAZIS . "`.`u_emails` e ON n.nUserID = e.nUserID
LEFT JOIN `" . CRMADATBAZIS . "`.`users` u ON n.nUserID = u.nUserID
WHERE LEFT(n.expire_datetime, 10) = CURDATE() AND hlID LIKE '99%'";

//Cadline-nak és Marconak a lejáró nonprofit kulcsok
if ($result = mysqli_query($mysqli, $emails)) {
  $num_rows = mysqli_num_rows($result);
  $mail = new PHPMailer;
  $mail->CharSet = 'UTF-8';
  $mail->isSMTP();
  $mail->SMTPAuth = true;
  $mail->Host = $mailconfig['Host'];
  $mail->Username = $mailconfig['Username'];
  $mail->Password = $mailconfig['Password'];
  $mail->SMTPSecure = $mailconfig['SMTPSecure'];
  $mail->Port = $mailconfig['Port'];
  $mail->isHTML(TRUE);
  $mail->setFrom(DEFAULT_EMAIL);
  $mail->addAddress('arpad.simon@cadline.hu');
  $mail->addBCC('webdeveloper@cadline.hu');
  $mail->Subject = 'Lejáró nonprofit user(ek) adatai';
  $message = "Lejáró nonprofit user(ek) adatai: <br />";

  if ($num_rows = mysqli_num_rows($result) == 0) {
    $message .= "Ma nincs lejáró non-profit kulcs";
  }

  while ($row = mysqli_fetch_array($result)) {
    $message .= "Name: " . $row['name'] . "<br /> E-mail: " . $row['email'] . "<br /> Telephone: " . $row['tel'] . "<br /><br />";
  }

  $mail->msgHTML($message);
  $mail->AltBody = strip_tags($message);
  $mail->send();
  unset($mail);
} else {
  die("Hiba");
}

/*
$emails2 = "SELECT e.email FROM `".CRMADATBAZIS."`.`nonprofit` n LEFT JOIN `".CRMADATBAZIS."`.`u_emails` e ON n.nUserID = e.nUserID WHERE LEFT(n.expire_datetime, 10) = CURDATE() AND hlID LIKE '94%' AND e.newsletter = 1";
*/

$emails2 = "SELECT e.email FROM `" . CRMADATBAZIS . "`.`nonprofit` n
LEFT JOIN `" . CRMADATBAZIS . "`.`u_emails` e ON n.nUserID = e.nUserID
WHERE LEFT(n.expire_datetime, 10) = CURDATE() AND hlID LIKE '99%'";

//Magának a usernek
if ($result = mysqli_query($mysqli, $emails2)) {
  if ($num_rows = mysqli_num_rows($result) != 0) {
    while ($row = mysqli_fetch_array($result)) {
      for ($i = 0; $i < $num_rows; $i++) {
        $mail = new PHPMailer;
        $mail->CharSet = 'UTF-8';
        $mail->isSMTP();
        $mail->SMTPAuth = true;
        $mail->Host = $mailconfig['Host'];
        $mail->Username = $mailconfig['Username'];
        $mail->Password = $mailconfig['Password'];
        $mail->SMTPSecure = $mailconfig['SMTPSecure'];
        $mail->Port = $mailconfig['Port'];
        $mail->isHTML(TRUE);
        $mail->setFrom(DEFAULT_EMAIL);
        $mail->addAddress('arpad.simon@cadline.hu');
        //$mail->addAddress($row[$i]);
        $mail->Subject = 'Lejár a non-profit kulcsod';
        $message = "
ITA
<p>
Il periodo di valutazione di ARCHLine.XP, versione completa per la progettazione architettonica, è scaduto. Il software tornerà nella modalità iniziale, con le procedure di ARCHLine.XP Namirial BIM
La ringraziamo per l’interesse dimostrato, e le ricordiamo che le è stata riservata un’offerta speciale nel caso desideri valutare l’acquisto di una licenza dell’architettonico.
Per maggiori informazioni in merito, la invitiamo a contattare gli uffici di Namirial o di Cadline Software.
Cordiali Saluti.</p>


ENG
<p>
The evaluation period of ARCHLine.XP, full version for architectural design, has expired. The software reverts back to the ARCHLine.XP Namirial BIM mode.
Thank you for your interest, we remember you that we reserved you a special offer, if you wish to evaluate to purchase the full license of this architectural solution.
For more information, please contact Namirial or Cadline Software.
Yours sincerely.
</p>";
        $mail->msgHTML($message);
        $mail->AltBody = strip_tags($message);
        $mail->send();
        unset($mail);
      }
    }
  }
} else {
  die("Hiba");
}

mysqli_close($mysqli);
