Phpmailer sending multiple emails

PHPMailer is a popular PHP class used for sending emails. Here's an example of how to send multiple emails using PHPMailer:

Method 1: Looping through an array of recipients

Let's say you have an array of recipients and their corresponding email addresses:

$recipients = array(
    array('name' => 'John Doe', 'email' => '[email protected]'),
    array('name' => 'Jane Doe', 'email' => '[email protected]'),
    array('name' => 'Bob Smith', 'email' => '[email protected]'),
);

$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'your_password';
$mail->setFrom('[email protected]', 'Your Name');

foreach ($recipients as $recipient) {
    $mail->addAddress($recipient['email'], $recipient['name']);
    $mail->Subject = 'Test email';
    $mail->Body = 'This is a test email';
    $mail->send();
}

In this example, we loop through the $recipients array and add each recipient's email address to the addAddress() method. We then set the subject and body of the email and send it using the send() method.

Method 2: Using a loop with a single email instance

Alternatively, you can create a single instance of PHPMailer and loop through the recipients using a single email instance:

$recipients = array(
    '[email protected]',
    '[email protected]',
    '[email protected]',
);

$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'your_password';
$mail->setFrom('[email protected]', 'Your Name');

foreach ($recipients as $recipient) {
    $mail->addAddress($recipient);
    $mail->Subject = 'Test email';
    $mail->Body = 'This is a test email';
    $mail->send();
}

In this example, we create a single instance of PHPMailer and loop through the $recipients array, adding each recipient's email address to the addAddress() method. We then set the subject and body of the email and send it using the send() method.

Method 3: Using a batch email sending class

If you need to send a large number of emails, you can use a batch email sending class like PHPMailerBatch which is a wrapper around PHPMailer that allows you to send multiple emails in a single request:

$recipients = array(
    '[email protected]',
    '[email protected]',
    '[email protected]',
);

$mail = new PHPMailerBatch();
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'your_password';
$mail->setFrom('[email protected]', 'Your Name');

$mail->addEmails($recipients);
$mail->sendBatch();

In this example, we create an instance of PHPMailerBatch and add the recipients to the addEmails() method. We then call the sendBatch() method to send all the emails in a single request.

Note that the above examples are just a starting point, and you may need to modify them to fit your specific use case. Additionally, be sure to check the PHPMailer documentation for more information on sending multiple emails.