How can one prevent the PHP mail() function from sending duplicate emails?

To prevent the PHP mail() function from sending duplicate emails, you can set a flag or variable to track whether an email has already been sent within a certain time frame. This way, you can avoid sending multiple emails for the same event or action.

// Example code to prevent sending duplicate emails using a flag
$alreadySent = false;

// Check if the email has already been sent within the last 24 hours
if (!$alreadySent) {
    // Send the email using the mail() function
    mail($to, $subject, $message);
    
    // Set the flag to true to indicate that the email has been sent
    $alreadySent = true;
}