2013-10-30 19 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

相关问题