麻烦PHPMAIL..Any建议?

时间:2015-08-23 13:25:52

标签: php mysql sql email

无法执行选择并发送到脚本中的所有电子邮件地址。

// 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+电子邮件发送电子邮件。 谁能看到我哪里出错?或协助。

非常感谢 尖峰

1 个答案:

答案 0 :(得分:0)

Fetch一次只能拉一行。将获取移动到像...一样的循环中。

// 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"]);
    }