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,14 +1348,7 @@ class Activities extends Controller {
if ($this->request->getMethod() === 'POST') {
// $check = $db->query("SELECT sendmail FROM activities WHERE actid='$actid'")->getRow();
// 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);
// // $email = \Config\Services::email();
$replyto = $this->request->getVar('replyto');
$to = $this->request->getVar('to');
@ -1364,43 +1357,47 @@ class Activities extends Controller {
$subject = $this->request->getVar('subject');
$message = $this->request->getVar('message');
$attachment = $this->request->getVar('attachment');
// $attachments = explode(',',$attachment);
// // $attachments = explode(',',$attachment);
/*
$email->setFrom('noreply@services.summit.co.id', 'SUMMIT CRM');
$email->setReplyTo($replyto);
$email->setTo(cleanmail($to));
if($cc != '') { $email->setCC(cleanmail($cc)); }
if($bcc != '') { $email->setBCC(cleanmail($bcc)); }
$email->setSubject($subject);
$email->setMessage($message);
foreach($attachments as $attachment) {
if($attachment != '') {
$attachment = FCPATH."/upload/".$attachment;
$email->attach($attachment);
}
}
//$email->set_newline("\r\n");
//$email->send();
# debugging email CI4
//$email->send(FALSE);
$myfile = fopen("log.txt", "a+") or die("Unable to open file!");
if ( $email->send(FALSE) ) {
fwrite($myfile, "\nemail sent success");
} else {
fwrite($myfile, "\n".$email->printDebugger() );
}
fclose($myfile);
*/
// /*
// $email->setFrom('noreply@services.summit.co.id', 'SUMMIT CRM');
// $email->setReplyTo($replyto);
// $email->setTo(cleanmail($to));
// if($cc != '') { $email->setCC(cleanmail($cc)); }
// if($bcc != '') { $email->setBCC(cleanmail($bcc)); }
// $email->setSubject($subject);
// $email->setMessage($message);
// foreach($attachments as $attachment) {
// if($attachment != '') {
// $attachment = FCPATH."/upload/".$attachment;
// $email->attach($attachment);
// }
// }
// //$email->set_newline("\r\n");
// //$email->send();
// # debugging email CI4
// //$email->send(FALSE);
// $myfile = fopen("log.txt", "a+") or die("Unable to open file!");
// if ( $email->send(FALSE) ) {
// fwrite($myfile, "\nemail sent success");
// } else {
// fwrite($myfile, "\n".$email->printDebugger() );
// }
// fclose($myfile);
// */
$tos = join(",",$to);
if(isset($cc)) { $ccs = join(",",$cc); } else { $ccs = ''; }
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);
// Siapkan Data
$data = [
'replyto' => $replyto,
'to' => $tos,
@ -1412,28 +1409,84 @@ class Activities extends Controller {
'certificates' => $certificates
];
$jsonData = json_encode($data);
//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'");
// }
// Cek Dulu Pada Log Email
$sql = "SELECT logdate FROM actsend_log WHERE actid = ? ORDER BY logdate DESC LIMIT 1";
$result = $db->query($sql, [$actid])->getRowArray();
if ($result) { // Jika Ada Log
$logDate = new \DateTime($result['logdate']);
$now = new \DateTime();
$interval = $logDate->diff($now); // Hitung Selisihnya Apakah Lebih Dari 2 Menit
if ($interval->i >= 2 || $interval->y > 0 || $interval->m > 0 || $interval->d > 0 || $interval->h > 0) { // Lebih Dari 2 Menit
$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);
} 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/');
} else {
return view('activities_compose', $data);