374 lines
12 KiB
Plaintext
374 lines
12 KiB
Plaintext
<?php
|
|
|
|
class Email_gateway extends MY_Controller
|
|
{
|
|
var $sender_std, $sender_keu, $sender_result;
|
|
|
|
function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->sender_std = [
|
|
"name" => "CpOne SAS",
|
|
"email" => "sascpone@gmail.com"
|
|
];
|
|
$this->sender_keu = [
|
|
"name" => "CpOne SAS",
|
|
"email" => "sascpone@gmail.com"
|
|
];
|
|
$this->sender_result = [
|
|
"name" => "CpOne SAS",
|
|
"email" => "sascpone@gmail.com"
|
|
];
|
|
}
|
|
//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
|
|
);
|
|
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='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 = [];
|
|
}
|
|
$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
|
|
);
|
|
|
|
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);
|
|
}
|
|
}
|