2013-10-30 23 views
0

我爲我的PHP項目使用Sendmail。郵件發送成功,但會一個一個發送。我的編碼流程是檢查數據庫,如果該項目的價值低於我設定的值,它將發送電子郵件通知用戶。如何合併單個電子郵件中的數據?

我想將所有的項目合併到一個單一的電子郵件,以便用戶沒有收到太多的電子郵件相同的通知。我希望你明白我的意思。問題是,我不知道如何使用Sendmail將數據合併到一個電子郵件中。有人可以顯示我應該改變的地方嗎?

這是我的代碼:

<?php 

include 'MasConnectDB.php'; 

$sql = "SELECT Item, Available FROM accessories_other"; 
$result = mysql_query($sql) or die('Query failed.'.mysql_error()); 

while($row = mysql_fetch_array($result)) 
{ 
$Item = $row['Item']; 
$Available = $row['Available']; 


if($row['Available'] <= 5){ 

    $message2 = $message3." <div style='margin:30px 0px;'> 
         $Item<br /></div>"; 

    } 

$to  = '[email protected]'; 
$subject = 'Testing sendmail.exe'; 
$message = 'The Following Your Product Expired. Product Code:'.$message2; 
$headers = 'From: [email protected]' . "\r\n" . 
     'Reply-To: [email protected]' . "\r\n" . 
     'MIME-Version: 1.0' . "\r\n" . 
     'Content-type: text/html; charset=iso-8859-1' . "\r\n" . 
     'X-Mailer: PHP/' . phpversion(); 
if(mail($to, $subject, $message, $headers)) 
    echo "Email sent"; 
else 
    echo "Email sending failed"; 

    }  

?> 

編輯:

<?php 

include 'MasConnectDB.php'; 

$sql ="SELECT TC_Status FROM thin_client WHERE TC_Status ='Available'"; 
$result = mysql_query($sql) or die('Query failed. ' . mysql_error()); 

while($row = mysql_fetch_array($result)) { 
$TC_STatus = $row['TC_Status']; 

if($result > 5){ 
echo "Thin client is more than 5"; 
    } 

else 

$to  = '[email protected]'; 
$subject = 'Notification of less on stock'; 
$message = 'less than 5'; 
$headers = 'From: [email protected]' . "\r\n" . 
    'MIME-Version: 1.0' . "\r\n" . 
    'Content-type: text/html; charset=iso-8859-1' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 
    if(mail($to, $subject, $message, $headers)) 
echo "Email sent"; 
    else 
echo "Email sending failed"; 

    } 
?> 
+1

把郵件功能的同時,外循環。 – BKM

+0

*旁註:*停止使用已棄用的'mysql_ *'函數。改用MySQLi或PDO。 – Raptor

回答

0

你循環While循環內mail()功能。通過調整下面的代碼,您可以發送1封電子郵件。 (當然,你可以微調的內容)

$to  = '[email protected]'; 
$subject = 'Testing sendmail.exe'; 
$message = ''; 
$headers = 'From: [email protected]' . "\r\n" . 
     'Reply-To: [email protected]' . "\r\n" . 
     'MIME-Version: 1.0' . "\r\n" . 
     'Content-type: text/html; charset=iso-8859-1' . "\r\n" . 
     'X-Mailer: PHP/' . phpversion(); 
while($row = mysql_fetch_array($result)) { 
    if($row['Available'] <= 5){ 
    $Item = $row['Item']; 
    $message .= "<div style='margin:30px 0;'>$Item<br /></div>"; 
    } 
} 
if(mail($to, $subject, $message, $headers)) 
    echo "Email sent"; 
else 
    echo "Email sending failed"; 

圖片的標題說明:

  • CSS值0沒有單位
  • 不使用過時mysql_*功能
  • 您發送HTML電子郵件,但身體是零散的&不完整
  • 你正在發送字符集ISO-8859-1的電子郵件,你應該確保內容s爲字符集內,例如沒有Unicode字符含.exe
  • 郵件內容通常被視爲垃圾郵件的電子郵件服務器
+0

感謝您的回答!現在正在工作。但是當我嘗試更改代碼時,我遇到了這個問題。假設數據超過5個,代碼將自動退出而不發送任何電子郵件。我試過了,但它不起作用。你能幫我麼 ?看到我編輯的問題。 – user2810332

+0

這是你的邏輯;你的'mail()'在'else'的情況下。 – Raptor

相關問題