<?php

//index.php


//$connect = new PDO("mysql:host=localhost;dbname=testing", "root", "");

//function fetch_customer_data($connect)
//{
//	$query = "SELECT * FROM tbl_customer";
//	$statement = $connect->prepare($query);
//	$statement->execute();
//	$result = $statement->fetchAll();
//	$output = '
//	<div class="table-responsive">
//		<table class="table table-striped table-bordered">
//			<tr>
//				<th>Name</th>
//				<th>Address</th>
//				<th>City</th>
//				<th>Postal Code</th>
//				<th>Country</th>
//			</tr>
//	';
//	foreach($result as $row)
//	{
//		$output .= '
//			<tr>
//				<td>'.$row["CustomerName"].'</td>
//				<td>'.$row["Address"].'</td>
//				<td>'.$row["City"].'</td>
//				<td>'.$row["PostalCode"].'</td>
//			</tr>
//		';
//	}
//	$output .= '
//		</table>
//	</div>
//	';
//	return $output;
//}


	include('pdf.php');
	$file_name = md5(rand()) . '.pdf';
	$html_code = '<html><body><h1>';
	$html_code .= 'INFORME 1</h1></body></html>';
	$pdf = new Pdf();
	$pdf->load_html($html_code);
	$pdf->render();
	$file = $pdf->output();
	file_put_contents($file_name, $file);
	
$to          = "joelmanquilepe@gmail.com"; // addresses to email pdf to
$from        = "exploratorioubb@betech.cl"; // address message is sent from
$subject     = "Your PDF email subject"; // email subject
$body        = "<p>The PDF is attached.</p>"; // email body
$pdfLocation = $file_name; // file location
$pdfName     = $file_name; // pdf file name recipient will get
$filetype    = "application/pdf"; // type

// create headers and mime boundry
$eol = PHP_EOL;
$semi_rand     = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers       = "From: $from$eol" .
  "MIME-Version: 1.0$eol" .
  "Content-Type: multipart/mixed;$eol" .
  " boundary=\"$mime_boundary\"";

// add html message body
  $message = "--$mime_boundary$eol" .
  "Content-Type: text/html; charset=\"iso-8859-1\"$eol" .
  "Content-Transfer-Encoding: 7bit$eol$eol" .
  $body . $eol;

// fetch pdf
$file = fopen($pdfLocation, 'rb');
$data = fread($file, filesize($pdfLocation));
fclose($file);
$pdf = chunk_split(base64_encode($data));

// attach pdf to email
$message .= "--$mime_boundary$eol" .
  "Content-Type: $filetype;$eol" .
  " name=\"$pdfName\"$eol" .
  "Content-Disposition: attachment;$eol" .
  " filename=\"$pdfName\"$eol" .
  "Content-Transfer-Encoding: base64$eol$eol" .
  $pdf . $eol .
  "--$mime_boundary--";

// Send the email
if(mail($to, $subject, $message, $headers)) {
  echo "The email was sent.";
}
else {
  echo "There was an error sending the mail.";
}


?>






