What are some recommended ways to structure and organize PHP code for sending bulk emails to multiple recipients?

When sending bulk emails to multiple recipients in PHP, it is recommended to use a loop to iterate through the list of recipients and send individual emails to each one. This ensures that each recipient receives a personalized email without the risk of exposing other recipients' email addresses. Additionally, it is important to properly handle errors and exceptions during the email sending process to ensure robustness and reliability.

// List of recipients
$recipients = array('recipient1@example.com', 'recipient2@example.com', 'recipient3@example.com');

// Loop through each recipient and send email
foreach ($recipients as $recipient) {
    $to = $recipient;
    $subject = 'Your Subject Here';
    $message = 'Your Email Message Here';
    $headers = 'From: yourname@example.com' . "\r\n" .
        'Reply-To: yourname@example.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();

    if (mail($to, $subject, $message, $headers)) {
        echo 'Email sent to ' . $recipient . '<br>';
    } else {
        echo 'Failed to send email to ' . $recipient . '<br>';
    }
}