Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php foreach every 100 times sleep then continue [duplicate]

Tags:

foreach

php

sleep

i have a foreach loop and 5000 user in my site and i want to send them emails

every 10 member sleep 5 second then continue

$headers   = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: Sender Name <[email protected]>";
$headers[] = "Bcc: JJ Chong <[email protected]>";
$headers[] = "Reply-To: Recipient Name <[email protected]>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();

$users = array('mohamed','ahmed');
foreach($users as $user){
mail($user, $subject, $email, implode("\r\n", $headers));
}

how to do it

like image 273
Mohamed Saleh Avatar asked Nov 27 '22 21:11

Mohamed Saleh


2 Answers

Use the modulus operator to detect multiples of 10.

foreach ($users as $i => $user) {
    mail($user, $subject, $email, implode("\r\n", $headers));
    if ($i > 0 && $i % 10 == 0) {
        sleep(5);
    }
}
like image 87
Barmar Avatar answered Dec 15 '22 03:12

Barmar


$count=0;
foreach($users as $user){
    $count++;
    mail($user, $subject, $email, implode("\r\n", $headers));
    if(($count%10)==0)
    {
        sleep(5);
    }
}
like image 36
Karan Thakkar Avatar answered Dec 15 '22 01:12

Karan Thakkar