我爲我的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";
}
?>
把郵件功能的同時,外循環。 – BKM
*旁註:*停止使用已棄用的'mysql_ *'函數。改用MySQLi或PDO。 – Raptor