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

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

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

  1. while ($row = mysql_fetch_object($query)) {
  2. $string .= ""."Product Name: ".$row->product_name."".""."Product Code: ".$row->product_code."".""."Product Color: ".$row->product_color_name."".""."Product Package/Size: ".$row->package_name."";
  3. }
  4. //email parameters
  5. $to = 'email' . ', '; // note the comma
  6. $to .= '';
  7. $subject = "Low Stock $date_o - $date_a";
  8. // To send HTML mail, the Content-type header must be set
  9. $headers = 'MIME-Version: 1.0' . "\r\n";
  10. $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
  11. $headers .= 'From: Low Stock Notification' . "\r\n";
  12. $message = "$e_message
  13. Dear Admin,<br><br>
  14. Stock of some or one product(s) is low, see the detail below: <br><br>
  15. $string
  16. $footer";
  17. mail($to,$subject,$message,$headers);
  18. ?>

实际输出看起来像收到时

nmpmafwu

nmpmafwu1#

试试这个:

  1. $string = "<table>";
  2. while ($row = mysql_fetch_object($query)) {
  3. $string .= "<tr>";
  4. foreach($row as $key => $value) {
  5. $string .= "<td>$key: $value</td>";
  6. }
  7. $string .= "</tr>";
  8. }
  9. $string .= "</table>";

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

cuxqih21

cuxqih212#

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

  1. $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 变量

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

试试上面的代码。

展开查看全部

相关问题