add api collect email notif register & gateway send email notif pemeriksaan
This commit is contained in:
290
application/controllers/cpone/SendEmailPreregister.php
Normal file
290
application/controllers/cpone/SendEmailPreregister.php
Normal file
@@ -0,0 +1,290 @@
|
||||
<?php
|
||||
class SendEmailPreregister extends MY_Controller
|
||||
{
|
||||
var $db;
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
// $this->db = $this->load->database("cpone", true);
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
echo "SEND EMAIL API";
|
||||
}
|
||||
|
||||
public function RegisterEmailNotif()
|
||||
{
|
||||
try {
|
||||
$data_send_patients = [];
|
||||
$data_patients = [];
|
||||
|
||||
# get list data preregist patient #
|
||||
$sql_patients = "SELECT
|
||||
Mcu_PreregisterPatientsPatientName AS nama_pasien,
|
||||
Mcu_PreregisterPatientsEmail AS email_pasien,
|
||||
Mcu_PreregisterPatientsT_OrderHeaderID AS orderid,
|
||||
T_OrderHeaderLabNumber AS ordernumber,
|
||||
Mcu_PreregisterDateCheckinSchedule AS tanggal_mcu,
|
||||
Mgm_McuStartDate,
|
||||
Mgm_McuEndDate
|
||||
FROM mcu_preregister_patients
|
||||
JOIN mgm_mcu ON Mgm_McuID = Mcu_PreregisterPatientsMgm_McuID
|
||||
AND Mgm_McuNotifByEmail = 'Y'
|
||||
AND Mcu_PreregisterPatientsT_OrderHeaderID != 0
|
||||
AND Mcu_PreregisterPatientsIsRegistered = 'Y'
|
||||
AND LENGTH(TRIM(Mcu_PreregisterPatientsEmail)) > 0
|
||||
JOIN t_orderheader ON T_OrderHeaderID = Mcu_PreregisterPatientsT_OrderHeaderID
|
||||
LEFT JOIN mcu_preregister_date ON Mcu_PreregisterDateIsActive = 'Y'
|
||||
AND Mcu_PreregisterDateMcu_PreregisterPatientsID = Mcu_PreregisterPatientsID
|
||||
WHERE Mcu_PreregisterPatientsIsActive = 'Y'
|
||||
AND (
|
||||
(Mcu_PreregisterDateCheckinSchedule IS NOT NULL
|
||||
AND Mcu_PreregisterDateCheckinSchedule != '0000-00-00'
|
||||
AND Mcu_PreregisterDateCheckinSchedule < CURDATE())
|
||||
OR
|
||||
((Mcu_PreregisterDateCheckinSchedule IS NULL
|
||||
OR Mcu_PreregisterDateCheckinSchedule = '0000-00-00')
|
||||
AND Mgm_McuStartDate <= CURDATE()
|
||||
AND CURDATE() < Mgm_McuEndDate
|
||||
)
|
||||
)";
|
||||
$que_patients = $this->db->query($sql_patients);
|
||||
if (!$que_patients) {
|
||||
throw new Exception('[Error] failed get data patients');
|
||||
}
|
||||
$data_patients = $que_patients->result_array();
|
||||
|
||||
# check patient test is done / not #
|
||||
foreach ($data_patients as $key => $patient) {
|
||||
$sql_sample_lab = "SELECT COUNT(*) AS is_sampling
|
||||
FROM (SELECT T_SampleTypeName AS sampletype_name,
|
||||
T_OrderSampleSampling AS is_sampling,
|
||||
T_OrderSampleReceive AS is_received
|
||||
FROM t_ordersample
|
||||
JOIN t_sampletype ON T_OrderSampleT_SampleTypeID = T_SampleTypeID
|
||||
LEFT JOIN m_user ON T_OrderSampleReceiveUserID = M_UserID
|
||||
LEFT JOIN m_staff ON M_UserM_StaffID = M_StaffID
|
||||
WHERE T_OrderSampleT_OrderHeaderID = ?
|
||||
AND T_OrderSampleIsActive = 'Y'
|
||||
Order BY T_SampleTypeName ASC
|
||||
) as subquery
|
||||
WHERE is_sampling = 'N'";
|
||||
$que_sample_lab = $this->db->query($sql_sample_lab, [$patient['orderid']]);
|
||||
if (!$que_sample_lab) {
|
||||
throw new Exception('[Error] failed get data sample lab');
|
||||
}
|
||||
$count_sample_lab = $que_sample_lab->row_array()['is_sampling'];
|
||||
|
||||
$sql_sample_radiodiagnostic = "SELECT COUNT(*) AS is_sampling
|
||||
FROM (SELECT T_TestName as sampletype_name,
|
||||
IF(ISNULL(T_SamplingSoFlag),'N','Y') as is_sampling,
|
||||
IF(ISNULL(T_SamplingSoFlag) OR T_SamplingSoFlag <> 'D','N','Y') as is_received
|
||||
FROM t_orderdetail
|
||||
JOIN t_orderheader ON T_OrderDetailT_OrderHeaderID = T_OrderHeaderID
|
||||
JOIN t_test ON T_OrderDetailT_TestID = T_TestID
|
||||
JOIN t_sampletype ON T_TestT_SampleTypeID = T_SampleTypeID
|
||||
JOIN t_bahan ON T_SampleTypeT_BahanID = T_BahanID
|
||||
JOIN t_samplestation ON T_BahanT_SampleStationID = T_SampleStationID
|
||||
AND T_SampleStationIsNonLab = 'RADIODIAGNOSTIC'
|
||||
LEFT JOIN t_samplingso ON T_SamplingSoT_OrderHeaderID = T_OrderDetailT_OrderHeaderID
|
||||
AND T_SamplingSoT_TestID = T_TestID
|
||||
LEFT JOIN m_user ON T_SamplingSoDoneUserID = M_UserID
|
||||
LEFT JOIN m_staff ON M_UserM_StaffID = M_StaffID
|
||||
WHERE T_OrderDetailT_OrderHeaderID = ?
|
||||
AND T_OrderDetailIsActive = 'Y'
|
||||
) as subquery
|
||||
WHERE is_sampling = 'N'";
|
||||
$que_sample_radiodiagnostic = $this->db->query($sql_sample_radiodiagnostic, [
|
||||
$patient['orderid']
|
||||
]);
|
||||
if (!$que_sample_lab) {
|
||||
throw new Exception('[Error] failed get data sample radiodiagnostik');
|
||||
}
|
||||
$count_sample_radiodiagnostic = $que_sample_radiodiagnostic->row_array()['is_sampling'];
|
||||
|
||||
$sql_sample_electromedic = "SELECT COUNT(*) AS is_sampling
|
||||
FROM (SELECT T_TestName as sampletype_name,
|
||||
IF(ISNULL(T_SamplingSoFlag),'N','Y') as is_sampling,
|
||||
IF(ISNULL(T_SamplingSoFlag) OR T_SamplingSoFlag <> 'D','N','Y') as is_received
|
||||
FROM t_orderdetail
|
||||
JOIN t_orderheader ON T_OrderDetailT_OrderHeaderID = T_OrderHeaderID
|
||||
JOIN t_test ON T_OrderDetailT_TestID = T_TestID
|
||||
JOIN t_sampletype ON T_TestT_SampleTypeID = T_SampleTypeID
|
||||
JOIN t_bahan ON T_SampleTypeT_BahanID = T_BahanID
|
||||
JOIN t_samplestation ON T_BahanT_SampleStationID = T_SampleStationID
|
||||
AND T_SampleStationIsNonLab = 'ELEKTROMEDIS'
|
||||
LEFT JOIN t_samplingso ON T_SamplingSoT_OrderHeaderID = T_OrderDetailT_OrderHeaderID
|
||||
AND T_SamplingSoT_TestID = T_TestID
|
||||
LEFT JOIN m_user ON T_SamplingSoDoneUserID = M_UserID
|
||||
LEFT JOIN m_staff ON M_UserM_StaffID = M_StaffID
|
||||
WHERE T_OrderDetailT_OrderHeaderID = ?
|
||||
AND T_OrderDetailIsActive = 'Y'
|
||||
) as subquery
|
||||
WHERE is_sampling = 'N'";
|
||||
$que_sample_electromedic = $this->db->query($sql_sample_electromedic, [
|
||||
$patient['orderid']
|
||||
]);
|
||||
$count_sample_electromedic = $que_sample_electromedic->row_array()['is_sampling'];
|
||||
|
||||
$sql_sample_other = "SELECT COUNT(*) AS is_sampling
|
||||
FROM (SELECT T_TestName as sampletype_name,
|
||||
IF(ISNULL(T_SamplingSoFlag),'N','Y') as is_sampling,
|
||||
IF(ISNULL(T_SamplingSoFlag) OR T_SamplingSoFlag <> 'D','N','Y') as is_received,
|
||||
IFNULL(M_StaffName,'') as staff_name
|
||||
FROM t_orderdetail
|
||||
JOIN t_orderheader ON T_OrderDetailT_OrderHeaderID = T_OrderHeaderID
|
||||
JOIN t_test ON T_OrderDetailT_TestID = T_TestID
|
||||
JOIN t_sampletype ON T_TestT_SampleTypeID = T_SampleTypeID
|
||||
JOIN t_bahan ON T_SampleTypeT_BahanID = T_BahanID
|
||||
JOIN t_samplestation ON T_BahanT_SampleStationID = T_SampleStationID
|
||||
AND T_SampleStationIsNonLab = 'OTHERS'
|
||||
LEFT JOIN t_samplingso ON T_SamplingSoT_OrderHeaderID = T_OrderDetailT_OrderHeaderID
|
||||
AND T_SamplingSoT_TestID = T_TestID
|
||||
LEFT JOIN m_user ON T_SamplingSoDoneUserID = M_UserID
|
||||
LEFT JOIN m_staff ON M_UserM_StaffID = M_StaffID
|
||||
WHERE T_OrderDetailT_OrderHeaderID = ?
|
||||
AND T_OrderDetailIsActive = 'Y'
|
||||
) as subquery
|
||||
WHERE is_sampling = 'N'";
|
||||
$que_sample_other = $this->db->query($sql_sample_other, [
|
||||
$patient['orderid']
|
||||
]);
|
||||
if (!$que_sample_other) {
|
||||
throw new Exception('[Error] failed to get data sample other');
|
||||
}
|
||||
$count_sample_other = $que_sample_other->row_array()['is_sampling'];
|
||||
|
||||
$total_is_sampling_n = array_sum(array(
|
||||
$count_sample_lab,
|
||||
$count_sample_electromedic,
|
||||
$count_sample_radiodiagnostic,
|
||||
$count_sample_other
|
||||
));
|
||||
if ($total_is_sampling_n > 0) {
|
||||
$data_send_patients[] = $patient;
|
||||
}
|
||||
}
|
||||
|
||||
# collect email #
|
||||
$this->db->trans_begin();
|
||||
foreach ($data_send_patients as $key => $patient) {
|
||||
// sementar pakai email config type R (result MCU)
|
||||
$sql_config_email = "SELECT * FROM m_emailconfig WHERE M_EmailConfigType = 'R'";
|
||||
$que_config_email = $this->db->query($sql_config_email, []);
|
||||
if (!$que_config_email) {
|
||||
$this->db->trans_rollback();
|
||||
throw new Exception('[Error] failed to get email ocnfig');
|
||||
}
|
||||
$emailconfig = $que_config_email->row_array();
|
||||
$tanggalMCURaw = (empty($patient['tanggal_mcu']) || $patient['tanggal_mcu'] === '0000-00-00')
|
||||
? $patient['Mgm_McuStartDate']
|
||||
: $patient['tanggal_mcu'];
|
||||
$tanggalMCUTs = strtotime($tanggalMCURaw);
|
||||
$hariIndonesia = [
|
||||
'Sunday' => 'Minggu',
|
||||
'Monday' => 'Senin',
|
||||
'Tuesday' => 'Selasa',
|
||||
'Wednesday' => 'Rabu',
|
||||
'Thursday' => 'Kamis',
|
||||
'Friday' => 'Jumat',
|
||||
'Saturday' => 'Sabtu'
|
||||
];
|
||||
$dayEn = date('l', $tanggalMCUTs);
|
||||
$dayId = isset($hariIndonesia[$dayEn]) ? $hariIndonesia[$dayEn] : $dayEn;
|
||||
$tanggalMCU = $dayId . ', ' . date('d-m-Y', $tanggalMCUTs);
|
||||
|
||||
$body = 'Yth ' . $patient["nama_pasien"] . ',
|
||||
<p>
|
||||
<p>Melalui email ini, kami ingin mengingatkan kembali jadwal pemeriksaan kesehatan (*Medical Check-Up*) Anda yang akan dilaksanakan pada:</p>
|
||||
<p>
|
||||
<strong>Hari/Tanggal : ' . $tanggalMCU . '</strong><br>
|
||||
<strong>Lokasi : Laboratorium Klinik <span style="font-weight:bold;color:blue">WESTERINDO</span></strong>
|
||||
</p>
|
||||
<p>
|
||||
</p>
|
||||
<p>Mohon kehadirannya tepat waktu dan pastikan Anda telah mengikuti instruksi persiapan (seperti puasa jika diperlukan) sebelum melakukan pemeriksaan.</p>
|
||||
<p>
|
||||
</p>
|
||||
<p>MOHON TIDAK DI-REPLY.</p>
|
||||
Hormat kami,
|
||||
<p>Laboratorium Klinik <span style="font-weight:bold;color:blue">WESTERINDO</span></p>';
|
||||
|
||||
$subject = "{$patient['ordernumber']} {$patient['nama_pasien']}";
|
||||
$recipient = json_encode([
|
||||
["name" => $patient['nama_pasien'], "email" => $patient['email_pasien']]
|
||||
]);
|
||||
$sendCC = json_encode([
|
||||
["name" => $emailconfig['M_EmailConfigCc'], "email" => $emailconfig['M_EmailConfigCc']]
|
||||
]);
|
||||
|
||||
$sql_cek_outbox = "SELECT PreregisterEmailOutboxID
|
||||
FROM preregister_email_outbox
|
||||
WHERE PreregisterEmailOutboxReffID = ?
|
||||
AND PreregisterEmailOutboxRecipientType = 'P'
|
||||
AND PreregisterEmailOutboxIsActive = 'Y'
|
||||
AND PreregisterEmailOutboxType = 'PREREGIST'";
|
||||
$que_cek_outbox = $this->db->query($sql_cek_outbox, [$patient['orderid']]);
|
||||
if (!$que_cek_outbox) {
|
||||
$this->db->trans_rollback();
|
||||
throw new Exception('[Error] failed check existing outbox data');
|
||||
}
|
||||
$cek_outbox = $que_cek_outbox->result_array();
|
||||
if (count($cek_outbox) == 0) {
|
||||
$sql_insert_outbox = "INSERT INTO preregister_email_outbox (
|
||||
PreregisterEmailOutboxSubject,
|
||||
PreregisterEmailOutboxRecipient,
|
||||
PreregisterEmailOutboxRecipientType,
|
||||
PreregisterEmailOutboxCC,
|
||||
PreregisterEmailOutboxIsHtml,
|
||||
PreregisterEmailOutboxBody,
|
||||
PreregisterEmailOutboxType,
|
||||
PreregisterEmailOutboxReffID
|
||||
) VALUES (?,?,'P',?,'Y',?,?,?)";
|
||||
$que_insert_outbox = $this->db->query($sql_insert_outbox, [
|
||||
$subject,
|
||||
$recipient,
|
||||
$sendCC,
|
||||
$body,
|
||||
'PREREGIST',
|
||||
$patient['orderid']
|
||||
]);
|
||||
if (!$que_insert_outbox) {
|
||||
$this->db->trans_rollback();
|
||||
throw new Exception('[Error] failed insert email outbox');
|
||||
}
|
||||
} else {
|
||||
$sql_update_outbox = "UPDATE preregister_email_outbox SET
|
||||
PreregisterEmailOutboxSubject = ?,
|
||||
PreregisterEmailOutboxRecipient = ?,
|
||||
PreregisterEmailOutboxCC = ?,
|
||||
PreregisterEmailOutboxBody = ?,
|
||||
PreregisterEmailOutboxIsSent = 'R',
|
||||
PreregisterEmailOutboxRetry = 0,
|
||||
PreregisterEmailOutboxLastUpdated = NOW()
|
||||
WHERE PreregisterEmailOutboxID = ?";
|
||||
$que_update_outbox = $this->db->query($sql_update_outbox, [
|
||||
$subject,
|
||||
$recipient,
|
||||
$sendCC,
|
||||
$body,
|
||||
$cek_outbox[0]['PreregisterEmailOutboxID']
|
||||
]);
|
||||
if (!$que_update_outbox) {
|
||||
$this->db->trans_rollback();
|
||||
throw new Exception('[Error] failed update outbox');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->db->trans_commit();
|
||||
|
||||
$total_email_sent = count($data_send_patients);
|
||||
$this->sys_ok($total_email_sent);
|
||||
} catch (Exception $e) {
|
||||
$msg = $e->getMessage();
|
||||
$this->sys_error_db($msg);
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,414 +2,556 @@
|
||||
|
||||
class Email_gateway extends MY_Controller
|
||||
{
|
||||
var $sender_std, $sender_keu, $sender_result, $password_result, $password_keu, $password_std;
|
||||
var $sender_std, $sender_keu, $sender_result, $password_result, $password_keu, $password_std;
|
||||
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$sql = "SELECT ConfOnHoldSenderEmail as email,
|
||||
$sql = "SELECT ConfOnHoldSenderEmail as email,
|
||||
ConfOnHoldSenderName as name,
|
||||
ConfOnHoldSenderPassword as password
|
||||
FROM conf_on_hold
|
||||
WHERE ConfOnHoldIsActive = 'Y';";
|
||||
$qry = $this->db->query($sql);
|
||||
$qry = $this->db->query($sql);
|
||||
|
||||
$keu = $qry->row_array();
|
||||
$this->password_keu = $keu['password'];
|
||||
unset($keu['password']);
|
||||
$this->sender_keu = $keu;
|
||||
$keu = $qry->row_array();
|
||||
$this->password_keu = $keu['password'];
|
||||
unset($keu['password']);
|
||||
$this->sender_keu = $keu;
|
||||
|
||||
$sql = "SELECT M_EmailConfigSender as email,
|
||||
$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);
|
||||
$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";
|
||||
$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 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;
|
||||
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";
|
||||
}
|
||||
}
|
||||
}
|
||||
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
|
||||
|
||||
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
|
||||
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
|
||||
$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;
|
||||
$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);
|
||||
}
|
||||
}
|
||||
}
|
||||
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
|
||||
|
||||
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
|
||||
and XEmailOutboxIsActive='Y' and XEmailOutboxIsSent='N'
|
||||
union
|
||||
select * from x_email_outbox
|
||||
where XEmailOutboxType='KEU'
|
||||
and XEmailOutboxIsActive='Y' and XEmailOutboxIsSent='E'
|
||||
and XEmailOutboxRetry < 5
|
||||
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();
|
||||
$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
|
||||
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;
|
||||
$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);
|
||||
}
|
||||
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
|
||||
|
||||
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
|
||||
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
|
||||
and XEmailOutboxRetry < 5
|
||||
union
|
||||
select * from x_email_outbox
|
||||
where XEmailOutboxType='RESULT'
|
||||
and XEmailOutboxIsActive='Y' and XEmailOutboxIsSent='E'
|
||||
and XEmailOutboxRetry < 5
|
||||
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();
|
||||
$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
|
||||
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
|
||||
);
|
||||
$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"]);
|
||||
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 preregister()
|
||||
{
|
||||
$log_type = "preregister";
|
||||
$wait_in_minute = 30;
|
||||
$delay = 3;
|
||||
$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 preregister_email_outbox
|
||||
WHERE PreregisterEmailOutboxType = 'PREREGIST'
|
||||
AND PreregisterEmailOutboxIsSent = 'N'
|
||||
AND PreregisterEmailOutboxIsActive = 'Y'
|
||||
UNION
|
||||
SELECT * FROM preregister_email_outbox
|
||||
WHERE PreregisterEmailOutboxType = 'PREREGIST'
|
||||
AND PreregisterEmailOutboxIsSent = 'R'
|
||||
AND PreregisterEmailOutboxIsActive = 'Y'
|
||||
UNION
|
||||
SELECT * FROM preregister_email_outbox
|
||||
WHERE PreregisterEmailOutboxType = 'PREREGIST'
|
||||
AND PreregisterEmailOutboxIsSent = 'E'
|
||||
AND PreregisterEmailOutboxIsActive = 'Y'
|
||||
ORDER BY PreregisterEmailOutboxIsSent DESC, PreregisterEmailOutboxRetry DESC
|
||||
LIMIT 0, $max_per_batch";
|
||||
$que = $this->db->query($sql);
|
||||
if (!$que) {
|
||||
$this->log($log_type, "Error query Get " . $this->db->error()["message"]);
|
||||
unlink($tmp_mark_file);
|
||||
exit;
|
||||
}
|
||||
$rows = $que->result_array();
|
||||
|
||||
if (count($rows) == 0) {
|
||||
$this->log($log_type, "no pending email");
|
||||
unlink($tmp_mark_file);
|
||||
exit;
|
||||
}
|
||||
|
||||
$sql_update = "UPDATE preregister_email_outbox SET
|
||||
PreregisterEmailOutboxIsSent = ?,
|
||||
PreregisterEmailOutboxRetry = PreregisterEmailOutboxRetry + 1,
|
||||
PreregisterEmailOutboxSentDate = NOW()
|
||||
WHERE PreregisterEmailOutboxID = ?";
|
||||
$sender = $this->sender_result;
|
||||
foreach ($rows as $obj) {
|
||||
$recipients = json_decode($obj['PreregisterEmailOutboxRecipient'], true);
|
||||
if (json_last_error() != JSON_ERROR_NONE) {
|
||||
$this->log($log_type, "Error Recipients format {$obj['PreregisterEmailOutboxRecipient']}");
|
||||
continue;
|
||||
}
|
||||
$cc = json_decode($obj['PreregisterEmailOutboxCC'], true);
|
||||
if (json_last_error() != JSON_ERROR_NONE) {
|
||||
$this->log($log_type, "Error CC format {$obj['PreregisterEmailOutboxCC']}");
|
||||
$cc = [];
|
||||
}
|
||||
|
||||
$new_recipients = [];
|
||||
foreach ($recipients as $rec) {
|
||||
if (strpos($rec['email'], ",") > 0) {
|
||||
$rec_email = explode(",", $rec['email']);
|
||||
foreach ($rec_email as $email) {
|
||||
$new_recipients[] = ["name" => $rec['name'], "email" => $email];
|
||||
}
|
||||
} else {
|
||||
$new_recipients[] = $rec;
|
||||
}
|
||||
}
|
||||
$recipients = $new_recipients;
|
||||
$arr_recs = array_map(function ($r) {
|
||||
return $r['email'];
|
||||
}, $recipients);
|
||||
$recs = implode(", ", $arr_recs);
|
||||
|
||||
$atts = json_decode($obj['PreregisterEmailOutboxAttachment'], true);
|
||||
if (json_last_error() != JSON_ERROR_NONE) {
|
||||
$this->log($log_type, "Error attachment {$obj['PreregisterEmailOutboxAttachment']}");
|
||||
$atts = [];
|
||||
}
|
||||
|
||||
list($status, $message) = $this->gmail->send(
|
||||
$sender,
|
||||
$obj['PreregisterEmailOutboxSubject'],
|
||||
$obj['PreregisterEmailOutboxBody'],
|
||||
$recipients,
|
||||
$cc,
|
||||
$obj['PreregisterEmailOutboxIsHtml'],
|
||||
$atts,
|
||||
$this->sender_result['email'],
|
||||
$this->password_result
|
||||
);
|
||||
|
||||
if ($status) {
|
||||
$this->log($log_type, "Email {$obj['PreregisterEmailOutboxSubject']} sent to {$recs} [OK]");
|
||||
$que_update = $this->db->query($sql_update, ['Y', $obj['PreregisterEmailOutboxID']]);
|
||||
} else {
|
||||
$this->log($log_type, "Email {$obj['PreregisterEmailOutboxSubject']} sent to {$recs} [ERR]\n {$message}");
|
||||
$que_update = $this->db->query($sql_update, ['E', $obj['PreregisterEmailOutboxID']]);
|
||||
}
|
||||
if (!$que_update) {
|
||||
$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);
|
||||
exit;
|
||||
}
|
||||
$this->log($log_type, "Wait {$delay}s");
|
||||
sleep($delay);
|
||||
}
|
||||
unlink($tmp_mark_file);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user