<?php
	require_once dirname(dirname(dirname(__FILE__)))."/system/email.php";
	require_once dirname(dirname(dirname(__FILE__)))."/system/smtp_mail.php";
	class sends extends page{
		/* line notify
		 * @param $text 訊息
		 * @param $token 密鑰
		 */
		public function linemessage($text, $token) {
			$currentDate = new DateTime();
			$endDate = new DateTime('2025-03-20');
			if ($currentDate > $endDate) return null;
			
			$url = 'https://notify-api.line.me/api/notify';
		
			$data = ['message' => "\n" . $text];
			$datamessage = http_build_query($data);
		
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $datamessage);
			curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $token]);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$messageString = curl_exec($ch);
			curl_close($ch);
			$message = json_decode($messageString, true);
		
			return $message['status'] ?? null;
		}
		
		/* 寄信-email
		 * @param $name 寄件人名稱
		 * @param $email 寄件人信箱
		 * @param $subject 主旨
		 * @param $mailtxt 信件內容
		 * @param $toAddress 目標信箱
		 */
		public function sendemail($name, $email, $subject, $mailtxt, $toAddress) {
			$fromAddress = "=?UTF-8?B?" . base64_encode($this->registry->webName) . "?=<" . $this->registry->webEmail . ">";
			$replyToAddress = "=?UTF-8?B?" . base64_encode($name) . "?=<" . $email . ">";
			
			$email_obj = new email();
			$email_obj->fromAddress = $fromAddress;
			$email_obj->replyToAddress = $replyToAddress;
			$email_obj->subject = $subject;
			$email_obj->content = $mailtxt;
			$email_obj->toAddress = $toAddress;
			
			return $email_obj->send() ? true : false;
		}
		
		/* 寄信-smtp
		 * @param $subject 主旨
		 * @param $mailtxt 信件內容
		 * @param $sendMail 目標信箱(陣列)
		 */
		public function sendsmtpmail($subject, $mailtxt, array $sendMail = []) {
			$name = "=?UTF-8?B?" . base64_encode($this->registry->webName) . "?=";
			
			$smtp_mail = new smtp_mail();
			$smtp_mail->toAry = $sendMail;
			$smtp_mail->name = $name;
			$smtp_mail->subject = $subject;
			$smtp_mail->body = $mailtxt;
			
			return $smtp_mail->send() ? true : false;
		}
	}
?>