How can you dynamically add multiple email recipients in a PHP script based on user input?

When dynamically adding multiple email recipients in a PHP script based on user input, you can use an array to store the email addresses provided by the user. You can then loop through this array and add each email address as a recipient in the email sending function.

<?php
// Get user input for email addresses
$user_input_emails = $_POST['emails'];

// Split the input into an array of email addresses
$emails = explode(',', $user_input_emails);

// Loop through the array and add each email address as a recipient
foreach ($emails as $email) {
    $to = trim($email);
    $subject = 'Test Email';
    $message = 'This is a test email.';
    $headers = 'From: your_email@example.com';

    // Send the email
    mail($to, $subject, $message, $headers);
}
?>