0

I am using Sendmail for my PHP Project. The email sent successfully but it will sent one by one. The flow of my coding is it going to check the database, then if the value of the item is less than what I set, it will send email to notify user.

I would like to combine all the item into one single email, so that user did not receive too many email for the same notification. I hope you get what I mean. The thing is, I dont know how to combine the data into one single email using Sendmail. Can someone show where should I change to do it ?

This is my code :

<?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";

  }     

?>

EDITED :

<?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";

  } 
?>
AnFi
  • 10,493
  • 3
  • 23
  • 47
user2810332
  • 29
  • 2
  • 9

1 Answers1

0

You are looping the mail() function within the While Loop. By adjusting the codes like below, you can send in 1 email. (Of course you can fine tune the contents)

$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";

Sidenotes:

  • CSS value 0 has no unit
  • do not use deprecated mysql_* functions
  • you are sending HTML email, but the body is fragmented & incomplete
  • you are sending email in character set ISO-8859-1, you should ensure the contents are within the character set, for example no Unicode characters
  • email contents containing .exe is usually treated as spam in email servers
Raptor
  • 53,206
  • 45
  • 230
  • 366
  • Thanks for your answer ! It's working now. But I have this problem when I try to change the code. Let say if the data is more than 5, the code will exit automatically without sending any email. I've tried but it is not working. Can you please help me ? See my edited question. – user2810332 Oct 31 '13 at 03:22
  • It's your logic; your `mail()` is in `else` case. – Raptor Oct 31 '13 at 04:19