提问者:小点点

php foreach每睡眠100次,然后继续[重复]


我有一个Foreach循环和5000用户在我的网站,我想给他们发送电子邮件

每10个成员睡5秒然后继续

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

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

怎么做呢


共2个答案

匿名用户

使用模运算符检测10的倍数。

foreach ($users as $i => $user) {
    mail($user, $subject, $email, implode("\r\n", $headers));
    if ($i > 0 && $i % 10 == 0) {
        sleep(5);
    }
}

匿名用户

$count=0;
foreach($users as $user){
    $count++;
    mail($user, $subject, $email, implode("\r\n", $headers));
    if(($count%10)==0)
    {
        sleep(5);
    }
}