I’m using PHPMailer for single emails successfully for a long time.
Now I’m trying to upgrade my back-office reservation system so it will send each day to our suppliers the next day reservations. We have approximately 150-300 reservations per day while each reservation requires its own separate email.
My script receives an array of reservation Id’s, loops them and for each one retrieves its details, set the html and send it with PHPMailer using the following script:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPAutoTLS = false;
$mail->Host = '11.222.33.444';
$mail->Port = 25;
$mail->Username = 'user@domain.com';
$mail->Password = '1234';
$mail->isHTML(true);
$mail->SetFrom('admin@client.com', 'admin');
$mail->Subject = $subject;
$mail->Body = $body;
$mail->CharSet = 'UTF-8';
$mail->AddAddress($service->supplier_email);
$mail->addCC('user@domain.com');
$strat_time = microtime(true);
$mail->Send();
echo microtime(true) - $strat_time . "<br>";
The problem is its taking long long time (approximately 5 second just execute to the $mail->Send() function, it means that in production the script needs to run about 25-35 minutes to send all reservations).
I’m not sure for how long to extend the ‘max_execution_time’ or if its smart to let the script to run for so long.
I have tried to switch the IsSMTP() to IsMail(), the script runs much faster but it seems no emails are received.
I would highly appreciate any kind of advice or direction about the most efficient way to perform the above task
******* Added after comments *******
@Synchro I have tried the approach you referred to, but i get minor improve in the execution time (about 20%), certainly not 250 messages per second. This is my test code:
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPKeepAlive = true;
$mail->SMTPAutoTLS = false;
$mail->Host = '******';
$mail->Port = 25;
$mail->Username = '*****';
$mail->Password = '****';
$mail->isHTML(true);
$mail->SetFrom('******', '*****');
$mail->CharSet = 'UTF-8';
foreach ($array as $value) {
$mail->Subject = $value->subject;
$mail->Body = $value->body;
$mail->AddAddress($value->mail);
$mail->Send();
$mail->clearAddresses();
}
Can you recognize what I'm doing wrong?