通过电子邮件发送动态表

时间:2012-09-10 14:45:32

标签: php mysql

我正在处理这个需要发送特定报告的Web应用程序。该报告是动态的,但它采用表格格式。我现在希望通过电子邮件将整个表格通过电子邮件发送给某人。我有什么方法可以这样做吗?

php脚本

    <?php
    $con = mysql_connect("localhost", "root", "");
    if (!$con) 
      {
      die('Could not connect: ' . mysql_error());
      }

    mysql_select_db("mydb", $con);
    $input = $_POST['id'];
    $query = mysql_query("SELECT * FROM table WHERE ID = '$input'");

    echo "<table border='5' align=center>
    <tr>
    <th>ID</th>
    <th>Type</th>
    <th>Setting</th>
    <th>Value</th>
    <th>Actual</th>
    </tr>";

    while($row = mysql_fetch_array($query)) 
        { 
         echo "<tr>";
      echo "<td>" . $row['ID'] . "</td>";
      echo "<td>" . $row['Type'] . "</td>";
      echo "<td>" . $row['Setting'] . "</td>";
      echo "<td>" . $row['Value'] . "</td>";
      echo "<td>" . $row['Actual'] . "</td>";
      echo "</tr>"; 

            if($row['Value'] != $row['Actual']) 
                { 
                echo "<td bgcolor='#FD2911'>" . "X" . "</td>";
                } 
            else 
                { 
                echo "<td bgcolor='#1CDC15'>" . "O" . "</td>";
                } 
        }
    echo "<br>";

    mysql_close($con);
    ?>

html代码

    <form action="query1.php" method="POST">
    Enter the choice: <input type="varchar" name="id" /><br />
    <input type="submit" value="Audit" />
    </form> 

2 个答案:

答案 0 :(得分:1)

你连接整个表格:

$table = '';
$table .= "<table border='5' align=center>
<tr>
<th>ID</th>
<th>Type</th>
<th>Setting</th>
<th>Value</th>
<th>Actual</th>
</tr>";

while($row = mysql_fetch_array($query)) 
    { 
     $table .= "<tr>";
  $table .= "<td>" . $row['ID'] . "</td>";
  $table .= "<td>" . $row['Type'] . "</td>";
  $table .= "<td>" . $row['Setting'] . "</td>";
  $table .= "<td>" . $row['Value'] . "</td>";
  $table .= "<td>" . $row['Actual'] . "</td>";
  $table .= "</tr>"; 

        if($row['Value'] != $row['Actual']) 
            { 
            $table .= "<td bgcolor='#FD2911'>" . "X" . "</td>";
            } 
        else 
            { 
            $table .= "<td bgcolor='#1CDC15'>" . "O" . "</td>";
            } 
    }
$table .= "</table><br>";

mail('to@example.com', 'My Subject', $table);

答案 1 :(得分:0)

如果你想要比表更多,你可以使用输出缓冲;

<?php
ob_start();

# all your code/page/table(s) with echos and all

$toBeSent = ob_get_contents();
ob_end_clean();
?>

然后通过电子邮件发送ob_start和ob_end_clean之间的全部内容,方法是用$ toBeSent替换Mihai帖子的mail()行中的$ table。