Update Perbaikan Email Ganda Saat Terkirim

This commit is contained in:
mikael-zakaria 2026-04-09 09:15:04 +07:00
parent 774520c7f2
commit f67960311b

View File

@ -1348,15 +1348,8 @@ class Activities extends Controller {
if ($this->request->getMethod() === 'POST') { if ($this->request->getMethod() === 'POST') {
// $check = $db->query("SELECT sendmail FROM activities WHERE actid='$actid'")->getRow(); // // $email = \Config\Services::email();
// if ($check && $check->sendmail == '1') {
// return redirect()->to('activities/')->with('error', 'Email Sudah Pernah Dikirim');
// }
// $email = \Config\Services::email();
$sql = "update activities set sendmail='1' where actid='$actid'";
$query = $db->query($sql);
$replyto = $this->request->getVar('replyto'); $replyto = $this->request->getVar('replyto');
$to = $this->request->getVar('to'); $to = $this->request->getVar('to');
$cc = $this->request->getVar('cc'); $cc = $this->request->getVar('cc');
@ -1364,43 +1357,47 @@ class Activities extends Controller {
$subject = $this->request->getVar('subject'); $subject = $this->request->getVar('subject');
$message = $this->request->getVar('message'); $message = $this->request->getVar('message');
$attachment = $this->request->getVar('attachment'); $attachment = $this->request->getVar('attachment');
// $attachments = explode(',',$attachment); // // $attachments = explode(',',$attachment);
/* // /*
$email->setFrom('noreply@services.summit.co.id', 'SUMMIT CRM'); // $email->setFrom('noreply@services.summit.co.id', 'SUMMIT CRM');
$email->setReplyTo($replyto); // $email->setReplyTo($replyto);
$email->setTo(cleanmail($to)); // $email->setTo(cleanmail($to));
if($cc != '') { $email->setCC(cleanmail($cc)); } // if($cc != '') { $email->setCC(cleanmail($cc)); }
if($bcc != '') { $email->setBCC(cleanmail($bcc)); } // if($bcc != '') { $email->setBCC(cleanmail($bcc)); }
$email->setSubject($subject); // $email->setSubject($subject);
$email->setMessage($message); // $email->setMessage($message);
foreach($attachments as $attachment) { // foreach($attachments as $attachment) {
if($attachment != '') { // if($attachment != '') {
$attachment = FCPATH."/upload/".$attachment; // $attachment = FCPATH."/upload/".$attachment;
$email->attach($attachment); // $email->attach($attachment);
} // }
} // }
//$email->set_newline("\r\n"); // //$email->set_newline("\r\n");
//$email->send(); // //$email->send();
# debugging email CI4 // # debugging email CI4
//$email->send(FALSE); // //$email->send(FALSE);
$myfile = fopen("log.txt", "a+") or die("Unable to open file!"); // $myfile = fopen("log.txt", "a+") or die("Unable to open file!");
if ( $email->send(FALSE) ) { // if ( $email->send(FALSE) ) {
fwrite($myfile, "\nemail sent success"); // fwrite($myfile, "\nemail sent success");
} else { // } else {
fwrite($myfile, "\n".$email->printDebugger() ); // fwrite($myfile, "\n".$email->printDebugger() );
} // }
fclose($myfile); // fclose($myfile);
*/ // */
$tos = join(",",$to); $tos = join(",",$to);
if(isset($cc)) { $ccs = join(",",$cc); } else { $ccs = ''; } if(isset($cc)) { $ccs = join(",",$cc); } else { $ccs = ''; }
if(isset($bcc)) { $bccs = join(",",$bcc); } else { $bccs = ''; } if(isset($bcc)) { $bccs = join(",",$bcc); } else { $bccs = ''; }
$sql = "INSERT into actsend_log (actid, replyto, emailto, emailcc, emailbcc, logdate) values ( '$actid', '$replyto', '$tos', '$ccs', '$bccs', NOW() )";
$query = $db->query($sql);
// Untuk Simpan Email Otomatis // Khusus Agar Pengirim Email Tidak Masuk daftar CC
$tosArray = array_map('trim', explode(',', $replyto));
$tos = implode(',', array_diff(array_map('trim', explode(',', $tos)), $tosArray));
$ccs = implode(',', array_diff(array_map('trim', explode(',', $ccs)), $tosArray));
// Untuk Simpan Email Baru Otomatis Saat Belum Ada di Database
$this->syncEmails($tos, $ccs, $bccs); $this->syncEmails($tos, $ccs, $bccs);
// Siapkan Data
$data = [ $data = [
'replyto' => $replyto, 'replyto' => $replyto,
'to' => $tos, 'to' => $tos,
@ -1412,28 +1409,84 @@ class Activities extends Controller {
'certificates' => $certificates 'certificates' => $certificates
]; ];
$jsonData = json_encode($data); $jsonData = json_encode($data);
//print_r($jsonData);
//$url = 'http://localhost/work/phpmailer/server.php'; // Cek Dulu Pada Log Email
$url = 'https://sadewa.services-summit.my.id/server.php'; $sql = "SELECT logdate FROM actsend_log WHERE actid = ? ORDER BY logdate DESC LIMIT 1";
$ch = curl_init($url); $result = $db->query($sql, [$actid])->getRowArray();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); if ($result) { // Jika Ada Log
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']); $logDate = new \DateTime($result['logdate']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $now = new \DateTime();
$response = curl_exec($ch); $interval = $logDate->diff($now); // Hitung Selisihnya Apakah Lebih Dari 2 Menit
if(curl_errno($ch)) { echo 'Curl error: '.curl_error($ch); }
//echo "sending email"; if ($interval->i >= 2 || $interval->y > 0 || $interval->m > 0 || $interval->d > 0 || $interval->h > 0) { // Lebih Dari 2 Menit
//echo "$jsonData <br/>";
//echo "<pre>$response</pre>"; $sql = "UPDATE activities set sendmail='1' where actid='$actid'";
curl_close($ch); $query = $db->query($sql);
/*
$myfile = fopen("log.txt", "a+") or die("Unable to open file!"); $sql = "INSERT into actsend_log (actid, replyto, emailto, emailcc, emailbcc, logdate) values ( '$actid', '$replyto', '$tos', '$ccs', '$bccs', NOW() )";
fwrite($myfile,$response); $query = $db->query($sql);
fclose($myfile);
*/ $url = 'https://sadewa.services-summit.my.id/server.php';
// if ($response) { $ch = curl_init($url);
// $db->query("UPDATE activities SET sendmail='1' WHERE actid='$actid'"); curl_setopt($ch, CURLOPT_POST, true);
// } curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if(curl_errno($ch)) { echo 'Curl error: '.curl_error($ch); }
curl_close($ch);
} else { // Kurang Dari 2 menit
return redirect()->to('activities/')->with('error', 'Harap tunggu 2 menit sebelum mengirim email kembali');
}
} else { // Jika Tidak Ada Log
$sql = "UPDATE activities set sendmail='1' where actid='$actid'";
$query = $db->query($sql);
$sql = "INSERT into actsend_log (actid, replyto, emailto, emailcc, emailbcc, logdate) values ( '$actid', '$replyto', '$tos', '$ccs', '$bccs', NOW() )";
$query = $db->query($sql);
$url = 'https://sadewa.services-summit.my.id/server.php';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if(curl_errno($ch)) { echo 'Curl error: '.curl_error($ch); }
curl_close($ch);
}
////print_r($jsonData);
////$url = 'http://localhost/work/phpmailer/server.php';
// $url = 'https://sadewa.services-summit.my.id/server.php';
// $ch = curl_init($url);
// curl_setopt($ch, CURLOPT_POST, true);
// curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
// curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// $response = curl_exec($ch);
// if(curl_errno($ch)) { echo 'Curl error: '.curl_error($ch); }
////echo "sending email";
////echo "$jsonData <br/>";
////echo "<pre>$response</pre>";
// curl_close($ch);
// /*
// $myfile = fopen("log.txt", "a+") or die("Unable to open file!");
// fwrite($myfile,$response);
// fclose($myfile);
// */
// // if ($response) {
// // $db->query("UPDATE activities SET sendmail='1' WHERE actid='$actid'");
// // }
return redirect()->to('activities/'); return redirect()->to('activities/');
} else { } else {
return view('activities_compose', $data); return view('activities_compose', $data);