2015-08-23 53 views
0

無法執行選擇併發送到腳本中的所有電子郵件地址。PHPMAIL麻煩..任何建議?

// Get Employee's Email Address 
      $getEmail = "SELECT empEmail AS theEmail FROM employees"; 
      $emailres = mysqli_query($mysqli, $getEmail) or die('-1'.mysqli_error()); 
      $col = mysqli_fetch_assoc($emailres); 
      $theEmail = $col['theEmail'];   
// the message 
      $message = '<html><body>'; 
      $message .= '<h3>New Site Notifications</h3>'; 
      $message .= '<p>'.$noticeTitle.'</p>'; 
      $message .= '<p>'.$noticeText.'</p>'; 
      $message .= '<p>'.$messageText.'</p>'; 
      $message .= '<hr>'; 
      $message .= '<p>'.$emailLoginLink.'</p>'; 
      $message .= '<p>Thank you<br>Bliss Door Supervisors</p>'; 
      $message .= '</body></html>'; 
      $headers = "From: ".$siteName." <".$businessEmail.">\r\n"; 
      $headers .= "Reply-To: ".$businessEmail."\r\n"; 
      $headers .= "MIME-Version: 1.0\r\n"; 
      $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; 



// use wordwrap() if lines are longer than 70 characters 
//$msg = wordwrap($msg,70); 

// send email 
mail($theEmail," New Site Notification",$message,$headers); 
//End Send Mail 

由於某種原因,它只發送數據庫中的第一封電子郵件,而不是其他10+ witihin。 任何人都可以看到我要去哪裏錯了嗎?或協助。

非常感謝 斯派克

+0

嘗試使用循環。 –

回答

0

取一次只拉一行。移動至讀取到環路像...

// Get Employee's Email Address 
      $getEmail = "SELECT empEmail AS theEmail FROM employees"; 
      $emailres = mysqli_query($mysqli, $getEmail) or die('-1'.mysqli_error()); 
      while($col = mysqli_fetch_assoc($emailres)){ //start loop here so each email address is pulled 
       $theEmail = $col['theEmail'];   
// the message 
       $message = '<html><body>'; 
       $message .= '<h3>New Site Notifications</h3>'; 
       $message .= '<p>'.$noticeTitle.'</p>'; 
       $message .= '<p>'.$noticeText.'</p>'; 
       $message .= '<p>'.$messageText.'</p>'; 
       $message .= '<hr>'; 
       $message .= '<p>'.$emailLoginLink.'</p>'; 
       $message .= '<p>Thank you<br>Bliss Door Supervisors</p>'; 
       $message .= '</body></html>'; 
       $headers = "From: ".$siteName." <".$businessEmail.">\r\n"; 
       $headers .= "Reply-To: ".$businessEmail."\r\n"; 
       $headers .= "MIME-Version: 1.0\r\n"; 
       $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; 



    // use wordwrap() if lines are longer than 70 characters 
    //$msg = wordwrap($msg,70); 

    // send email 
    mail($theEmail," New Site Notification",$message,$headers); 
    //End Send Mail 
}//end loop 

參考:http://php.net/manual/en/mysqli-result.fetch-assoc.php

返回對應於所提取的行

從他們的示例關聯數組:

$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5"; 

if ($result = $mysqli->query($query)) { 

    /* fetch associative array */ 
    while ($row = $result->fetch_assoc()) { 
     printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]); 
    } 
+0

令人驚訝。非常感謝你的幫助。 :d –