What are the best practices for sending emails to multiple recipients in PHP?

When sending emails to multiple recipients in PHP, it is important to use the BCC (Blind Carbon Copy) field to protect the privacy of the recipients. This ensures that each recipient's email address is not visible to others. Additionally, it is recommended to loop through the list of recipients and send individual emails to each one to avoid potential issues with email clients displaying the email incorrectly.

<?php
$recipients = array('recipient1@example.com', 'recipient2@example.com', 'recipient3@example.com');

$subject = 'Your Subject Here';
$message = 'Your Message Here';

foreach ($recipients as $recipient) {
    $headers = 'From: Your Name <your_email@example.com>' . "\r\n";
    $headers .= 'Bcc: ' . $recipient . "\r\n";

    mail('', $subject, $message, $headers);
}
?>