Search code examples
phpmysqlhtml-tablenotificationshtml-email

Php MySQL Query reult in HTML Table send via email


Can somebody help me with this. I've build script in php to export mysql search query via email. Everything is working fine, except data is exported and looks like string, is it possible to have same data but in basic html table so it looks better? Please see atatched immage how it lloks when receiving email. Thank you in advance:

while ($row = mysql_fetch_object($query)) { 
$string .= ""."Product Name: ".$row->product_name."".""."Product Code: ".$row->product_code."".""."Product Color: ".$row->product_color_name."".""."Product Package/Size: ".$row->package_name."";
}



//email parameters

$to  = 'email' . ', '; // note the comma
$to .= '';

$subject = "Low Stock $date_o - $date_a";

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Low Stock Notification' . "\r\n";

$message = "$e_message

Dear Admin,<br><br>

Stock of some or one product(s) is low, see the detail below: <br><br>

$string
$footer";

mail($to,$subject,$message,$headers);


?> 

actual export looks like when received

enter image description here


Solution

  • You have to format the $string variable in the loop as per the html table or in your desired html code. as your variable is plain string so it is also plain string in email.

    try to format $string variable.

    $string .= "<tr><td>"."Product Name: ".$row->product_name."</td></tr>"."<tr><td>"."Product Code: ".$row->product_code."</td></tr>"."<tr><td>"."Product Color: ".$row->product_color_name."</td></tr>"."<tr><td>"."Product Package/Size: ".$row->package_name."</td></tr>";
    

    also edit the $message variable

    $message = "$e_message
    
        Dear Admin,<br><br>
    
        Stock of some or one product(s) is low, see the detail below: <br><br>
        <table>
           $string
        </table>
         $footer";
    

    try above code.