user2810332
user2810332

Reputation: 29

How to merge data in one single email?

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       = '[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";

  }     

?>

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       = '[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";

  } 
?>

Upvotes: 0

Views: 92

Answers (1)

Raptor
Raptor

Reputation: 54212

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       = '[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";

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

Upvotes: 0

Related Questions