2015-10-13 27 views
-1

僅當數量低於5時,我如何才能發送電子郵件?下面的代碼將隨時發送電子郵件,我加載頁面。我怎樣才能達到這個限制?PHP - 僅當數量低於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 { 

     } 
+0

你寫的代碼是這樣的。在發送電子郵件時檢查數量時添加if()。 –

回答

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'; 
} 
+0

在條件'$ stockquantity'中,它只攜帶最後一條記錄數。 – Sachink

+0

@ChHong你檢查過了嗎? –

+1

不能工作,因爲當我echo $ total會得到0,所以會運行電子郵件功能。 –

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'; 
} 
+0

羽絨選民請加上原因。所以我會糾正我的錯誤。 – Sachink

+0

我剛剛向你反對反對票。我的印象是,有些人只是圍繞着投票。 – curls

+2

不能工作,因爲當我echo $ total會得到0,所以會運行電子郵件功能。 –