【问题标题】:Php MySQL Query reult in HTML Table send via email通过电子邮件发送 HTML 表中的 PHP MySQL 查询结果
【发布时间】:2018-12-21 16:13:00
【问题描述】:

有人可以帮我解决这个问题吗?我在 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);


?> 

实际导出时的样子

【问题讨论】:

    标签: php mysql html-table notifications html-email


    【解决方案1】:

    试试这个:

    $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 col 创建一个表 col(显示字段名称和值)。

    【讨论】:

      【解决方案2】:

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

      尝试格式化$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";
      

      试试上面的代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多