How do I email results of php mysql query in while loop?
I have created a file called 'userhistoryreport.php'. It will be setup to run once a day. The purpose of the file is to gather contact information and history for users who have interacted with the site for that particular day. The script works, but I am unsure of how to get the r开发者_如何学Cesults to send to email. The email should be formatted just like the page in the broswer - where each user is listed with their history. Any help is appreciated.
echo '<div style="padding: 0 20px;">';
echo '<span style="display: block; font-size: 22px; font-weight: bold; margin: 25px 0 -15px 0;"> User Activity on' . $website . 'for' . $date . '</span>';
//query the database for today's history
$result = mysql_query("SELECT * FROM user_history WHERE date = '$date' ORDER by name, time, title")
or die(mysql_error());
$old_user = '';
while($row = mysql_fetch_array( $result )) {
$new_user = $row['uid'];
if ($new_user != $old_user) {
echo '<br /><br /><hr />' . '<span style="font-size: 18px; font-weight: bold;">' . $row['name'] . '</span>' . '<br />' . $row['company'] . '<br />' . $row['email'] . '<br />' . $row['phone'] . '<br /><br />';
$old_user = $new_user;
}
echo '<ul><li>' . $row['time'] .
'<ul><li>' . $row['title'] . ' (<a href="' . $row['url'] . '">' . $row['url'] . '</a> )' . '</li></ul>' . '<br /><br />' .
'</li></ul>';
}
echo '</div>';
echo
'<div style="position: fixed; bottom: 0; height: 40px; width: 100%; margin: 20px 0 0 0; background: #000; color: #FFF; line-height: 40px;">' .
'Report generated ' . $date . ' ' . $time .
'</div>';
Straight from the mail() function in the php manual, try this;
Put all of your html into a string instead of echoing it immediately, by replacing all your echoes with something like this;
$message .= '<br /><br /><hr />' . '<span style="font-size: 18px; font-weight: bold;">' . $row['name'] . '</span>' . '<br />' . $row['company'] . '<br />' . $row['email'] . '<br />' . $row['phone'] . '<br /><br />';
$message .= '<ul><li>' . $row['time'] .
'<ul><li>' . $row['title'] . ' (<a href="' . $row['url'] . '">' . $row['url']. '</a> )' . '</li></ul>' . '<br /><br />' .
'</li></ul>';
To display the report, use
echo $message;
To mail the report, put this bit after the final closing tag to send the report with html.
// 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";
// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
// Mail it
mail('your@emailadress.com', 'Report generated ' . $date . ' ' . $time, $message, $headers);
For you coders out there, a useful tip for the above is to generate the table headers dynamically based on the result set.
$fieldArray = array_keys( mysql_fetch_array( mysql_query( $sQuery, $link), MYSQL_ASSOC));
//Build Table Headers
$report.= '<table><tr>';
//Spit out result column names
for ($i=0;$i<count($fieldArray);$i++) {
$report.= '<th><strong>'.$fieldArray[$i].'</strong></th>';
}
//close row
$report.= '<\tr>';
Then within your query you can use the columname AS "First Name" syntax and voila! :)
Shameless plug but I wrote a commercial PHP script for creating html emails based on mySQL queries here -> http://www.julian-young.com/mysqlreporter/ with html email, multiple colour schemes and it's very flexible for automatically generating multiple reports from queries.
精彩评论