What are the best practices for creating a script to generate and store emails in a specific directory in PHP?

To generate and store emails in a specific directory in PHP, you can create a script that generates the email content, saves it as a file in the desired directory, and then retrieves the emails when needed. This can be useful for storing email templates, logs, or backups in a structured manner.

<?php
// Generate email content
$emailContent = "Hello, this is a test email.";

// Define directory to store emails
$directory = "emails/";

// Create unique filename for each email
$filename = $directory . "email_" . uniqid() . ".txt";

// Save email content to file
file_put_contents($filename, $emailContent);

// Retrieve emails from directory
$emails = glob($directory . "*.txt");

// Display stored emails
foreach ($emails as $email) {
    echo file_get_contents($email) . "<br>";
}
?>