Login , Register ,and onboarding
This commit is contained in:
@@ -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"] = "<b>Error: </b> You haven't set SMTP Server yet<br>----------<br>กรุณาตั้งค่า SMTP Server ก่อนส่ง Email";
|
||||
$answer["message"] = "<b>Error:</b> You haven't set SMTP Server yet<br>----------<br>กรุณาตั้งค่า 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"] = "<b>Error1</b>: ".nl2br($e->errorMessage());
|
||||
exit(json_encode($answer));
|
||||
}
|
||||
catch (Exception $e) {
|
||||
|
||||
catch (Exception $e) {
|
||||
$answer = [];
|
||||
$answer["success"] = 0;
|
||||
$answer["message"] = "<b>Error2</b>: ".nl2br($mail->ErrorInfo);
|
||||
exit(json_encode($answer));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
?>
|
||||
Reference in New Issue
Block a user