睡眠功能不能在PHP中工作

时间:2016-01-22 05:56:03

标签: php

我正在尝试按时间间隔发送许多邮件,这是我的代码。

<?php session_start();
if(isset($_SESSION['init']) && isset($_SESSION['user']) && $_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['company']) && $_GET['company'] != '') {
    require_once("../../includes/config.php");
    $failed = 0; $success = 0; $values = '';
    require_once('../../includes/mailer/class.phpmailer.php');
    $companyResult = mysql_query("SELECT com_name,com_email,password,com_id FROM company_details WHERE com_id IN (".$_GET['company'].")");
    $n = 0; $s = 0;
    while ($companyRow = mysql_fetch_array($companyResult)) {
        $mailBody = "Hello There '".date('h:i:s')."'";
        $mail = new PHPMailer;
        $mail->isSMTP();
        $mail->Host = 'grimlock.secure-dns.net';
        $mail->SMTPAuth = true;
        $mail->Username = 'username';
        $mail->Password = 'password';
        $mail->SMTPSecure = 'tls';
        $mail->Port = 587;
        $mail->setFrom('info@example.com', 'example');
        $mail->addReplyTo('info@example.com', 'example');
        $mail->addAddress($companyRow[1], 'example');
        $mail->WordWrap = 50;
        $mail->isHTML(true);
        $mail->Subject = "Account Detail";
        $mail->Body = $mailBody;
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
        if(!$mail->send()) {$failed++;continue;} else {$success++;continue;}
        sleep(10);
    }
    if($failed > 0) {
        $_SESSION['error'][0] = $failed . " company has error";
        $_SESSION['message'] = $success . " company has recieved mail";
    } else
        $_SESSION['message'] = "Mail Successfully Sent";
    header("location:".$_SERVER['HTTP_REFERER']);
} else header("location:../user/out.php");
?>

对于时间间隔,我添加了睡眠功能,但它不起作用。我正在测试三个邮件,但三个邮件在邮件主体中同时显示。请帮我添加时间间隔。

1 个答案:

答案 0 :(得分:4)

你的路线;

if(!$mail->send()) {$failed++;continue;} else {$success++;continue;}

...无论发送邮件是否失败,都会continue

continue将跳过循环的其余部分(sleep)并立即发送下一封邮件。

要执行sleep,只需删除continue;

if(!$mail->send()) {$failed++;} else {$success++;}

...或在该行之前移动sleep

相关问题