使用php将表数据发送到电子邮件

时间:2015-11-19 02:30:50

标签: php html mysql sql email

我尝试将包含动态表内容的电子邮件发送到电子邮件。 我尝试了几种方法,但到目前为止它们还没有对我有用。如果我尝试输入php代码,我会在加载时得到一个空白页。

if(isset($_POST['email'])) { 
        $to = "myemailg@hotmail.com";
        $subject = "Parking Log";
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";           
        $message = "
        <html>
        <head>
        <title>Car Log</title>
        </head>
        <body>
        <?php //This prints out the car log data
        echo "<h3>Log of ".$_SESSION['date']." </h3>";
        $sql = "SELECT * FROM carLog";
        $result = $databaseConnection->query($sql);
        echo "<table class='TFtable' border='1' style='width':100%>"; //starts the table tag
        echo "<tr><td>Name</td><td>Vehicle</td><td>Licence Plate</td><td>In</td><td>Out</td><td>Comments</td></tr>"; //sets headings
        while($row = $result->fetch_assoc()) { //loops for each result
        echo "<tr><td>".$row['name']."</td><td>".$row['vehicle']."</td><td>".$row['plate']. "</td><td>".$row['inTime']."</td><td>".$row['outTime']."</td><td>".$row['comments']."</td>";
        }
        echo "</table>"; //closes the table
        ?>
        </body>
        </html>
        ";
        $message = wordwrap($message, 70);    
        mail($to,$subject,$message,$headers);
    }

我在页面上显示的内容与我在body标签中的代码相同,而且工作正常。我只是希望通过电子邮件发送我正在显示的所有内容。但因为页面甚至不会加载。

1 个答案:

答案 0 :(得分:0)

不知道它是否有效但寻找微小的变化

<?php
    if(isset($_POST['email'])) { 
            $to = "myemailg@hotmail.com";
            $subject = "Parking Log";
            $headers = "MIME-Version: 1.0" . "\r\n";
            $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";           
            $message = "
            <html>
            <head>
            <title>Car Log</title>
            </head>
            <body>";
            $body = "<h3>Log of ".$_SESSION['date']." </h3>";

            $sql = "SELECT * FROM carLog";
            $result = $databaseConnection->query($sql);

            $body .= "<table class='TFtable' border='1' style='width':100%>"; //starts the table tag
            $body .= "<tr><td>Name</td><td>Vehicle</td><td>Licence Plate</td><td>In</td><td>Out</td><td>Comments</td></tr>"; //sets headings
            while($row = $result->fetch_assoc()) { //loops for each result
            $body .= "<tr><td>".$row['name']."</td><td>".$row['vehicle']."</td><td>".$row['plate']. "</td><td>".$row['inTime']."</td><td>".$row['outTime']."</td><td>".$row['comments']."</td>";
            }
            $body .= "</table>"; //closes the table
            $message .= $body . "
            </body>
            </html>";
            $message = wordwrap($message, 70);    
            mail($to,$subject,$message,$headers);
        }