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.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度