-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_reminder.php
197 lines (175 loc) · 5.62 KB
/
send_reminder.php
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
include('db.php');
session_start();
// Check if the user is logged in
if (!isset($_SESSION['user_id'])) {
header('Location: index.html');
exit();
}
// Get the logged-in user's ID
$user_id = $_SESSION['user_id'];
// Fetch the logged-in user's email
$user_email_query = "SELECT email FROM users WHERE id = $user_id";
$user_email_result = $conn->query($user_email_query);
if ($user_email_result->num_rows > 0) {
$user_email_row = $user_email_result->fetch_assoc();
$user_email = $user_email_row['email'];
} else {
echo "User email not found.";
exit();
}
// Fetch upcoming bills for the user
$sql = "SELECT * FROM bills WHERE user_id = $user_id AND due_date >= CURDATE() AND due_date <= DATE_ADD(CURDATE(), INTERVAL 3 DAY)";
$result = $conn->query($sql);
// Initialize the reminder status
$reminder_status = '';
if ($result->num_rows > 0) {
// Import PHPMailer classes
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
require 'PHPMailer/src/Exception.php';
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; // Use your mail server
$mail->SMTPAuth = true;
$mail->Username = 'Your_email'; // Your email
$mail->Password = 'your_password'; // Your password or app-specific password
$mail->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
// Set sender and recipient
$mail->setFrom('Your_email', 'Bill Reminder');
$mail->addAddress($user_email); // Recipient email
// Prepare email body with bill reminders
$mail_body = "Upcoming bills:\n\n";
while ($row = $result->fetch_assoc()) {
$mail_body .= "Bill: " . $row['bill_name'] . " - Due Date: " . $row['due_date'] . "\n"."Please pay the upcoming ". $row['bill_name']." bill within the due date!";
}
// Set email subject and body
$mail->Subject = 'Upcoming Bill Payment Reminder';
$mail->Body = $mail_body;
// Try to send the email
if ($mail->send()) {
$reminder_status = 'success'; // Set success status
} else {
$reminder_status = 'error'; // Set error status
}
} else {
$reminder_status = 'no_bills'; // Set no upcoming bills status
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Email Reminder Status</title>
<link rel="stylesheet" href="style.css"> <!-- External styles -->
<style>
/* General Modal Styles */
.modal {
display: none;
position: fixed;
z-index: 999;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.7);
transition: opacity 0.4s ease, visibility 0.4s ease;
}
.modal-content {
background-color: #fff;
margin: 10% auto;
padding: 30px;
border-radius: 15px;
text-align: center;
width: 80%;
max-width: 500px;
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
animation: slide-down 0.5s ease-out;
}
@keyframes slide-down {
from {
transform: translateY(-50px);
opacity: 0;
}
to {
transform: translateY(0);
opacity: 1;
}
}
.success {
background-color: #d4edda;
color: #155724;
border-left: 5px solid #28a745;
}
.error {
background-color: #f8d7da;
color: #721c24;
border-left: 5px solid #dc3545;
}
.no-bills {
background-color: #fff3cd;
color: #856404;
border-left: 5px solid #ffc107;
}
.close-btn {
margin-top: 20px;
padding: 12px 25px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
font-weight: bold;
transition: background-color 0.3s ease, transform 0.2s ease;
}
.close-btn:hover {
background-color: #0056b3;
transform: translateY(-2px);
}
@media screen and (max-width: 600px) {
.modal-content {
width: 90%;
padding: 20px;
}
h2 {
font-size: 22px;
}
.close-btn {
padding: 10px 20px;
font-size: 14px;
}
}
</style>
</head>
<body>
<!-- Modal for email status -->
<div id="emailStatusModal" class="modal">
<div class="modal-content <?php echo $reminder_status; ?>">
<h2>
<?php
if ($reminder_status == 'success') {
echo "Reminder email sent successfully!";
} elseif ($reminder_status == 'error') {
echo "Error sending reminder email.";
} elseif ($reminder_status == 'no_bills') {
echo "No upcoming bills to send reminders for.";
}
?>
</h2>
<button class="close-btn" onclick="closeModal()">Close</button>
</div>
</div>
<script>
// Display the modal
document.getElementById('emailStatusModal').style.display = 'block';
// Function to close the modal
function closeModal() {
document.getElementById('emailStatusModal').style.display = 'none';
window.location.href = "dashboard.php"; // Redirect to dashboard after closing
}
</script>
</body>
</html>