2013-12-09 66 views
0

你好我嘗試了一些自制的PHP腳本只是爲了瞭解PHP編碼。我創建了一個簡單的訂單系統。現在我創建一個提交按鈕來獲取我的郵箱中的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

mysql_ *函數已被棄用.. – Tredged

回答

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); 

?> 
+0

Lmao ..認真迴應它? – Tredged

0

我還是覺得與基本Click here

$message = file_get_contents('./view_users.php',true);

0

PHP內建的郵件功能往往是麻煩的工作。作爲一名初學者,您可以從PHPMailer和Swift Mailer庫中獲益。

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,如果消息正確發送,所以您可以用上面的函數的返回值,看看是否電子郵件被髮送。

請記住,這是一個未經測試的例子。