What are some common pitfalls to avoid when sending emails to multiple recipients in PHP?

One common pitfall to avoid when sending emails to multiple recipients in PHP is not properly sanitizing and validating the email addresses. This can lead to errors in sending the emails or potentially exposing sensitive information. To solve this issue, always validate the email addresses before sending the emails to ensure they are in the correct format.

// Example of validating email addresses before sending emails
$recipients = ['recipient1@example.com', 'recipient2@example.com'];

foreach ($recipients as $recipient) {
    if (filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
        // Send email to $recipient
    } else {
        // Handle invalid email address
    }
}