2016-01-22 161 views
1

我試圖在一次發送許多電子郵件與一些時間間隔不工作,這是我的代碼。睡眠功能在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('[email protected]', 'example'); 
     $mail->addReplyTo('[email protected]', '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"); 
?> 

對於時間間隔我增加了睡眠功能,但它不工作。我正在測試三封郵件,但所有三封郵件都顯示在同一時間。請幫我添加時間間隔。

回答

4

你行;

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

...會做一個continue郵件是否發送失敗。

continue將跳過循環的其餘部分(sleep)並立即發送下一封郵件。

要執行sleep,只是刪除continue;

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

...或在該行之前移動sleep

+1

約阿希姆是正確的,如果現在 – vitr

+0

移動之前睡覺,我怎麼能在每封郵件顯示郵件發送或失敗? –