【问题标题】:PHP - Only send email when Quantity is below 5PHP - 仅在数量低于 5 时发送电子邮件
【发布时间】:2015-10-13 06:14:06
【问题描述】:

如果数量少于 5,我如何才能发送电子邮件?下面的代码将在我加载页面时发送电子邮件。我怎样才能达到这个限制?

  $queryNotification = "SELECT * from stock where stockQty <= :qua1 ";
  $stmtNotification  = $conn->prepare($queryNotification);
  $stmtNotification->bindParam(':qua1',$qua1);
  $stmtNotification->execute();

  $listofnotification = '';
  while ($queryNotRow = $stmtNotification->fetch()){

        $stockname     = $queryNotRow['stockName'];
        $stockquantity = $queryNotRow['stockQty'];
        $Category      = $queryNotRow['category'];


  } 

      $mail = new PHPMailer;

      // Set mailer to use SMTP 
      $mail->isSMTP();

      // Specify main SMTP servers 
      $mail->Host = 'smtp.gmail.com'; 
      // Enable SMTP authentication
      $mail->SMTPAuth = true;

      // SMTP username 
      $mail->Username = '';
      // SMTP password 
      $mail->Password = '';

      // Enable TLS encryption (gmail setting)
      $mail->SMTPSecure = 'tls';  
      // TCP port to connect to (gmail setting) 
      $mail->Port = 587;
      $mail->From = 'a';
      $mail->FromName = 'a';
      // Add recipients
      $mail->addAddress($adminemail, $adminfullname);


      $mail->isHTML(true);   // Set email format to HTML

      $mail->Subject =  'Notification Of Stock Low';
      $mail->Body    = 'Dear '.$adminfullname.', <br><br> Your Shop Stock Are Low ,<br>Please Reorder Again,<br><br> Below are the Stock Low Details<br><br>'.$listofnotification.'<br><br>Thank you.';

      if(!$mail->send()) {
          echo 'Message could not be sent.';
          echo 'Mailer Error: ' . $mail->ErrorInfo;
      } else {

      }

【问题讨论】:

  • 你写的代码是这样的。发送邮件时添加if()查看数量。

标签: php html email pdo sendmail


【解决方案1】:
$queryNotification = "SELECT * from stock where stockQty <= :qua1 ";
$stmtNotification  = $conn->prepare($queryNotification);
$stmtNotification->bindParam(':qua1',$qua1);
$stmtNotification->execute();

$listofnotification = '';

$totalQty = 0;
while ($queryNotRow = $stmtNotification->fetch()){

$stockname     = $queryNotRow['stockName'];
$stockquantity = $queryNotRow['stockQty'];
$Category      = $queryNotRow['category'];
$totalQty = $totalQty + $stockquantity;
}

if($totalQty <=5)
{
    $mail = new PHPMailer;

    // Set mailer to use SMTP 
    $mail->isSMTP();

    // Specify main SMTP servers 
    $mail->Host = 'smtp.gmail.com'; 
    // Enable SMTP authentication
    $mail->SMTPAuth = true;

    // SMTP username 
    $mail->Username = '';
    // SMTP password 
    $mail->Password = '';

    // Enable TLS encryption (gmail setting)
    $mail->SMTPSecure = 'tls';  
    // TCP port to connect to (gmail setting) 
    $mail->Port = 587;
    $mail->From = 'a';
    $mail->FromName = 'a';
    // Add recipients
    $mail->addAddress($adminemail, $adminfullname);


    $mail->isHTML(true);   // Set email format to HTML

    $mail->Subject =  'Notification Of Stock Low';
    $mail->Body    = 'Dear '.$adminfullname.', <br><br> Your Shop Stock Are Low ,<br>Please Reorder Again,<br><br> Below are the Stock Low Details<br><br>'.$listofnotification.'<br><br>Thank you.';

    if(!$mail->send()) {
      echo 'Message could not be sent.';
      echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Maile Sent';
    }
}
else
{
    echo 'Quantity More than 5';
}

【讨论】:

  • 拒绝投票者请添加原因。所以我会纠正我的错误。
  • 我刚刚支持你反对反对票。我的印象是,有些人只是投反对票。
  • 不能工作,因为当我回显 $total 时会得到 0,所以会运行电子邮件功能。
【解决方案2】:
$queryNotification = "SELECT * from stock where stockQty <= :qua1 ";
$stmtNotification  = $conn->prepare($queryNotification);
$stmtNotification->bindParam(':qua1',$qua1);
$stmtNotification->execute();

$listofnotification = '';
$total = 0 ;
while ($queryNotRow = $stmtNotification->fetch()){

    $stockname     = $queryNotRow['stockName'];
    $stockquantity = $queryNotRow['stockQty'];
    $Category      = $queryNotRow['category'];
    $total =  $total + $stockquantity;
}

if($total <=5)
{
    $mail = new PHPMailer;

    // Set mailer to use SMTP 
    $mail->isSMTP();

    // Specify main SMTP servers 
    $mail->Host = 'smtp.gmail.com'; 
    // Enable SMTP authentication
    $mail->SMTPAuth = true;

    // SMTP username 
    $mail->Username = '';
    // SMTP password 
    $mail->Password = '';

    // Enable TLS encryption (gmail setting)
    $mail->SMTPSecure = 'tls';  
    // TCP port to connect to (gmail setting) 
    $mail->Port = 587;
    $mail->From = 'a';
    $mail->FromName = 'a';
    // Add recipients
    $mail->addAddress($adminemail, $adminfullname);


    $mail->isHTML(true);   // Set email format to HTML

    $mail->Subject =  'Notification Of Stock Low';
    $mail->Body    = 'Dear '.$adminfullname.', <br><br> Your Shop Stock Are Low ,<br>Please Reorder Again,<br><br> Below are the Stock Low Details<br><br>'.$listofnotification.'<br><br>Thank you.';

    if(!$mail->send()) {
      echo 'Message could not be sent.';
      echo 'Mailer Error: ' . $mail->ErrorInfo;
    } else {
        echo 'Maile Sent';
    }
}
else
{
    echo 'Quantity More than 5';
}

【讨论】:

  • $stockquantity 条件下,它只携带最后一条记录数。
  • @ChHong 你检查了吗??
  • 不能工作,因为当我回显 $total 时会得到 0,所以会运行电子邮件功能。
猜你喜欢
  • 2015-02-12
  • 1970-01-01
  • 2015-10-01
  • 2011-02-04
  • 2011-09-16
  • 2012-02-04
  • 1970-01-01
  • 2016-04-13
  • 1970-01-01
相关资源
最近更新 更多