php mysql查询在html表中通过电子邮件发送

weylhg0b  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(234)

谁能帮我一下吗。我在php中构建了一个脚本,可以通过电子邮件导出mysql搜索查询。一切都很好,除了数据是导出的,看起来像字符串,有可能有相同的数据,但在基本的html表,所以它看起来更好?请看一页它是如何在收到电子邮件时。提前感谢:

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);

?>

实际输出看起来像收到时

nmpmafwu

nmpmafwu1#

试试这个:

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

它循环处理sql结果,为每个找到的数据库行创建一个表行,为每个sql列创建一个表列(显示字段名和值)。

cuxqih21

cuxqih212#

你必须格式化 $string 根据html表或所需的html代码在循环中设置变量。因为变量是纯字符串,所以在电子邮件中也是纯字符串。
尝试格式化 $string 变量。

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

同时编辑 $message 变量

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

试试上面的代码。

相关问题