<?php
// include autoloader
require_once 'dompdf/autoload.inc.php';


$reEmail = $_POST['reEmail'];
if (isset($_POST["reEmail"]) && !empty($_POST["reEmail"])){
	$reEmail = filter_var($reEmail, FILTER_SANITIZE_EMAIL);
}
else{
	
	exit("Must input the received email address");
}

$htmlData = $_POST['htmlData'];

$htmlData = 
'<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><style>font-family: ms-gothic, DejaVu Sans, serif;</style></head><body>'.$htmlData.'</body></html>';


// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml($htmlData);
//$dompdf->set_option('isFontSubsettingEnabled', true);

// (Optional) Setup the paper size and orientation
// Defautl DPi 96. So A4 dimension will 794x1123
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
//$dompdf->stream();

// Output the generated PDF (1 = download and 0 = preview)
//$dompdf->stream("codex",array("Attachment"=>0));






$output = $dompdf->output();


require 'PHPMailer/PHPMailerAutoload.php';
//SMTP time zone set
date_default_timezone_set('Etc/UTC');

///
///Send mail by using gmail smtp
///
//Create a new PHPMailer instance
$mail = new PHPMailer;

//Tell PHPMailer to use SMTP
$mail->isSMTP();

//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "naitce@gmail.com";
//Password to use for SMTP authentication
$mail->Password = "Xinchaocacban";
//Set who the message is to be sent from
$mail->setFrom('noreply@timesfun.net', '3D House');
//Set an alternative reply-to address
//$mail->addReplyTo('replyto@example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress($reEmail, '');
//Set the subject line
$mail->Subject = 'An estimation table from 3D House';
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents('mailContent.html'), dirname(__FILE__));
//Replace the plain text body with one created manually
$mail->AltBody = 'Please get file in attachment';
//Attach an image file
$mail->AddStringAttachment($output, '3DHouse-Estimation.pdf', 'base64', 'application/pdf');
//$mail->addAttachment($output,'application/pdf','3DHouse-Estimation.pdf', false);
//$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Email is sent!";
}


?>