What are some best practices for handling one-time email notifications in PHP scripts?

When sending one-time email notifications in PHP scripts, it is important to ensure that the email is sent only once and not repeatedly. One way to achieve this is to set a flag in the database indicating that the email has been sent, and check this flag before sending the email again.

// Check if email has already been sent
if (!$email_sent) {
    // Send email
    $to = "recipient@example.com";
    $subject = "Notification";
    $message = "This is a one-time notification.";
    $headers = "From: sender@example.com";
    
    if (mail($to, $subject, $message, $headers)) {
        // Update flag in database to indicate email has been sent
        $email_sent = true;
        // Save the flag in database or session
    } else {
        echo "Failed to send email.";
    }
}