Hello im trying out some homemade php scripts just to understand PHP coding. I create a simple order system. Now i'm creating a submit button to get the php form (what have been automatic created by php) in my mail box.
This is the script what create my data in a table:
<?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
?>
This script works fine. But i want to send it in a email. To send a email I use this script:
<?php
$to = 'adress';
$subject = 'Broodje bestelling';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "
";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";
// Put your HTML here
$message = file_get_contents('view_users.php');
// Mail it
mail($to, $subject, $message, $headers);
?>
I do receive mail but my the message is empty, so how do i get the table in my message?
Thanks in forward.
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' . "
";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";
// Put your HTML here
$message = file_get_contents('view_users.php');
echo $message;
// Mail it
//mail($to, $subject, $message, $headers);
?>
I still feel going with basics Click here
$message = file_get_contents('./view_users.php',true);
PHP's native mail function is often troublesome to work with. Being a beginner you may benefit from looking into PHPMailer and Swift Mailer libraries.
You "cannnot" use a .php script as message, if you want to use the results of that file after a form submit you should assign the result to a variable (preferably an array()) and then pass that variable to a function that will send the email.
For instance:
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' . "
";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";
// Put your HTML here
$message = implode('<br/>',$message);
// Mail it
return mail($to, $subject, $message, $headers);
}
The php mail() function return TRUE if the message was sent correctly, so you can use the return value of the above function to see if the email was sent.
Keep in mind that is an untested example.