-2

我有一个能够从 gmail 帐户发送电子邮件的网络服务器。我有一个页面可以根据 SQL 查询等生成动态表单。然后,我有一个表单可以输入您的电子邮件地址,它会重定向到另一个显示“已发送消息”的页面,该页面上的代码会发送电子邮件。但是,如何更改电子邮件的内容,使其成为上一页生成的表格?我不想重新创建整个表并将其设置为变量,因为我认为有一种更有效的方法来做到这一点。任何帮助,将不胜感激。

“search.php”(此页面根据前一页面执行的查询生成表)

    <html>
<head>
<style type="text/css">
table {
background-color: #C0C0C0;
}

th{
width: 150px;
text-align:center;
border-style: solid;
border-width: 2px;
border-color: black;
background-color: #008080;
font-family: Helvetica;
}
td {
border-style: solid;
border-width: 2px;
border-color: black;
font-family: Helvetica;
background-color: #FFFF00;
text-align:center;
}
body {
background-color:#1C2932;
}

h1 {
font-family: Helvetica;
font-size: 24px;
color: #989898;
}

p {
font-family: Helvetica;
font-size: 18px;
color: #989898;
}

</style>
</head>
<body>

<?php
include 'tablegen.php';

if(isset ($_POST['term'])) {
$x = $_POST['term'];
     connect($x);
     tableGen();
}//end main if
//area 52 what is going on...

echo "<form action='email.php' method = 'post'>";
echo "<p><b>Do you want this in an email?</b></p>";
echo "<input type='text' name='send'>";
echo "<input type='submit' name='submit' value='Send!' />";
echo "</form>";

?>
<br></br>
<form method="LINK" action="landing.php">
<input type="submit" value="Go Back!">
</form>

</body>
</html>

“email.php”(此页面实际发送电子邮件)

    <html>
<head>
<style>
body {
background-color:#1C2932;
}
p {
font-family: Helvetica;
font-size: 18px;
color: #989898;
}

</style>
</head>
<?php

$email = $_POST['send'];

$headers = array(
'From: summitmathguide@gmail.com',
'Content-Type: text/html',
'Content-Type: text/css'
);



mail($email,'HTML Email','I want to send an HTML table!!!',implode("\r\n",$headers));
echo "<p>Email Sent!</p>";
?>
</html>

“tablegen.php”(显示表格的功能)——有效!!!

    <?php
function connect(){

    mysql_connect("localhost","root","water123") or die ('Error Reaching Database');
    mysql_select_db("MathGuide");


}   
    //Area  51, idk what I'm doing
function tableGen($x) {
$term=$x;
$sql = mysql_query("select * from student_info where ID like '%$term%'");
echo "<h1>STUDENT DATA for ID: $search</h1>";
echo "<table>";
echo "<tr>
<th>ID</th>
<th>Project</th>
<th>Starter Project</th>
<th>Course</th>
<th>KDs Completed in your Course</th>
<th>Projects Completed</th>
<th>Project 1</th>
<th>P1KD1</th>
<th>P1KD2</th>
<th>P1KD3</th>
<th>P1KD4</th>
<th>P1KD5</th>
<th>Project 2</th>
<th>P2KD1</th>
<th>P2KD2</th>
<th>P2KD3</th>
<th>P2KD4</th>
<th>P2KD5</th>
<th>Project 3</th>
<th>P3KD1</th>
<th>P3KD2</th>
<th>P3KD3</th>
<th>P3KD4</th>
<th>P3KD5</th>
<th>Project 4</th>
<th>P4KD1</th>
<th>P4KD2</th>
<th>P4KD3</th>
<th>P4KD4</th>
<th>P4KD5</th>
</tr>";

while ($row = mysql_fetch_array($sql))
{
echo "<tr><td>";
echo $row['ID'];
echo "</td><td>";
echo $row['Project'];
echo "</td><td>";
echo $row['Starter Project'];
echo "</td><td>";
echo $row['Course'];
echo "</td><td>";
echo $row['KDs completed in your course'];
echo "</td><td>";
echo $row['Projects Completed'];
echo "</td><td>";
echo $row['Project 1'];
echo "</td><td>";
echo $row['P 1 KD 1'];
echo "</td><td>";
echo $row['P 1 KD 2'];
echo "</td><td>";
echo $row['P 1 KD 3'];
echo "</td><td>";
echo $row['P 1 KD 4'];
echo "</td><td>";
echo $row['P 1 KD 5'];
echo "</td><td>";
echo $row['Project 2'];
echo "</td><td>";
echo $row['P 2 KD 1'];
echo "</td><td>";
echo $row['P 2 KD 2'];
echo "</td><td>";
echo $row['P 2 KD 3'];
echo "</td><td>";
echo $row['P 2 KD 4'];
echo "</td><td>";
echo $row['P 2 KD 5'];
echo "</td><td>";
echo $row['Project 3'];
echo "</td><td>";
echo $row['P 3 KD 1'];
echo "</td><td>";
echo $row['P 3 KD 2'];
echo "</td><td>";
echo $row['P 3 KD 3'];
echo "</td><td>";
echo $row['P 3 KD 4'];
echo "</td><td>";
echo $row['P 3 KD 5'];
echo "</td><td>";
echo $row['Project 4'];
echo "</td><td>";
echo $row['P 4 KD 1'];
echo "</td><td>";
echo $row['P 4 KD 2'];
echo "</td><td>";
echo $row['P 4 KD 3'];
echo "</td><td>";
echo $row['P 4 KD 4'];
echo "</td><td>";
echo $row['P 4 KD 5'];
echo "</td></tr>";
}

echo "</table>";
}//end main if
4

1 回答 1

1

您应该将生成表的代码移动到两者都可以包含的 PHP 文件中,将连接到 mysql 的代码、执行查询的代码和生成表的代码放在不同的函数中。

就我个人而言,我会通过使用模板引擎创建一个字符串来为电子邮件生成表格,但要保留基于“echo”的代码,您可以使用ob_start ,并使用ob_get_clean将内容获取到变量中。然后,您将在第二个参数中发送此变量的内容,mail()而不是读取“HTML 电子邮件”的字符串。

还要注意我在评论中指出的安全 问题

于 2013-04-30T01:41:52.530 回答