diff --git a/scripts/send_email.php b/scripts/send_email.php index fc6bed4a..c102e45d 100755 --- a/scripts/send_email.php +++ b/scripts/send_email.php @@ -268,7 +268,7 @@ $sql = " LEFT JOIN m_patient p ON p.M_PatientID = oh.T_OrderHeaderM_PatientID WHERE e.T_SendEmailIsActive = 'Y' - AND e.T_SendEmailStatus IN ('S') + AND e.T_SendEmailStatus = 'S' AND e.T_SendEmailCount < {$max_retry} {$where_id} ORDER BY e.T_SendEmailID ASC @@ -373,27 +373,28 @@ foreach ($rows as $row) { if (file_exists($f)) unlink($f); } + $group_result_names = array_filter(array_column( + array_filter($reports, 'is_array'), + 'result' + )); + if ($err === null) { - log_msg(" Sent OK"); + log_msg(" Sent OK → R"); $pdo->prepare(" UPDATE t_send_email - SET T_SendEmailStatus = 'D', + SET T_SendEmailStatus = 'R', T_SendEmailCount = T_SendEmailCount + 1, T_SendEmailReceived = NOW(), T_SendEmailLastUpdated = NOW() WHERE T_SendEmailID = ? ")->execute([$id]); - $group_result_names = array_filter(array_column( - array_filter($reports, 'is_array'), - 'result' - )); $pdo_log->prepare(" INSERT INTO t_send_email_log (T_SendEmailLogT_OrderHeaderID, T_SendEmailLogRecepient, T_SendEmailLogStatus, T_SendEmailLogResponse, T_SendEmailLogGroup_ResultName, T_SendEmailLogJson, T_SendEmailLogCreated, T_SendEmailLogCreatedUserID) - VALUES (?, ?, 'D', NULL, ?, ?, NOW(), 0) + VALUES (?, ?, 'R', NULL, ?, ?, NOW(), 0) ")->execute([ $order_id, $recipient, @@ -401,15 +402,33 @@ foreach ($rows as $row) { json_encode($row), ]); } else { - log_msg(" Error: {$err}"); + $new_count = (int)$row['T_SendEmailCount'] + 1; + $new_status = ($new_count >= $max_retry) ? 'E' : 'S'; + log_msg(" Error (retry {$new_count}/{$max_retry}) → {$new_status}: {$err}"); $pdo->prepare(" UPDATE t_send_email - SET T_SendEmailStatus = 'S', + SET T_SendEmailStatus = ?, T_SendEmailCount = T_SendEmailCount + 1, T_SendEmailResponse = ?, T_SendEmailLastUpdated = NOW() WHERE T_SendEmailID = ? - ")->execute([$err, $id]); + ")->execute([$new_status, $err, $id]); + if ($new_status === 'E') { + $pdo_log->prepare(" + INSERT INTO t_send_email_log + (T_SendEmailLogT_OrderHeaderID, T_SendEmailLogRecepient, + T_SendEmailLogStatus, T_SendEmailLogResponse, + T_SendEmailLogGroup_ResultName, T_SendEmailLogJson, + T_SendEmailLogCreated, T_SendEmailLogCreatedUserID) + VALUES (?, ?, 'E', ?, ?, ?, NOW(), 0) + ")->execute([ + $order_id, + $recipient, + $err, + implode(', ', $group_result_names), + json_encode($row), + ]); + } } }