【问题标题】:Send email to selected users in database向数据库中的选定用户发送电子邮件
【发布时间】:2016-03-04 00:30:59
【问题描述】:

我正在构建一个 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);
  }
?>

【问题讨论】:

标签: php


【解决方案1】:

将此部分更改为...

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);
        }

    }
}

【讨论】:

    猜你喜欢
    • 2012-11-18
    • 2017-09-29
    • 2015-02-24
    • 2020-06-26
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 2019-12-15
    • 1970-01-01
    相关资源
    最近更新 更多