你好,我正在尝试一些自制的php脚本,只是为了理解php编码。我创建了一个简单的订单系统。现在,我正在创建一个提交按钮,以便在我的邮箱中获得php表单(php自动创建的表单)。
这是在表中创建我的数据的脚本:
<?php //view users.php
// connect to the database
require_once 'script/login.php';
$query = "SELECT CONCAT(first_name) AS name, last_name, sex FROM users";
$result = @mysql_query ($query);
$sql = "SELECT last_name, sex, COUNT(*) AS aantal
FROM users
GROUP BY last_name, sex
ORDER BY aantal DESC";
$res = mysql_query($sql);
if($res===false) die($sql .' doet '. mysql_error());
if ($result) { //If it ran ok display the records
echo '<table align="center" cellpadding="5" cellspacing="5">
<tr><td align="left"><b>Broodje</b></td><td align="left"><b>Beleg</b></td><td align="left"><b>Hoeveelheid</b></td></tr>';
while($row=mysql_fetch_assoc($res)) {
echo '<tr><td align="left">' . $row['last_name'] . '</td><td align="left">' . $row['sex'] . '</td><td align="left">' . $row['aantal'];
}
$data = mysql_query("select count(1) as aantal from users");
$aantal = mysql_result($data, 0, 'aantal');
echo '<tr><td align="left"><strong>Totaal bestelling:</strong><td align="left"><td align="left">'.$aantal;
echo '</table>';
mysql_free_result ($result);
} else { //if it did not run ok
echo '<p class="error">The current users could not be retrieved. We apologise for any inconvienience.</p>'; //public message
echo '<p>' . mysql_error() . '<br/><br/> Query: ' . $query . '</p>'; //debugging message
}
mysql_close(); // Close database connection
?>
这个剧本很好用。但我想通过电子邮件发送。要发送电子邮件,我使用以下脚本:
<?php
$to = 'adress';
$subject = 'Broodje bestelling';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "'r'n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n";
// Put your HTML here
$message = file_get_contents('view_users.php');
// Mail it
mail($to, $subject, $message, $headers);
?>
我确实收到邮件,但我的邮件是空的,那么我如何在邮件中找到表格?
谢谢。
Before you mailed , can you echo your messasge to see if there is any error about file_get_contents.
<?php
$to = 'adress';
$subject = 'Broodje bestelling';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "'r'n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n";
// Put your HTML here
$message = file_get_contents('view_users.php');
echo $message;
// Mail it
//mail($to, $subject, $message, $headers);
?>
我仍然觉得要坚持基础知识点击此处
$message = file_get_contents('./view_users.php',true);
PHP的本地邮件函数通常很难使用。作为一个初学者,您可能会从PHPMailer和Swift Mailer库中受益。
您"不能"使用.php脚本作为消息,如果您想在表单提交后使用该文件的结果,则应将结果分配给一个变量(最好是array()),然后将该变量传递给将发送电子邮件的函数。
例如:
function sendEmail(array $message) {
$to = 'adress';
$subject = 'Broodje bestelling';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "'r'n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n";
// Put your HTML here
$message = implode('<br/>',$message);
// Mail it
return mail($to, $subject, $message, $headers);
}
如果消息发送正确,php-mail()函数将返回TRUE,因此您可以使用上述函数的返回值来查看电子邮件是否已发送。
请记住,这是一个未经测试的例子。