<?php
//ini_set('display_errors','1');
//error_reporting(E_ALL);
//header('Content-Type: application/json; charset=utf-8');

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\SMTP;
require '../PHPMailer/src/Exception.php';
require '../PHPMailer/src/PHPMailer.php';
require '../PHPMailer/src/SMTP.php';

$mail = new PHPMailer(true);
try {
    //Server settings
    //$mail->SMTPDebug = SMTP::DEBUG_SERVER;                      //Enable verbose debug output
    $mail->isSMTP();                                            //Send using SMTP
    $mail->Host       = 'www.gui-tian.com.tw';                     //Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                   //Enable SMTP authentication
    $mail->Username   = 'mail@gui-tian.com.tw';                     //SMTP username
    $mail->Password   = 'med2@she';                               //SMTP password
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            //Enable implicit TLS encryption
    $mail->Port       = 465;                                    //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
    $mail->CharSet = 'UTF-8';
    $mail->Encoding = 'base64';
    
    //Recipients
    $mail->setFrom('mail@gui-tian.com.tw', 'Guitian');
    //$mail->addAddress('kywei88@gmail.com', 'Joe User');     //Add a recipient
    $mail->addAddress($_REQUEST['email']);               //Name is optional
    //$mail->addReplyTo('info@example.com', 'Information');
    //$mail->addCC('cc@example.com');
    //$mail->addBCC('bcc@example.com');

    //Attachments
    $mail->addAttachment(dirname(__FILE__).'/reports/'.$_REQUEST['orderID']);         //Add attachments
    //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    //Optional name

    //Content
    $mail->isHTML(true);                                  //Set email format to HTML
    //$mail->Subject = '桂田技術顧問報告:'.$_REQUEST['orderID'];
    $mail->Subject = "=?utf-8?B?" . base64_encode("桂田技術顧問報告") . "?=";
    //$mail->Subject = "=?utf-8?B?" . base64_encode("桂田技術顧問報告".$_REQUEST['orderID']) . "?=";
    $mail->Body    = '桂田技術顧問報告。附件名稱：'.$_REQUEST['orderID'].'';
    //$mail->AltBody = '附件名稱：'.$_REQUEST['orderID'].'';

    $mail->send();
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

?>
<html>   
<head>   
<meta http-equiv="refresh" content="5;url=reports.php?id=<?php echo $_REQUEST['id'];?>">   
</head>   
<body> 
郵件已寄出
</body>
</html>