<?php
   /* function mail_attachment($file, $mailto, $from_name, $from_mail, $subject, $message) {
      //$file = $path.$filename;
	  //$mailto = 'andrisgauracs@gmail.com';
      $file_size = filesize($file);
      $handle = fopen($file, "r");
      $content = fread($handle, $file_size);
      fclose($handle);
      $content = chunk_split(base64_encode($content));
      $uid = md5(uniqid(time()));
      $name = basename($file);
      $header = "From: ".$from_name." <".$from_mail.">\r\n";
      $header .= "MIME-Version: 1.0\r\n";
      $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
      //$header .= "This is a multi-part message in MIME format.\r\n";
      $msg = "--".$uid."\r\n";
      $msg .= "Content-type:text/plain; charset=iso-8859-1\r\n";
      $msg .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
      $msg .= $message."\r\n\r\n";
      $msg .= "--".$uid."\r\n";
      $msg .= "Content-Type: application/octet-stream; name=\"".$file."\"\r\n"; // use different content types here
      $msg .= "Content-Transfer-Encoding: base64\r\n";
      $msg .= "Content-Disposition: attachment; filename=\"".$file."\"\r\n\r\n";
      $msg .= $content."\r\n\r\n";
      $msg .= "--".$uid."--";
      if (mail($mailto, $subject, $msg, $header)) {
         echo "OK";
      } else {
         echo "ERROR";
      }
   } */

   date_default_timezone_set('Etc/UTC');
require '../phpMailer/PHPMailerAutoload.php';

   function mail_attachment($file, $mailto, $from_name, $from_mail, $replyto, $subject, $message) {
   //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";
   //Set the SMTP port number - likely to be 25, 465 or 587
   $mail->Port = 25;
   //Whether to use SMTP authentication
   $mail->SMTPAuth = true;
   //Username to use for SMTP authentication
   $mail->Username = "gatewayandpartners@gmail.com";
   //Password to use for SMTP authentication
   $mail->Password = "Empower123";
   //Set who the message is to be sent from
   $mail->setFrom('gatewayandpartners@gmail.com', 'Gateway And Partners');
   //Set an alternative reply-to address
   $mail->addReplyTo($replyto, $replyto);
   //Set who the message is to be sent to
   $mail->addAddress($mailto, $from_name);
   //Set the subject line
   $mail->Subject = $subject;
   //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($subject);
   //Replace the plain text body with one created manually
   $mail->AltBody = 'This is a plain-text message body';
   //Attach an image file
   $mail->addAttachment($file);

   //send the message, check for errors
   if (!$mail->send()) {
       echo "Mailer Error: " . $mail->ErrorInfo;
   } else {
       echo json_encode(["status"=>'OK']);
   }
}

   $projName=$_GET["proj"];
   $to=$_GET["to"];

   //$to = 'andrisgauracs@gmail.com';


   $fileName=str_replace(array("+"," "), "_", $_GET['proj']).'_Final_Report.xlsx';
   $message='';

   $projName=$_GET["proj"];
   $to=$_GET["to"];

   mail_attachment($fileName, $to, $to, $to, $to, 'Final report in Excel file', $message);
   //unlink($fileName);
?>
