How can developers optimize PHP scripts for performance and efficiency when sending emails using the mail() function?

To optimize PHP scripts for performance and efficiency when sending emails using the mail() function, developers can batch multiple emails into a single call to the mail() function. This reduces the overhead of opening and closing connections for each email sent, improving performance.

// Batch multiple emails into a single call to the mail() function
$to = "recipient1@example.com, recipient2@example.com";
$subject = "Subject";
$message = "Message";
$headers = "From: sender@example.com";

mail($to, $subject, $message, $headers);