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();
}
Related Questions
- In what situations would it be more efficient to use preg_split instead of regular expressions in PHP for string manipulation?
- What is the purpose of developing standalone applications with phpGTK and how does it differ from traditional web applications?
- How can dynamic sorting based on multiple columns be achieved in PHP, especially when dealing with special characters like umlauts?