<?php
set_time_limit(360);
error_reporting(E_ALL & ~(E_NOTICE | E_WARNING)); // 關閉警示及錯誤訊息
// 防止session file lock
session_write_close();

if (isset($_SERVER["HTTP_HOST"])) {
	header('HTTP/1.0 404 Not Found');
	exit;
}

require_once "Parameters.php";
$today = date("Y-m-d");
//$argv= array("", "Month_TGPF", "2021-05-03");
$setup = getIniFile("../cfg/mail_sender", $argv[1]);
file_put_contents("log/".$today."_email_sender.log", print_r($argv, true)."\r\n", FILE_APPEND);

if ($setup[$argv[1]]) {
	$cfg = $setup[$argv[1]];
	switch ($argv[1]) {
		case "Month_TGPF":
			$date = date("Y-m-d", strtotime($argv[2]));
			foreach (array("subject", "attachment") as $_var) {
				if ($_var == "attachment" && $cfg[$_var]) {
					foreach ($cfg[$_var] as $_i => $_f) {
						$cfg[$_var][$_i] = str_replace(array("{@Year@}", "{@Month@}", "{@Day@}"), explode("-", $date), $_f);
					}
				} else {
					$cfg[$_var] = str_replace(array("{@Year@}", "{@Month@}", "{@Day@}"), explode("-", $date), $cfg[$_var]);
				}
			}
			break;
		default:
			die("GG");
	}
	file_put_contents("log/".$today."_email_sender.log", print_r($cfg, true)."\r\n", FILE_APPEND);
	$rtn = sysSendEmail($cfg["to"], $cfg["subject"], $cfg["html_body"], $cfg["cc"], $cfg["attachment"]);
	file_put_contents("log/".$today."_email_sender.log", print_r($rtn, true)."\r\n", FILE_APPEND);
	die(json_encode($rtn));
}

die("Done");
?>