What best practices should be followed when marking and deleting emails in a PHP script?

When marking and deleting emails in a PHP script, it is important to ensure that the actions are performed securely and accurately. To do this, you should use proper error handling to catch any potential issues, validate user input to prevent malicious attacks, and confirm the action with the user before proceeding.

// Example code for marking and deleting emails in a PHP script

// Validate user input
$emailId = filter_input(INPUT_POST, 'email_id', FILTER_VALIDATE_INT);
$action = filter_input(INPUT_POST, 'action', FILTER_SANITIZE_STRING);

// Confirm action with user
if ($action === 'mark_as_read') {
    // Mark email as read
    // Code to mark email as read
} elseif ($action === 'delete') {
    // Delete email
    // Code to delete email
} else {
    echo 'Invalid action';
}

// Error handling
if (!$emailId || !$action) {
    echo 'Error processing request';
    // Log error or handle accordingly
}