向数据库中的选定用户发送电子邮件

时间:2015-11-30 11:23:07

标签: php

我建立了一个cron,当我的库存很低时,它会向管理员发送一封电子邮件。现在,我只能将这封电子邮件发送给一个收件人。有人可以帮帮我吗?我似乎无法从中得到逻辑。

<?php

  $db_host = "dbhost";
  $db_username = "user";
  $db_password = "pass";

  $conn = mysql_connect($db_host,$db_username,$db_password) or die ('MySQL Not found // Could Not Connect.');
  $db  = mysql_select_db("mydb", $conn) or die (mysql_error());

  $result = mysql_query("SELECT prod_count, restock_level, prod_code FROM inventory WHERE prod_count <= restock_level;");

  if ($result) {
    $row = mysql_fetch_assoc($result);
    $count = $row['prod_count'];
    $restocklvl = $row['restock_level'];

    if ($count <= $restocklvl) {
      $subject ="Low Inventory Notification";
      $recipient = "adminemail@email.com"; //Users from the database

      $msg = "Dear Admin, 

      Please check your inventory status. There are currently some items that are low on stock and needs to be replenished.

      *This is an automated message. Please do not reply.";
      mail($recipient, $subject, $msg);
    }
  } else {
    $msg = "An error occurred while checking inventory: " . mysql_error();
    mail($recipient, "Inventory check error", $msg);
  }
?>

1 个答案:

答案 0 :(得分:0)

将此部分更改为...

if ($result) {
    while ($row = mysql_fetch_assoc($result)){
        $count = $row['prod_count'];
        $restocklvl = $row['restock_level'];
        if ($count <= $restocklvl) {
            $subject ="Low Inventory Notification";
            $recipient = "adminemail@email.com"; //Users from the database

            $msg = "Dear Admin, 

             Please check your inventory status. There are currently some items that are low on stock and needs to be replenished.

            *This is an automated message. Please do not reply.";
            mail($recipient, $subject, $msg);
        }

    }
}