简体   繁体   中英

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

在此处输入图片说明

try this:

$string = "<table>";
while ($row = mysql_fetch_object($query)) { 
    $string .= "<tr>";
    foreach($row as $key => $value) {
        $string .= "<td>$key: $value</td>";
    }
    $string .= "</tr>";
}
$string .= "</table>";

it loops over your sql results, creates a table row for each found database row and a table col for each sql col (displaying the field name and value).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM