diff --git a/.gitignore b/.gitignore index 6d374e4..2c13f95 100755 --- a/.gitignore +++ b/.gitignore @@ -43,6 +43,7 @@ build/Release # Dependency directories node_modules/ +ignores/ jspm_packages/ # Snowpack dependency directory (https://snowpack.dev/) diff --git a/app.zip b/app.zip deleted file mode 100644 index 271b024..0000000 Binary files a/app.zip and /dev/null differ diff --git a/app/assets/utils/module/mailer.php b/app/assets/utils/module/mailer.php index 2fe4a12..03b1709 100644 --- a/app/assets/utils/module/mailer.php +++ b/app/assets/utils/module/mailer.php @@ -11,44 +11,36 @@ class mailer{ if( empty($_db["pdo1"]) ){ - exit(json_encode(["success"=>0, "message"=>"Error {$this->new_line}Developer need to define pdo1 - class ap"])); + exit(json_encode(["success"=>0, "message"=>"Error: Developer need to define pdo1 - class mailer"])); } - if( empty($_db["pdo2"]) ){ - - exit(json_encode(["success"=>0, "message"=>"Error {$this->new_line}Developer need to define pdo2 - class ap"])); - } - - $this->pdo1 = $_db["pdo1"]; - $this->pdo2 = $_db["pdo2"]; + $this->pdo1 = $_db["pdo1"]; } public function get_authen($input = array()){ - - - $revise = []; - $revise["company_id"] = $input["company_id"]; - if(count($input["smtp"]) > 0){ + $revise = []; + $revise["company_id"] = $input["company_id"]; + + // If SMTP config passed directly, use it (e.g. system default from config.php) + if( !empty($input["smtp"]) && count($input["smtp"]) > 0 ){ return $input["smtp"]; } - $sql = "SELECT * FROM smtp_setting WHERE company_id = :company_id "; - - $sth = $this->pdo2->prepare("$sql"); - $sth->execute([ - ":company_id" => $revise["company_id"] - ]); + // Otherwise read from company_smtp on pdo1 (wms) + $sth = $this->pdo1->prepare("SELECT * FROM company_smtp WHERE company_id = :company_id"); + $sth->execute([":company_id" => $revise["company_id"]]); $res = $sth->fetch(PDO::FETCH_ASSOC); if( empty($res) ){ $answer = []; $answer["success"] = 0; - $answer["message"] = "Error: You haven't set SMTP Server yet
----------
กรุณาตั้งค่า SMTP Server ก่อนส่ง Email"; + $answer["message"] = "Error: You haven't set SMTP Server yet
----------
กรุณาตั้งค่า SMTP Server ก่อนส่ง Email"; exit(json_encode($answer)); } + return $res; } @@ -62,7 +54,6 @@ class mailer{ $revise["require"] = $input["require"]; $revise["input"] = $input["input"]; - foreach ($revise["require"] as $key => $item) { if( !isset($revise["input"][$item]) ){ @@ -75,7 +66,7 @@ class mailer{ private function decrypt($input){ $this->check_required_fields([ - "class" => "trdb", + "class" => "mailer", "function" => "decrypt", "require" => ["key","data"], "input" => $input @@ -87,15 +78,13 @@ class mailer{ public function send_email($input = array()){ - $this->check_required_fields([ - "class" => "trdb", + "class" => "mailer", "function" => "send_email", "require" => ["subject","message","company_id"], "input" => $input ]); - - + $revise = []; $revise["subject"] = $input["subject"]; $revise["message"] = $input["message"]; @@ -106,73 +95,68 @@ class mailer{ $authen = $this->get_authen($input); - require_once dirname(__FILE__).'/phpmailer/vendor/autoload.php'; + require_once dirname(__FILE__).'/phpmailer/vendor/autoload.php'; - $mail = new PHPMailer(true); + $mail = new PHPMailer(true); - try { - //Server settings - $mail->SMTPDebug = false; - $mail->isSMTP(); - $mail->CharSet = "UTF-8"; - $mail->Host = $authen["server"]; - $mail->SMTPAuth = true; - $mail->Timeout = 20; - $mail->Username = $authen["username"]; - $mail->Password = $this->decrypt(["data"=>$authen["password"],"key"=>$revise["key"]]); + try { + $mail->SMTPDebug = false; + $mail->isSMTP(); + $mail->CharSet = "UTF-8"; + $mail->Host = $authen["server"]; + $mail->SMTPAuth = true; + $mail->Timeout = 20; + $mail->Username = $authen["username"]; + $mail->Password = $this->decrypt(["data"=>$authen["password"],"key"=>$revise["key"]]); - if ($authen["port"] === "465") { + $port = (string)$authen["port"]; + if ($port === "465") { $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; - } elseif ($authen["port"] === "587" || $authen["port"] === "25") { + } elseif ($port === "587" || $port === "25") { $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; } else { - $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // default fallback + $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; } - $mail->Port = $authen["port"]; + $mail->Port = $authen["port"]; - if(true){ - $mail->SMTPOptions = [ - 'socket' => ['bindto' => '0.0.0.0:0'] - ]; + $mail->SMTPOptions = [ + 'socket' => ['bindto' => '0.0.0.0:0'] + ]; + + // Sender + $from_email = !empty($authen["from_email"]) ? $authen["from_email"] : $authen["username"]; + $from_name = !empty($revise["channel_name"]) ? $revise["channel_name"] + : (!empty($authen["from_name"]) ? $authen["from_name"] : $authen["username"]); + $mail->setFrom($from_email, $from_name); + + $des = explode(",", $revise["to"]); + foreach ($des as $key => $value) { + $mail->addAddress(trim($value)); } - //Recipients - $mail->setFrom($authen["username"], $revise["channel_name"]); - - - $des = explode(",", $revise["to"]); - - foreach ($des as $key => $value) { - $mail->addAddress(trim($value)); - } - - //Content - $mail->isHTML(true); - $mail->Subject = $revise["subject"]; - $mail->Body = nl2br($revise["message"]); - $mail->AltBody = $revise["message"]; - - $mail->send(); - } - catch (phpmailerException $e) { + $mail->isHTML(true); + $mail->Subject = $revise["subject"]; + $mail->Body = nl2br($revise["message"]); + $mail->AltBody = $revise["message"]; + $mail->send(); + } + catch (phpmailerException $e) { $answer = []; $answer["success"] = 0; $answer["message"] = "Error1: ".nl2br($e->errorMessage()); exit(json_encode($answer)); } - catch (Exception $e) { - + catch (Exception $e) { $answer = []; $answer["success"] = 0; $answer["message"] = "Error2: ".nl2br($mail->ErrorInfo); exit(json_encode($answer)); - - } + } } } -?> +?> \ No newline at end of file diff --git a/app/dbconn.php b/app/dbconn.php index 470f01d..95515cd 100644 --- a/app/dbconn.php +++ b/app/dbconn.php @@ -31,8 +31,6 @@ class db_statement extends PDOStatement { // Perform logging here. PDO object is accessible // from $this->pdo. - echo $this->pdo->last_query(); - if (!is_array($args)) { $args = func_get_args(); }else{ diff --git a/app/include_setting_sidebar.php b/app/include_setting_sidebar.php index 68a9c0b..b04d504 100644 --- a/app/include_setting_sidebar.php +++ b/app/include_setting_sidebar.php @@ -3,7 +3,7 @@ ?> -