How can PHP be used to automatically delete bounced emails after logging them in a file?

When dealing with bounced emails, it is important to log them for tracking purposes but also to remove them from the mailing list to prevent further attempts to send to invalid addresses. To automatically delete bounced emails after logging them in a file, you can use PHP to read the email log file, identify bounced emails, and remove them from the mailing list.

// Read the email log file
$logFile = 'email_log.txt';
$emails = file($logFile, FILE_IGNORE_NEW_LINES);

// Identify bounced emails and remove them from the mailing list
foreach ($emails as $key => $email) {
    if (strpos($email, 'BOUNCE') !== false) {
        unset($emails[$key]);
    }
}

// Write the updated email list back to the log file
file_put_contents($logFile, implode("\n", $emails));