-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_email_queue.php
More file actions
165 lines (150 loc) · 5.43 KB
/
Copy pathprocess_email_queue.php
File metadata and controls
165 lines (150 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* Process Email Queue - Background Job
* This script processes pending emails from the queue
* Can be run via cron job or called asynchronously
*/
// Set execution time limit for background processing
set_time_limit(300);
// Include configuration
require_once 'config.php';
require_once 'send_email.php';
// Process pending emails
$stmt = $conn->prepare("
SELECT eq.id, eq.complaint_id, eq.department_id, eq.attempts, eq.max_attempts
FROM email_queue eq
WHERE eq.status = 'pending' AND eq.attempts < eq.max_attempts
ORDER BY eq.created_at ASC
LIMIT 10
");
$stmt->execute();
$result = $stmt->get_result();
$processed = 0;
$failed = 0;
while ($queue_item = $result->fetch_assoc()) {
$queue_id = $queue_item['id'];
$complaint_id = $queue_item['complaint_id'];
$department_id = $queue_item['department_id'];
$attempts = $queue_item['attempts'];
$max_attempts = $queue_item['max_attempts'];
try {
// Get complaint info
$stmt_complaint = $conn->prepare("
SELECT c.id, c.description, c.created_at, cat.name as category_name
FROM complaints c
LEFT JOIN categories cat ON c.category_id = cat.id
WHERE c.id = ?
");
$stmt_complaint->bind_param("i", $complaint_id);
$stmt_complaint->execute();
$complaint_info = $stmt_complaint->get_result()->fetch_assoc();
if (!$complaint_info) {
// Mark as failed if complaint not found
$stmt_update = $conn->prepare("
UPDATE email_queue
SET status = 'failed', error_message = 'Complaint not found'
WHERE id = ?
");
$stmt_update->bind_param("i", $queue_id);
$stmt_update->execute();
$failed++;
continue;
}
// Get department info
$stmt_dept = $conn->prepare("
SELECT id, name, manager, email
FROM departments
WHERE id = ?
");
$stmt_dept->bind_param("i", $department_id);
$stmt_dept->execute();
$dept_info = $stmt_dept->get_result()->fetch_assoc();
if (!$dept_info) {
// Mark as failed if department not found
$stmt_update = $conn->prepare("
UPDATE email_queue
SET status = 'failed', error_message = 'Department not found'
WHERE id = ?
");
$stmt_update->bind_param("i", $queue_id);
$stmt_update->execute();
$failed++;
continue;
}
// Send email
$email_result = sendDepartmentNotification($dept_info, $complaint_info);
if ($email_result['success']) {
// Mark as sent
$stmt_update = $conn->prepare("
UPDATE email_queue
SET status = 'sent', sent_at = NOW(), attempts = attempts + 1
WHERE id = ?
");
$stmt_update->bind_param("i", $queue_id);
$stmt_update->execute();
$processed++;
} else {
// Increment attempts
$new_attempts = $attempts + 1;
$error_msg = $email_result['message'] ?? 'Unknown error';
if ($new_attempts >= $max_attempts) {
// Mark as failed after max attempts
$stmt_update = $conn->prepare("
UPDATE email_queue
SET status = 'failed', error_message = ?, attempts = ?
WHERE id = ?
");
$stmt_update->bind_param("sii", $error_msg, $new_attempts, $queue_id);
$stmt_update->execute();
$failed++;
} else {
// Keep as pending for retry
$stmt_update = $conn->prepare("
UPDATE email_queue
SET error_message = ?, attempts = ?
WHERE id = ?
");
$stmt_update->bind_param("sii", $error_msg, $new_attempts, $queue_id);
$stmt_update->execute();
}
}
} catch (Exception $e) {
// Log error and mark as failed
$error_msg = $e->getMessage();
$new_attempts = $attempts + 1;
if ($new_attempts >= $max_attempts) {
$stmt_update = $conn->prepare("
UPDATE email_queue
SET status = 'failed', error_message = ?, attempts = ?
WHERE id = ?
");
$stmt_update->bind_param("sii", $error_msg, $new_attempts, $queue_id);
$stmt_update->execute();
$failed++;
} else {
$stmt_update = $conn->prepare("
UPDATE email_queue
SET error_message = ?, attempts = ?
WHERE id = ?
");
$stmt_update->bind_param("sii", $error_msg, $new_attempts, $queue_id);
$stmt_update->execute();
}
}
}
// Return JSON response if called via AJAX
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') {
header('Content-Type: application/json');
echo json_encode([
'success' => true,
'processed' => $processed,
'failed' => $failed
]);
} else {
// CLI output
echo "Email Queue Processor\n";
echo "Processed: $processed\n";
echo "Failed: $failed\n";
}
$conn->close();
?>