How can PHPMailer and SMTP be used to send emails to multiple recipients without causing timeout issues?
When sending emails to multiple recipients using PHPMailer and SMTP, the issue of timeout can arise if the script takes too long to process all recipients. To solve this, you can use the `set_time_limit()` function to extend the script execution time. Additionally, you can use the `clearAddresses()` method in a loop to clear the recipient list after each email is sent, preventing the list from growing too large.
// Extend script execution time
set_time_limit(0);
// Loop through multiple recipients and send emails
$recipients = ['recipient1@example.com', 'recipient2@example.com', 'recipient3@example.com'];
foreach ($recipients as $recipient) {
$mail = new PHPMailer(true);
$mail->addAddress($recipient);
$mail->Subject = 'Subject';
$mail->Body = 'Email body';
$mail->send();
// Clear recipient list to prevent timeout
$mail->clearAddresses();
}