Initial import
This commit is contained in:
415
application/controllers/tools/Email_gateway.php
Normal file
415
application/controllers/tools/Email_gateway.php
Normal file
@@ -0,0 +1,415 @@
|
||||
<?php
|
||||
|
||||
class Email_gateway extends MY_Controller
|
||||
{
|
||||
var $sender_std, $sender_keu, $sender_result, $password_result, $password_keu, $password_std;
|
||||
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$sql = "SELECT ConfOnHoldSenderEmail as email,
|
||||
ConfOnHoldSenderName as name,
|
||||
ConfOnHoldSenderPassword as password
|
||||
FROM conf_on_hold
|
||||
WHERE ConfOnHoldIsActive = 'Y';";
|
||||
$qry = $this->db->query($sql);
|
||||
|
||||
$keu = $qry->row_array();
|
||||
$this->password_keu = $keu['password'];
|
||||
unset($keu['password']);
|
||||
$this->sender_keu = $keu;
|
||||
|
||||
$sql = "SELECT M_EmailConfigSender as email,
|
||||
M_EmailConfigUsername as name,
|
||||
M_EmailConfigPassword as password
|
||||
FROM m_emailconfig
|
||||
WHERE M_EmailConfigIsActive = 'Y';";
|
||||
$qry = $this->db->query($sql);
|
||||
|
||||
$rst = $qry->row_array();
|
||||
$this->password_result = $rst['password'];
|
||||
unset($rst['password']);
|
||||
$this->sender_result = $rst;
|
||||
$this->sender_std = $rst;
|
||||
// print_r($this->sender_result);
|
||||
}
|
||||
function coba()
|
||||
{
|
||||
print_r($this->sender_result);
|
||||
print_r($this->sender_keu);
|
||||
}
|
||||
//Email type standard
|
||||
function log($type, $message)
|
||||
{
|
||||
$fname = "/xtmp/x-email-$type.log";
|
||||
$dt = date("Y-m-d H:i:s");
|
||||
echo "$dt $message\n";
|
||||
$st = file_put_contents($fname, "$dt $message\n", FILE_APPEND);
|
||||
if ($st === false) {
|
||||
echo "$dt Error writing to log file $fname\n";
|
||||
}
|
||||
}
|
||||
function standard()
|
||||
{
|
||||
$wait_in_minute = 30; //staled process wait till 30 minute
|
||||
$delay = 3; // email delay 3s
|
||||
$max_per_batch = 20;
|
||||
$log_type = "std";
|
||||
$tmp_mark_file = "/xtmp/standard-file.json";
|
||||
$this->load->library("Gmail");
|
||||
$is_running = false;
|
||||
$last_pid = "---";
|
||||
if (file_exists($tmp_mark_file)) {
|
||||
$j_mark = file_get_contents($tmp_mark_file);
|
||||
$mark = json_decode($j_mark, true);
|
||||
$last_run = $mark["time"];
|
||||
$last_pid = $mark["pid"];
|
||||
if ($last_run != "") {
|
||||
$x_now = date("Y-m-d H:i:s");
|
||||
$last_run_stale = date("Y-m-d H:i:s", strtotime($j_mark . " + $wait_in_minute minute"));
|
||||
if ($last_run_stale > $x_now) {
|
||||
$is_running = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($is_running) {
|
||||
$this->log($log_type, "Process Already Running : $last_pid");
|
||||
exit;
|
||||
}
|
||||
$xpid = getmygid();
|
||||
if ($xpid === false) {
|
||||
$xpid = "-1"; //
|
||||
}
|
||||
file_put_contents($tmp_mark_file, json_encode([
|
||||
"time" => date("Y-m-d H:i:s"),
|
||||
"pid" => $xpid
|
||||
]));
|
||||
$sql = "select * from x_email_outbox where XEmailOutboxType='STD'
|
||||
and XEmailOutboxIsActive='Y' and XEmailOutboxIsSent='N'
|
||||
union
|
||||
select * from x_email_outbox where XEmailOutboxType='STD'
|
||||
and XEmailOutboxIsActive='Y' and XEmailOutboxIsSent='E'
|
||||
and XEmailOutboxRetry < 5
|
||||
order by XEmailOutboxIsSent desc,XEmailOutboxRetry desc
|
||||
limit 0,$max_per_batch";
|
||||
|
||||
$qry = $this->db->query($sql);
|
||||
if (!$qry) {
|
||||
$this->log($log_type, "Error query Get " . $this->db->error()["message"]);
|
||||
unlink($tmp_mark_file);
|
||||
exit;
|
||||
}
|
||||
$rows = $qry->result_array();
|
||||
if (count($rows) == 0) {
|
||||
$this->log($log_type, "No Pending Email");
|
||||
unlink($tmp_mark_file);
|
||||
exit;
|
||||
}
|
||||
$sql_u = "update x_email_outbox set XEmailOutboxIsSent=?, XEmailOutboxRetry = XEmailOutboxRetry +1
|
||||
, XEmailOutboxSentDate = now()
|
||||
where XEmailOutboxID =?";
|
||||
$sender = $this->sender_std;
|
||||
foreach ($rows as $r) {
|
||||
//$sender = json_decode($r["XEmailOutboxSender"], true);
|
||||
// if (json_last_error() != JSON_ERROR_NONE) {
|
||||
// $this->log($log_type, "Error Sender format {$r["XEmailOutboxSender"]}");
|
||||
// }
|
||||
$recipients = json_decode($r["XEmailOutboxRecipients"], true);
|
||||
if (json_last_error() != JSON_ERROR_NONE) {
|
||||
$this->log($log_type, "Error Recipients format {$r["XEmailOutboxSender"]}");
|
||||
continue;
|
||||
}
|
||||
$cc = [];
|
||||
$jcc = json_decode($r["XEmailOutboxCc"], true);
|
||||
if (json_last_error() != JSON_ERROR_NONE) {
|
||||
$cc = $jcc;
|
||||
}
|
||||
$arr_recs = array_map(function ($r) {
|
||||
return $r["email"];
|
||||
}, $recipients);
|
||||
$recs = implode(", ", $arr_recs);
|
||||
$atts = json_decode($r["XEmailOutboxAttachments"], true);
|
||||
if (json_last_error() != JSON_ERROR_NONE) {
|
||||
$atts = [];
|
||||
}
|
||||
list($status, $message) = $this->gmail->send(
|
||||
$sender,
|
||||
$r["XEmailOutboxSubject"],
|
||||
$r["XEmailOutboxBody"],
|
||||
$recipients,
|
||||
$cc,
|
||||
$r["XEmailOutboxIsHtml"],
|
||||
$atts
|
||||
);
|
||||
if ($status) {
|
||||
$this->log($log_type, "Email {$r["XEmailOutboxSubject"]} sent to {$recs} [OK]");
|
||||
$qry_u = $this->db->query($sql_u, ["Y", $r["XEmailOutboxID"]]);
|
||||
} else {
|
||||
$this->log($log_type, "Email {$r["XEmailOutboxSubject"]} sent to {$recs} [ERR]\n $message");
|
||||
$qry_u = $this->db->query($sql_u, ["E", $r["XEmailOutboxID"]]);
|
||||
}
|
||||
if (!$qry_u) {
|
||||
$this->log($log_type, "Error query Update" . $this->db->error()["message"]);
|
||||
unlink($tmp_mark_file);
|
||||
exit;
|
||||
}
|
||||
$this->log($log_type, "Wait {$delay}s");
|
||||
sleep($delay);
|
||||
}
|
||||
}
|
||||
|
||||
function keu()
|
||||
{
|
||||
$log_type = "keu";
|
||||
$wait_in_minute = 30; //staled process wait till 30 minute
|
||||
$delay = 3; // email delay 3s
|
||||
$max_per_batch = 20;
|
||||
|
||||
$this->log($log_type, "Start Process Keu Email");
|
||||
$tmp_mark_file = "/xtmp/{$log_type}-file.json";
|
||||
$this->load->library("Gmail");
|
||||
$is_running = false;
|
||||
$last_pid = "---";
|
||||
if (file_exists($tmp_mark_file)) {
|
||||
$j_mark = file_get_contents($tmp_mark_file);
|
||||
$mark = json_decode($j_mark, true);
|
||||
$last_run = $mark["time"];
|
||||
$last_pid = $mark["pid"];
|
||||
if ($last_run != "") {
|
||||
$x_now = date("Y-m-d H:i:s");
|
||||
$last_run_stale = date("Y-m-d H:i:s", strtotime($last_run . " + $wait_in_minute minutes"));
|
||||
if ($last_run_stale > $x_now) {
|
||||
$is_running = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($is_running) {
|
||||
$this->log($log_type, "Process Already Running : $last_pid @ $last_run");
|
||||
exit;
|
||||
}
|
||||
$xpid = getmygid();
|
||||
if ($xpid === false) {
|
||||
$xpid = "-1"; //
|
||||
}
|
||||
file_put_contents($tmp_mark_file, json_encode([
|
||||
"time" => date("Y-m-d H:i:s"),
|
||||
"pid" => $xpid
|
||||
]));
|
||||
$sql = "select * from x_email_outbox
|
||||
where XEmailOutboxType='KEU'
|
||||
and XEmailOutboxIsActive='Y' and XEmailOutboxIsSent='N'
|
||||
union
|
||||
select * from x_email_outbox
|
||||
where XEmailOutboxType='KEU'
|
||||
and XEmailOutboxIsActive='Y' and XEmailOutboxIsSent='E'
|
||||
and XEmailOutboxRetry < 5
|
||||
order by XEmailOutboxIsSent desc,XEmailOutboxRetry desc
|
||||
limit 0,$max_per_batch";
|
||||
|
||||
$qry = $this->db->query($sql);
|
||||
if (!$qry) {
|
||||
$this->log($log_type, "Error query Get " . $this->db->error()["message"]);
|
||||
unlink($tmp_mark_file);
|
||||
exit;
|
||||
}
|
||||
$rows = $qry->result_array();
|
||||
|
||||
if (count($rows) == 0) {
|
||||
$this->log($log_type, "No Pending Email");
|
||||
unlink($tmp_mark_file);
|
||||
exit;
|
||||
}
|
||||
$sql_u = "update x_email_outbox set XEmailOutboxIsSent=?, XEmailOutboxRetry = XEmailOutboxRetry +1
|
||||
, XEmailOutboxSentDate = now()
|
||||
where XEmailOutboxID =?";
|
||||
|
||||
|
||||
$sender = $this->sender_keu;
|
||||
foreach ($rows as $r) {
|
||||
//$sender = json_decode($r["XEmailOutboxSender"], true);
|
||||
// if (json_last_error() != JSON_ERROR_NONE) {
|
||||
// $this->log($log_type, "Error Sender format {$r["XEmailOutboxSender"]}");
|
||||
// }
|
||||
$recipients = json_decode($r["XEmailOutboxRecipients"], true);
|
||||
if (json_last_error() != JSON_ERROR_NONE) {
|
||||
$this->log($log_type, "Error Recipients format {$r["XEmailOutboxRecipients"]}");
|
||||
$this->log($log_type, "Debug : " . print_r($r, true));
|
||||
continue;
|
||||
}
|
||||
$cc = [];
|
||||
$jcc = json_decode($r["XEmailOutboxCc"], true);
|
||||
if (json_last_error() != JSON_ERROR_NONE) {
|
||||
$cc = $jcc;
|
||||
}
|
||||
$arr_recs = array_map(function ($r) {
|
||||
return $r["email"];
|
||||
}, $recipients);
|
||||
$recs = implode(", ", $arr_recs);
|
||||
$atts = json_decode($r["XEmailOutboxAttachments"], true);
|
||||
if (json_last_error() != JSON_ERROR_NONE) {
|
||||
$atts = [];
|
||||
}
|
||||
list($status, $message) = $this->gmail->send(
|
||||
$sender,
|
||||
$r["XEmailOutboxSubject"],
|
||||
$r["XEmailOutboxBody"],
|
||||
$recipients,
|
||||
$cc,
|
||||
$r["XEmailOutboxIsHtml"],
|
||||
$atts,
|
||||
$this->sender_keu['email'],
|
||||
$this->password_keu
|
||||
);
|
||||
if ($status) {
|
||||
$this->log($log_type, "Email {$r["XEmailOutboxSubject"]} sent to {$recs} [OK]");
|
||||
$qry_u = $this->db->query($sql_u, ["Y", $r["XEmailOutboxID"]]);
|
||||
} else {
|
||||
$this->log($log_type, "Email {$r["XEmailOutboxSubject"]} sent to {$recs} [ERR]\n $message");
|
||||
$qry_u = $this->db->query($sql_u, ["E", $r["XEmailOutboxID"]]);
|
||||
}
|
||||
if (!$qry_u) {
|
||||
$this->log($log_type, "Error query Update" . $this->db->error()["message"]);
|
||||
unlink($tmp_mark_file);
|
||||
exit;
|
||||
}
|
||||
$this->log($log_type, "Wait {$delay}s");
|
||||
sleep($delay);
|
||||
}
|
||||
unlink($tmp_mark_file);
|
||||
}
|
||||
|
||||
function result()
|
||||
{
|
||||
$log_type = "result";
|
||||
$wait_in_minute = 30; //staled process wait till 30 minute
|
||||
$delay = 3; // email delay 3s
|
||||
$max_per_batch = 20;
|
||||
|
||||
$this->log($log_type, "Start Process $log_type Email");
|
||||
$tmp_mark_file = "/xtmp/{$log_type}-file.json";
|
||||
$this->load->library("Gmail");
|
||||
$is_running = false;
|
||||
$last_pid = "---";
|
||||
if (file_exists($tmp_mark_file)) {
|
||||
$j_mark = file_get_contents($tmp_mark_file);
|
||||
$mark = json_decode($j_mark, true);
|
||||
$last_run = $mark["time"];
|
||||
$last_pid = $mark["pid"];
|
||||
if ($last_run != "") {
|
||||
$x_now = date("Y-m-d H:i:s");
|
||||
$last_run_stale = date("Y-m-d H:i:s", strtotime($last_run . " + $wait_in_minute minutes"));
|
||||
if ($last_run_stale > $x_now) {
|
||||
$is_running = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($is_running) {
|
||||
$this->log($log_type, "Process Already Running : $last_pid @ $last_run");
|
||||
exit;
|
||||
}
|
||||
$xpid = getmygid();
|
||||
if ($xpid === false) {
|
||||
$xpid = "-1"; //
|
||||
}
|
||||
file_put_contents($tmp_mark_file, json_encode([
|
||||
"time" => date("Y-m-d H:i:s"),
|
||||
"pid" => $xpid
|
||||
]));
|
||||
$sql = "select * from x_email_outbox
|
||||
where XEmailOutboxType='RESULT'
|
||||
and XEmailOutboxIsActive='Y' and XEmailOutboxIsSent='N'
|
||||
union
|
||||
select * from x_email_outbox
|
||||
where XEmailOutboxType='RESULT'
|
||||
and XEmailOutboxIsActive='Y' and XEmailOutboxIsSent='R'
|
||||
and XEmailOutboxRetry < 5
|
||||
union
|
||||
select * from x_email_outbox
|
||||
where XEmailOutboxType='RESULT'
|
||||
and XEmailOutboxIsActive='Y' and XEmailOutboxIsSent='E'
|
||||
and XEmailOutboxRetry < 5
|
||||
order by XEmailOutboxIsSent desc,XEmailOutboxRetry desc
|
||||
limit 0,$max_per_batch";
|
||||
|
||||
$qry = $this->db->query($sql);
|
||||
if (!$qry) {
|
||||
$this->log($log_type, "Error query Get " . $this->db->error()["message"]);
|
||||
unlink($tmp_mark_file);
|
||||
exit;
|
||||
}
|
||||
$rows = $qry->result_array();
|
||||
|
||||
if (count($rows) == 0) {
|
||||
$this->log($log_type, "No Pending Email");
|
||||
unlink($tmp_mark_file);
|
||||
exit;
|
||||
}
|
||||
$sql_u = "update x_email_outbox set XEmailOutboxIsSent=?, XEmailOutboxRetry = XEmailOutboxRetry +1
|
||||
, XEmailOutboxSentDate = now()
|
||||
where XEmailOutboxID =?";
|
||||
$sender = $this->sender_result;
|
||||
foreach ($rows as $r) {
|
||||
$recipients = json_decode($r["XEmailOutboxRecipients"], true);
|
||||
if (json_last_error() != JSON_ERROR_NONE) {
|
||||
$this->log($log_type, "Error Recipients format {$r["XEmailOutboxRecipients"]}");
|
||||
continue;
|
||||
}
|
||||
$cc = json_decode($r["XEmailOutboxCc"], true);
|
||||
if (json_last_error() != JSON_ERROR_NONE) {
|
||||
$this->log($log_type, "Error Cc format {$r["XEmailOutbox"]}");
|
||||
$cc = [];
|
||||
}
|
||||
//fix email
|
||||
$new_recipients = [];
|
||||
foreach ($recipients as $r01) {
|
||||
if (strpos($r01["email"], ",") > 0) {
|
||||
$a_email = explode(",", $r01["email"]);
|
||||
foreach ($a_email as $email) {
|
||||
$new_recipients[] = ["name" => $r01["name"], "email" => $email];
|
||||
}
|
||||
} else {
|
||||
$new_recipients[] = $r01;
|
||||
}
|
||||
}
|
||||
$recipients = $new_recipients;
|
||||
$arr_recs = array_map(function ($r) {
|
||||
return $r["email"];
|
||||
}, $recipients);
|
||||
$recs = implode(", ", $arr_recs);
|
||||
$atts = json_decode($r["XEmailOutboxAttachment"], true);
|
||||
if (json_last_error() != JSON_ERROR_NONE) {
|
||||
$this->log($log_type, "Error Attachments {$r["XEmailOutboxAttachment"]}");
|
||||
$atts = [];
|
||||
}
|
||||
list($status, $message) = $this->gmail->send(
|
||||
$sender,
|
||||
$r["XEmailOutboxSubject"],
|
||||
$r["XEmailOutboxBody"],
|
||||
$recipients,
|
||||
$cc,
|
||||
$r["XEmailOutboxIsHtml"],
|
||||
$atts,
|
||||
$this->sender_result['email'],
|
||||
$this->password_result,
|
||||
false
|
||||
);
|
||||
|
||||
if ($status) {
|
||||
$this->log($log_type, "Email {$r["XEmailOutboxSubject"]} sent to {$recs} [OK]");
|
||||
$qry_u = $this->db->query($sql_u, ["Y", $r["XEmailOutboxID"]]);
|
||||
} else {
|
||||
$this->log($log_type, "Email {$r["XEmailOutboxSubject"]} sent to {$recs} [ERR]\n $message");
|
||||
$qry_u = $this->db->query($sql_u, ["E", $r["XEmailOutboxID"]]);
|
||||
}
|
||||
if (!$qry_u) {
|
||||
$this->log($log_type, "Error query Update" . $this->db->error()["message"]);
|
||||
unlink($tmp_mark_file);
|
||||
exit;
|
||||
}
|
||||
$this->log($log_type, "Wait {$delay}s");
|
||||
sleep($delay);
|
||||
}
|
||||
unlink($tmp_mark_file);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user