What is the role of a cron job in scheduling tasks like sending emails at specific times in PHP?

Cron jobs are used to schedule tasks to run at specific times on a server. In the context of sending emails at specific times in PHP, a cron job can be set up to execute a PHP script that sends the emails at the desired times. This allows for automated and timely email notifications to be sent without manual intervention.

// Example of a PHP script to send emails at specific times
// This script can be executed by a cron job to send emails automatically

// Set the time to send the email
$send_time = strtotime('tomorrow 9:00 AM');

// Check if the current time matches the send time
if (time() >= $send_time) {
    // Code to send the email
    $to = 'recipient@example.com';
    $subject = 'Scheduled Email';
    $message = 'This is a scheduled email sent at a specific time.';
    $headers = 'From: sender@example.com';

    // Send the email
    mail($to, $subject, $message, $headers);
}