如何在一封电子邮件中合并数据


How to merge data in one single email?

我正在使用Sendmail进行PHP项目。电子邮件发送成功,但将逐个发送。我的编码流程是它将检查数据库,然后如果项目的值小于我设置的值,它将发送电子邮件通知用户。

我想将所有项目合并到一封电子邮件中,以便用户不会收到太多电子邮件来发送相同的通知。我希望你明白我的意思。问题是,我不知道如何使用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       = 'some@email.com';
$subject  = 'Testing sendmail.exe';
$message  = 'The Following Your Product Expired. Product Code:'.$message2;
$headers  = 'From: some@email.com' . "'r'n" .
        'Reply-To: some@email.com' . "'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       = 'some@email.com';
$subject  = 'Notification of less on stock';
$message = 'less than 5';
$headers  = 'From: some@email.com' . "'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";
  } 
?>

您正在 While 循环中循环mail()函数。通过调整如下代码,您可以发送1封电子邮件。(当然你可以微调内容)

$to       = 'some@email.com';
$subject  = 'Testing sendmail.exe';
$message = '';
$headers  = 'From: some@email.com' . "'r'n" .
        'Reply-To: some@email.com' . "'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";

旁注:

  • 没有单位0 CSS 值
  • 不要使用已弃用的mysql_*函数
  • 您正在发送HTML电子邮件,但正文支离破碎且不完整
  • 您正在以字符集ISO-8859-1发送电子邮件,您应该确保内容在字符集中,例如没有Unicode字符
  • 包含.exe的电子邮件内容在电子邮件服务器中通常被视为垃圾邮件