How can PHP interact with a MySQL database to schedule and send reminder emails?
To schedule and send reminder emails using PHP and MySQL, you can create a script that runs periodically (e.g., using a cron job) to check the database for upcoming reminders and send emails accordingly. You can use PHP's `mysqli` or `PDO` extension to connect to the MySQL database, query the reminders table for upcoming events, and send emails using PHP's `mail` function or a library like PHPMailer.
<?php
// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");
// Check for connection errors
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Query database for upcoming reminders
$query = "SELECT * FROM reminders WHERE reminder_date = CURDATE()";
$result = $mysqli->query($query);
// Loop through results and send reminder emails
while ($row = $result->fetch_assoc()) {
$to = $row['email'];
$subject = "Reminder: " . $row['reminder_message'];
$message = "This is a reminder for: " . $row['reminder_message'];
// Send email
mail($to, $subject, $message);
}
// Close database connection
$mysqli->close();
?>