Skip to content
PHP Logo

What are the potential pitfalls of instancing the PEAR Mail class for each email recipient in PHP?

Creating a new instance of the PEAR Mail class for each email recipient can lead to performance issues and unnecessary resource consumption. To solve this, you can create a single instance of the PEAR Mail class and reuse it for sending emails to multiple recipients.

php
// Create a single instance of the PEAR Mail class
$mail = new Mail();

// Loop through email recipients and send emails
foreach ($recipients as $recipient) {
    // Use the same $mail instance to send email to each recipient
    $mail->send($recipient, $subject, $message);
}