What are the best practices for sending automated emails in PHP based on specific conditions, such as birthdays?

Sending automated emails in PHP based on specific conditions, such as birthdays, can be achieved by setting up a cron job to run a PHP script at a scheduled time. The PHP script should check the database for users whose birthdays match the current date and then send personalized birthday emails to them.

// Check for users with birthdays today
$currentDate = date('m-d');
$query = "SELECT email FROM users WHERE DATE_FORMAT(birthday, '%m-%d') = '$currentDate'";
$result = mysqli_query($conn, $query);

// Send personalized birthday emails
while($row = mysqli_fetch_assoc($result)) {
    $to = $row['email'];
    $subject = "Happy Birthday!";
    $message = "Happy Birthday! We hope you have a fantastic day!";
    $headers = "From: your_email@example.com";
    mail($to, $subject, $message, $headers);
}