从mysql检索数据并通过电子邮件发送

时间:2012-01-17 18:57:55

标签: php mysql email select

我有一个php页面,它显示了mysql数据库中每个用户的类调度数据,如下所示:

$result = mysql_query($sql);

echo "<table border='0'>
<thead>
    <tr>
        <th>Class Link</th>
        <th>Student Skype ID</th>
        <th>Student Name</th>
        <th>Faculty Name</th>
        <th>Class Name</th>
        <th>USA Date (DD/MM/YY)</th>
        <th>USA Start Time</th>
        <th>USA End Time</th>
        <th>India Date (DD/MM/YY)</th>
        <th>India Start Time</th>
        <th>India End Time</th>
        <th>Status</th>
    </tr>
</thead>";

while($row = mysql_fetch_array($result))
{
    echo "<tbody>";
        echo "<tr>";
            echo "<td><a href=\"".$row['class_link']."\" target='blank'>Start Class</a></td>";
            echo "<td>" . $row['skype_id'] . "</td>";
            echo "<td>" . $row['student_name'] . "</td>";
            echo "<td>" . $row['faculty_name'] . "</td>";
            echo "<td>" . $row['subject_name'] . "</td>";
            echo "<td>" . $row['date'] . "</td>";
            echo "<td>" . $row['starttime'] . "</td>";
            echo "<td>" . $row['endtime'] . "</td>";
            echo "<td>" . $row['facdate'] . "</td>";
            echo "<td>" . $row['facstarttime'] . "</td>";
            echo "<td>" . $row['facendtime'] . "</td>";
            echo "<td>" . $row['status'] . "</td>";
    echo "</tr>";
    echo "</tbody>";
}
echo "</table>";

现在我有以下php电子邮件脚本

$to = 'support@xyz.com';
$subject = "Your Class Schedule";
$message = **'HOW CAN I PUT THE DATA FROM MYSQL HERE?'**;
$headers = 'From: REPLY@XYZ.COM' . "\r\n" .
$headers = "MIME-Version: 1.0" . "\r\n" .
$headers = "Content-type:text/html;charset=iso-8859-1" . "\r\n" .
            'Reply-To: REPLY@XYZ.COM' . "\r\n" .
           'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);

正如上面代码的$ message中所提到的,我想将这些检索到的数据放入$ message部分,以便它进入邮件..

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:3)

取决于您的mysql数据的来源及其存储方式,您无法检索它并将其添加到$ message变量中吗?

<?PHP
    $query = "SELECT * FROM yourtable WHERE youridentifier = 'unique'"
    $result = mysql_query($query) or die(mysql_error());
    while ($row = mysql_fetch_array($result)) {
        $content = $row['field with email content']
        // or if there is more than one field
        $content2 = $row['field with more email content']
    }
    // then you can create the "message" as you wish
    $message = "Greetings ".$content.",

        you are receiving this email because of blah. ".$content2."

        Thank you,
        code guy"
    // Then you can still use $message as your variable
}
?>

将其格式化为(HTML或不等)..并邮寄。

多行的

会稍微改变一下......

<?PHP
    // give your message the starting string
    $message = 'Greetings,

        you are receiving this email as an invoice as follows:
        <table style="width: 80%;">
            <tr>
                <td>Description</td>
                <td>Cost</td>
                <td>Weight</td>
                <td>Color</td>
            </tr>
    '
    $query = "SELECT * FROM yourtable WHERE youridentifier = 'unique'"
    $result = mysql_query($query) or die(mysql_error());
    while ($row = mysql_fetch_array($result)) {
        $message .= "        <tr>";
        $message .= "            <td>".$row['itemdescription']."</td>";
        $message .= "            <td>".$row['cost']."</td>";
        $message .= "            <td>".$row['shippingweight']."</td>";
        $message .= "            <td>".$row['color']."</td>";
        $message .= "        </tr>";
    }
    // then update the message with the ending
    $message .= "
        </table>

        Thank you,
        code guy"
    // Then you can still use $message as your variable
}
?>

如果你使用的是HTML格式的电子邮件,那就是压力,否则它只是格式化文本。