-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmail.php
More file actions
115 lines (92 loc) · 3.54 KB
/
mail.php
File metadata and controls
115 lines (92 loc) · 3.54 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
<?php
require __DIR__ . '/vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
// Access sensitive information from environment variables
$smtp=$_ENV['SMTP'];
$mails=$_ENV['MAIL'];
$pass=$_ENV['PASS'];
$pass2=$_ENV['PASS2'];
$port=$_ENV['PORT'];
require('C:/xampp/htdocs/sms/PHPMailer-master/src/PHPMailer.php');
require('C:/xampp/htdocs/sms/PHPMailer-master/src/Exception.php');
require('C:/xampp/htdocs/sms/PHPMailer-master/src/SMTP.php');
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
include('DB_connect.php');
session_start();
$userId = $_SESSION['user_id']; // Fetching user ID from session
function getAdminDetails($connect, $userId) {
$query = "SELECT admin_email FROM admin_users WHERE admin_id = ?";
$stmt = $connect->prepare($query);
$stmt->bind_param('i', $userId);
$stmt->execute();
$result = $stmt->get_result();
if ($row = $result->fetch_assoc()) {
return $row;
} else {
return null;
}
}
function getsmtpDetails($connect) {
$query = "SELECT student_id, student_email, student_contact_number1 FROM students";
$result = $connect->query($query);
$students = [];
while ($row = $result->fetch_assoc()) {
$students[] = $row;
}
return $students;
}
function getOverduePayments($connect) {
$currentDate = date('Y-m-d');
$query = "SELECT payment_id, student_id, total_amount, due_date FROM deposit WHERE due_date < ? AND status = 'pending'";
$stmt = $connect->prepare($query);
$stmt->bind_param('s', $currentDate);
$stmt->execute();
$result = $stmt->get_result();
$payments = [];
while ($row = $result->fetch_assoc()) {
$payments[] = $row;
}
return $payments;
}
function sendReminder($connect, $payment, $student, $smtpDetails) {
$email = $student['student_email'];
$amount = $payment['total_amount'];
$dueDate = $payment['due_date'];
$mail = new PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = $_ENV['SMTP']; // Use 'smtp.gmail.com'
$mail->SMTPAuth = true;
$mail->Username = $_ENV['MAIL']; // Use your Gmail address
$mail->Password = $_ENV['PASS'];
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = $_ENV['PORT']; // Use port 587
$mail->setFrom($smtpDetails['admin_email'], 'Moses');
$mail->addAddress($email);
$mail->isHTML(true);
$mail->Subject = 'Payment Reminder';
$mail->Body = "Dear Student,<br><br>You have a payment of $amount due on $dueDate.<br>Please make sure to complete the payment before the due date to avoid any penalties.<br><br>Thank you.";
$mail->AltBody = "Dear Student,\n\nYou have a payment of $amount due on $dueDate.\nPlease make sure to complete the payment before the due date to avoid any penalties.\n\nThank you.";
$mail->send();
echo "Reminder sent to $email for payment ID {$payment['payment_id']}<br>";
} catch (Exception $e) {
echo "Message could not be sent to $email. Mailer Error: {$mail->ErrorInfo}<br>";
}
}
$smtpDetails = getSMTPDetails($connect, $userId);
if (!$smtpDetails) {
die("SMTP details not found.");
}
$students = getStudentDetails($connect);
$payments = getOverduePayments($connect);
foreach ($payments as $payment) {
foreach ($students as $student) {
if ($student['student_id'] == $payment['student_id']) {
sendReminder($connect, $payment, $student, $smtpDetails);
}
}
}
$connect->close();
?>