How can PHP scripts be optimized to prevent excessive memory usage when processing emails?

To prevent excessive memory usage when processing emails in PHP scripts, it is important to optimize the code by freeing up memory after processing each email. This can be done by unsetting variables and objects that are no longer needed, closing database connections, and using efficient memory management techniques.

// Process email
// Your email processing code here

// Free up memory
unset($emailData);
unset($attachment);
// Close database connection
$dbConnection->close();