<?php
ini_set('display_errors', 1);

require 'PHPMailerAutoload.php';
require 'log.php';

$link = new mysqli("localhost","homepage_su","01think00","homepage_digital_night");

$result = $link->query('SELECT * FROM users WHERE mail_sent = 0 ORDER BY id DESC LIMIT 1');
$user   = mysqli_fetch_assoc($result);

$result->close();

$fullName = $user['first_name'].' '.$user['last_name'];
$userId   = $user['id'];
$email    = $user['email'];
$link     = 'http://homepage.rs/digitalnight/vip-registracija/?token='.$user['token'];

var_dump($user);


$mail = new PHPMailer;

$mail->CharSet  = "UTF-8";
$mail->From     = 'aleksandar.bobic@homepage.rs';
$mail->FromName = 'Digital Night';
$mail->addAddress('bakicdj@gmail.com');       // Name is optional
$mail->addReplyTo('no-replay@homepage.rs', 'Digital Night No-Reply');

$mail->WordWrap = 50;                                 // Set word wrap to 50 characters
$mail->isHTML(true);                                  // Set email format to HTML

$mail->AddEmbeddedImage('header.png', 'header_png');
$mail->AddEmbeddedImage('organizacija-bgd.png', 'organizacija_png');
$mail->AddEmbeddedImage('registrujse.png', 'registrujse_png');  

$mail->Subject = 'Test email sa slikom';
$mail->Body    = '<table style="background: url(cid:organizacija_png) top center repeat;width: 610px;" border="0" cellpadding="0" cellspacing="0" bgcolor="#fff" width="610"><tbody><tr><td align="left"><h1 style="font-size: 24px;font-weight: bold;margin: 0px 0px 20px 0px;padding: 0px;font: Arial, Helvetica, sans-serif;text-align: center color: #000;letter-spacing: normal;" align="center"><img src="cid:header_png" alt="Digital Night" border="0" width="610" height="107"/></h1></td></tr><tr><td style="line-height: 21px;font: 15px Arial, Helvetica, sans-serif;color: #464646;" width="610"><p style="font: 18px Arial, Helvetica, sans-serif;color: #464646;line-height: 21px;margin: 0;padding: 0px 20px 10px 20px;font-weight: bold;">Pozdrav '.$fullName.',</p><p style="font: 15px Arial, Helvetica, sans-serif;color: #464646;line-height: 21px;margin: 0;padding: 0px 20px 10px 20px;">Sa zadovoljstvom te pozivamo da nam se pridružiš i budeš naš gost/gošća na "Digital Night-u". Molimo te da potvrdiš svoj dolazak tako što ćeš uneti neophodne podatke koji su navedeni na linku:</p><p style="font: 24px Arial, Helvetica, sans-serif;color: #464646;line-height: 30px;margin: 0;padding: 0px 20px 10px 20px;font-weight: bold;text-align: center;"><a style="font: 24px Arial, Helvetica, sans-serif;color: #464646;line-height: 30px;margin: 0;padding: 0px 20px 10px 20px;font-weight: bold;text-align: center;" href="'.$link.'"><img src="cid:registrujse_png" alt="Registruj se"></a></p><p style="font: 15px Arial, Helvetica, sans-serif;color: #464646;line-height: 21px;margin: 0;padding: 0px 20px 10px 20px;">Kada uspešno popuniš sve podatke, možeš da očekuješ da će ti personalizovana karta biti dostavljena na adresu koju si naveo/la najkasnije do 20.07.</p><p style="font: 15px Arial, Helvetica, sans-serif;color: #464646;line-height: 21px;margin: 0;padding: 0px 20px 20px 20px;">Prijem svih gostiju se očekuje od 19:00 – 20:00h.</p><p style="font: 15px Arial, Helvetica, sans-serif;color: #464646;line-height: 21px;margin: 0;font-weight: bold;padding: 0px 20px 20px 20px;">Dobrodošao/la na "Digital Night".</p><p style="font: 11px Arial, Helvetica, sans-serif;color: #464646;line-height: 17px;margin: 0;padding: 0px 20px 10px 20px;">Personalizovana karta važi za jednu osobu.</p></td></tr></tbody></table>';

$log = logger::getInstance();



if(!$mail->send()) {
    $log = logger::getInstance();

    $log->logfile = 'log.txt';
    $log->write('NOT SENT: '. $mail->ErrorInfo);
    $log->write('TO: '. $user['email']);
    $log->write("---------------------------------------------------------------------------------");
    $log->write(" ");
} else {
    echo 'Message has been sent';
    $link->query('UPDATE users SET mail_sent = 1 WHERE id = '.$userId) or die('esfsfesfsefsefesfsefsef');
}

/*
	$mail->isSMTP();
	$mail->Host = 'smtp.gmail.com';
	$mail->SMTPAuth = true;
	$mail->Port = 465;
	$mail->Username = 'aleksandar.bobic@homepage.rs';
	$mail->Password = '!Trt12345';
	$mail->SMTPSecure = 'ssl';
*/
