<?php
/**
 * Created by PhpStorm.
 * User: noman sheikh
 * Date: 1/11/2016
 * Time: 2:26 PM
 */

include("../classes/manager.class.php");
require '../classes/PHPMailer/PHPMailerAutoload.php';

extract($_REQUEST);
$m =new Manager();
$msg = null;
function ForgetPassword($un) {

    global $db;
    $query = "SELECT * FROM user_setup WHERE username = '$un' AND status = 'active'";
    //print($query);exit;
    $rows = $db->query_first($query);

    if($db->affected_rows > 0){

        return $rows;
    }else{

        return false;
    }
}


$un= $username;
$forget = ForgetPassword($un);

if($forget){
//$a = mysql_fetch_array($result);

    $Email = $forget->email;
    $username = $forget->username;
    $password = $forget->password;
    $to=$Email;
    $from=__RESOURCE_EMAIL__;

    $subject="Welcome to ".PROJECT_NAME;

    $message="<h2>Welcome to ".PROJECT_NAME."</h2>
				<br><br>
				Dear ".$a['username'].",
				<br><br>
				<p>Please keep this email for your records. Your account information is as follows:</p>
				 <br><br>
				----------------------------<br>
				Username: ".$username."<br>
				Password: ".$password."<br>
				----------------------------<br>

				 <p>For any Queries, please feel free to contact us anytime at,</p>
				 <br><br>
					<b>".PROJECT_NAME." Company Limited</b>
					<br>
					".PROJECT_ADDRESS."
					<br>
					Tell: ".PROJECT_TEL."
					<br>
					Email: ".PROJECT_EMAIL."
					<br><br>

					Please visit our website ".PROJECT_WEBSITE." for our Company & Products information.
					<br><br>

					For & on behalf of
					<br><br>
					<b>".PROJECT_NAME." Company Limited</b>";
    $separator = md5(time());
    $eol = PHP_EOL;

    $attachment = chunk_split(base64_encode($pdfdoc));


    $headers  = "From: ".$from.$eol;
    $headers .= "Cc: ".__EMAIL_CC__. $eol;
    $headers .= "Bcc: ".__EMAIL_BCC__. $eol;
    $headers .= "MIME-Version: 1.0".$eol;
    $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol.$eol;
    $headers .= "Content-Transfer-Encoding: 7bit".$eol;
    $headers .= "This is a MIME encoded message.".$eol.$eol;
    // message
    $headers .= "--".$separator.$eol;
    $headers .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
    $headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
    $headers .= $message.$eol.$eol;
    // attachment
    $headers .= "--".$separator.$eol;
    $headers .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol;
    $headers .= "Content-Transfer-Encoding: base64".$eol;
    $pdffile = "Policy wording Health.pdf";
    $headers .= "Content-Disposition: attachment; filename=$pdffile".$eol.$eol;
    $headers .= $attachment.$eol.$eol;
    $headers .= "--".$separator."--";
    // send message
    //mail($to, $subject, "", $headers);
    ///////////// Mail Sent with PDF attached ///////////////////////
    $mail = new PHPMailer();
    $mail->From = $from;
    $mail->FromName = '';
    //echo $to;
    $mail->addAddress($to, '');
    $mail->addCC(__EMAIL_CC__);
    $mail->addBCC(__EMAIL_BCC__);
    $mail->isHTML(true);
    $mail->Subject = $subject;
    $mail->Body = $message;
    if(!$mail->send()) {
        $msg = 2;
        echo $msg;
        die();
        //echo 'Message could not be sent.';
    }else{
        $msg = 1;
        echo $msg;
        die();
        //echo 'Send';
    }


//echo json_encode($arr);

//echo $a;  // for correct login response

}
else{
    $msg = 0;
    echo $msg;
    die();
}
