PHP - 仅在数量低于5时发送电子邮件

时间:2015-10-13 06:14:06

标签: php html email pdo sendmail

如果数量少于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 {

      }

2 个答案:

答案 0 :(得分:0)

$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';
}

答案 1 :(得分:0)

$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';
}