您好我试用一些自制的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);
?>
我确实收到了邮件,但我的邮件是空的,所以如何在邮件中找到该表?
谢谢你。
答案 0 :(得分:1)
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);
?>
答案 1 :(得分:0)
我仍然觉得有基础Click here
$message = file_get_contents('./view_users.php',true);
答案 2 :(得分:0)
PHP的本机邮件功能通常很麻烦。作为初学者,您可以从查看PHPMailer和Swift Mailer库中受益。
答案 3 :(得分:0)
你“不能”使用.php脚本作为消息,如果要在表单提交后使用该文件的结果,则应将结果分配给变量(最好是数组()),然后将该变量传递给一个将发送电子邮件的功能。
例如:
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,因此您可以使用上述函数的返回值来查看电子邮件是否已发送。
请记住,这是一个未经测试的例子。