What are some best practices for handling email sending functionality in PHP scripts to prevent accidental mass mailings?

Accidental mass mailings can be prevented by implementing safeguards such as setting a limit on the number of recipients per email or using a delay between sending each email. This can help avoid overwhelming servers or inadvertently spamming recipients.

// Example code snippet to prevent accidental mass mailings in PHP
$recipients = array('recipient1@example.com', 'recipient2@example.com', 'recipient3@example.com');

// Set a limit on the number of recipients per email
$maxRecipientsPerEmail = 10;

// Loop through the recipients array and send emails with a delay
foreach ($recipients as $recipient) {
    $to = $recipient;
    $subject = 'Your subject here';
    $message = 'Your message here';

    // Send email
    mail($to, $subject, $message);

    // Introduce a delay between sending each email
    sleep(1); // 1 second delay
}