从MySQL中检索数据,并通过电子邮件发送
问题描述:
我有一个显示课程表的数据从mysql数据库中的每个用户这样的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 = '[email protected]';
$subject = "Your Class Schedule";
$message = **'HOW CAN I PUT THE DATA FROM MYSQL HERE?'**;
$headers = 'From: [email protected]' . "\r\n" .
$headers = "MIME-Version: 1.0" . "\r\n" .
$headers = "Content-type:text/html;charset=iso-8859-1" . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
正如在上面的代码的$消息中提到的,我想把这个检索的数据放到$ message部分,以便它在邮件中。
任何人都可以帮忙吗?
答
取决于你的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格式(不包括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
}
?>
这pressumption是,如果你使用的是HTML格式的电子邮件,否则只会被格式化文本。
你已经用'mysql_fetch_array()'获取了你的行。只需连接或插入变量:'$ message ='一些文本{$ row ['col_from_db']}“;' – 2012-01-17 18:59:42
Michael,我运行搜索时弹出多行,如何将它们全部输入消息部分? +我也想保持表格格式完整,当它在电子邮件中.. – ABI 2012-01-17 19:01:25