<?php 
require_once('phpmailer/PHPMailerAutoload.php');
include_once("encrypt.php");

function send_cc_email($C_email="",$C_message="",$C_subject="Hanyu+ Online Platform",$madd="",$C_bcc=""){   
  $mail= new  PHPMailer();                             //建立新物件
  // $mail->SMTPDebug = 2; // debug 模式,顯示發送狀況
  $mail->IsSMTP();                                    //設定使用SMTP方式寄信
  $mail->SMTPAuth = true;                        //設定SMTP需要驗證  
  $mail->SMTPSecure = "ssl";                    // Gmail的SMTP主機需要使用SSL連線:"ssl"
  $mail->Host ="smtp.gmail.com" ;             //Gamil的SMTP主機"smtp.gmail.com"
  $mail->Port = 465;                          //Gamil的SMTP主機的埠號(Gmail為465)。
  $mail->CharSet = "utf-8";                       //郵件編碼   
  $mail->Username =  getenv('mailUsername');       //Gamil帳號
  $mail->Password = getenv('mailPassword');                //Gmail密碼 
  $mail->From = "hanyuvr@frontierteches.tw";        //寄件者信箱
  $mail->FromName = "Hanyu VR app";                  //寄件者姓名
  $mail->Subject =$C_subject; //郵件標題
  $mail->Body = $C_message; //郵件內容
  if(!empty($madd)){
    $mail->addAttachment($madd); //附件，改以新的檔名寄出
  }
  //  $mail->addAttachment('../uploadfile/file/dirname.png','new.jpg'); //附件，改以新的檔名寄出
  $mail->IsHTML(true);                             //郵件內容為html
  //$mail->AddAddress("$C_email");            //收件者郵件及名稱
  if (is_array($C_email)) {           
    foreach ($C_email as $memail=>$mname) {
        $mail->AddAddress("$memail","$mname");            //收件者郵件及名稱
    }
  } else {
      $mail->AddAddress("$C_email");            //收件者郵件及名稱
  }
  //$mail->AddAddress($mail->From); 
  if (is_array($C_bcc)) {           
    foreach ($C_bcc as $memail=>$mname) {
        $mail->AddBCC("$memail","$mname");  // 密件副本 
    }
  } else {
      $mail->AddBCC("$C_bcc");  // 密件副本 
  }
  $mail->AddBCC($mail->From); 
  $mRes=$mail->Send();
  if($mRes===FALSE){
    echo "Mailer Error: " . $mail->ErrorInfo;
     return false;
  }else{
     return true;
  }       
}

function send_to_email($C_email="",$C_message="",$C_subject="Hanyu+ Online Platform",$madd="",$C_bcc=""){     
  $mail= new  PHPMailer();                             //建立新物件
 // $mail->SMTPDebug = 2; // debug 模式,顯示發送狀況
  $mail->IsSMTP();                               //設定使用SMTP方式寄信
  $mail->SMTPAuth = true;                        //設定SMTP需要驗證:true
  
  $mail->SMTPSecure = "tls";                    // Gmail的SMTP主機需要使用SSL連線:"ssl"
  $mail->Host = getenv('mailHost') ;             //Gamil的SMTP主機"smtp.gmail.com"
  $mail->Port = getenv('mailPort') ;              //Gamil的SMTP主機的埠號(Gmail為465)。
  $mail->CharSet = "utf-8";                       //郵件編碼   
  
  $mail->Username =  getenv('mailUsername');       //Gamil帳號
  $mail->Password = getenv('mailPassword');                //Gmail密碼 
  $mail->From = "HanyuVR@frontierteches.tw";
  $mail->FromName = "HanyuVR";                  //寄件者姓名
  $mail->Subject =$C_subject; //郵件標題
  $mail->Body = $C_message; //郵件內容

  // 设置 Reply-To 和 Return-Path
  $mail->addReplyTo("support@frontierteches.tw", "HanyuVR"); // 确保回复地址正确
  //$mail->ReturnPath = "support@frontierteches.tw"; // 设置退信地址

  if(!empty($madd)){
    $mail->addAttachment($madd); //附件，改以新的檔名寄出
  }
  //  $mail->addAttachment('../uploadfile/file/dirname.png','new.jpg'); //附件，改以新的檔名寄出
  $mail->IsHTML(true);                             //郵件內容為html
  //$mail->AddAddress("$C_email");            //收件者郵件及名稱
  if (is_array($C_email)) {           
    foreach ($C_email as $memail) {
        $mail->AddAddress("$memail","$memail");            //收件者郵件及名稱
    }
  } else {
      $mail->AddAddress("$C_email");            //收件者郵件及名稱
  }
  //$mail->AddAddress($mail->From); 
  
  if (is_array($C_bcc)) {           
    foreach ($C_bcc as $memail) {
        $mail->AddBCC("$memail");  // 密件副本 
    }
  } else {      
      $mail->AddBCC("$C_bcc");  // 密件副本 
  }
  $mail->AddBCC($mail->From); 
  $mRes=$mail->Send();
  if($mRes===FALSE){
    echo "Mailer Error: " . $mail->ErrorInfo;
     return false;
  }else{
     return true;
  }       
}

function send_email_regist($C_email="",$mgo=""){ 
  if(($C_email=="")){return false;}
  $mhere = "<span style='background-color: 	#0080FF; padding: 2px; color: white;'>" . $mgo . "</span>";
  $filename = "email_forgetPassword.html";
  $handle = fopen($filename, "r");
  $C_message = fread($handle, filesize($filename));
  fclose($handle);
  $C_message=str_replace("{here}", $mhere, $C_message); 
  $C_subject="負碳平台忘記密碼"; 
  return send_to_email($C_email,$C_message,$C_subject);
}

/// 註冊信, email 確認
function send_email_regist2($C_email, $C_rid, $mgo, $key, $iv){ 
  if(($C_email == "" || $C_rid == '')){return false;}
  // $url = "http://localhost:8080/custRegist_confirm.php?action=";
  // $url = "https://admin.hanyu-ai.com/demo/hanyuVR/custRegist_confirm.php?action=";
  // $url = "https://hanyu.azurewebsites.net/custRegist_confirm.php?action=";
  $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
  $host = $_SERVER['HTTP_HOST'];
  if($host== "127.0.0.1" ){
     $host=$host."/nchc_nz";
  }
  $fullUrl = $protocol . $host;
  $url = $fullUrl . "/custRegist_confirm.php?action=";
  $mgoUrl = $mgo . $C_email . "&userRid=" . $C_rid;
  $url2 = encrypt($mgoUrl, $key, $iv);
  $mhref = $url . $url2;
  $mhere = "<a href='{$mhref}' style='font-size: 24px; text-decoration: underline;'> here </a>";
 
  $filename = "email_regist.html";
  $handle = fopen($filename, "r");
  $C_message = fread($handle, filesize($filename));
  fclose($handle);
  $C_message=str_replace("{here}", $mhere, $C_message); 
  $C_message=str_replace("{href}", $mhref, $C_message);
  $C_subject="負碳平台帳號確認"; 
  return send_to_email($C_email,$C_message,$C_subject);
}

