doupo2241 2016-01-22 05:56
浏览 62
已采纳

睡眠功能不能在PHP中工作

i am trying to send many mails at time with some time interval and this is my code.

<?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");
?>

for time interval i added sleep function but it's not working. i am testing three mails at time but all three displays same time in mailbody. please help me to add time interval.

  • 写回答

1条回答 默认 最新

  • doufu6196 2016-01-22 06:08
    关注

    Your line;

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

    ...will do a continue whether the mail fails sending or not.

    continue will skip the rest of the loop (the sleep) and send the next mail right away.

    To execute the sleep, just remove continue;

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

    ...or move the sleep before that line.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?