How can the PHP code for sending newsletters be optimized to handle a large number of email addresses efficiently?

When sending newsletters to a large number of email addresses, it is important to optimize the PHP code to handle the process efficiently. One way to do this is by using batch processing to send emails in chunks rather than all at once. This can prevent timeouts and memory issues that may occur when processing a large number of email addresses.

// Example PHP code snippet for sending newsletters efficiently to a large number of email addresses using batch processing

// List of email addresses
$emailAddresses = array('email1@example.com', 'email2@example.com', 'email3@example.com', ...);

// Batch size for sending emails
$batchSize = 100;

// Loop through email addresses in batches
for ($i = 0; $i < count($emailAddresses); $i += $batchSize) {
    $batchEmails = array_slice($emailAddresses, $i, $batchSize);

    // Send emails in the current batch
    foreach ($batchEmails as $email) {
        // Code to send email to $email
        // For example: mail($email, 'Newsletter Subject', 'Newsletter Content');
    }

    // Optional: Add a delay between batches to prevent server overload
    // For example: sleep(1);
}