Material_Verwaltungs_Tool/send_notifications.php

41 lines
1.4 KiB
PHP
Raw Permalink Normal View History

2024-02-25 15:28:37 +00:00
<?php
// Datenbankverbindung herstellen
include_once 'config/config.php';
// Verbindung zur Datenbank herstellen
$conn = new mysqli($host, $username, $password, $database);
if ($conn->connect_error) {
die("Verbindung zur Datenbank fehlgeschlagen: " . $conn->connect_error);
}
// Aktuelles Datum und Zeit abrufen
$current_datetime = date("Y-m-d H:i:s");
// Benachrichtigungsgrenze berechnen (13 Uhr)
$notification_datetime = date("Y-m-d 13:00:00");
// Nur weitermachen, wenn es nach 13 Uhr ist
if ($current_datetime >= $notification_datetime) {
// SQL-Abfrage vorbereiten
$sql = "SELECT * FROM materials WHERE expiration_date <= DATE_ADD(CURDATE(), INTERVAL notification_days DAY)";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// E-Mails für ablaufende Artikel senden
while($row = $result->fetch_assoc()) {
$to = $row["email"];
$subject = 'Artikel Ablaufbenachrichtigung';
$message = 'Der Artikel ' . $row["item_name"] . ' mit Barcode ' . $row["barcode"] . ' läuft am ' . $row["expiration_date"] . ' ab.';
$headers = 'From: noreply@viper.com' . "\r\n" .
'Reply-To: noreply@viper.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
}
}
$conn->close();
?>