What are the potential issues with using a while loop to send multiple emails in PHP?

Using a while loop to send multiple emails in PHP can lead to potential performance issues if a large number of emails need to be sent. This is because sending each email within the loop can be time-consuming and resource-intensive. To solve this issue, you can consider using a batch processing approach where emails are sent in batches rather than individually.

// Example of sending emails in batches using a for loop
$emails = ['email1@example.com', 'email2@example.com', 'email3@example.com'];

$batchSize = 10; // Number of emails to send in each batch

for ($i = 0; $i < count($emails); $i += $batchSize) {
    $batchEmails = array_slice($emails, $i, $batchSize);

    foreach ($batchEmails as $email) {
        // Code to send email
        echo "Sending email to: $email\n";
    }

    // Additional logic after sending batch of emails
    echo "Batch sent successfully\n";
}