我正在为我的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 = '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";
}
?>
答案 0 :(得分:0)
您正在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
没有单位mysql_*
函数.exe
的电子邮件内容通常在电子邮件服务器中被视为垃圾邮件