Arun Kumaresh Arun Kumaresh -5 years ago 169
SQL Question

how to send the database data as email using php mailer?

I'm trying to send the database data as mail using php mailer. Email is sending properly but in the message I get only the last data in the database. What can I do to send all the data in the database.

I have tried the following code:

require 'PHPMailer/PHPMailerAutoload.php';
function Selectdata($table,$condition)
global $conn,$result,$selectarray,$rowcount;
$sql="SELECT * from ".$table." ".$condition." ";
$selectarray = array();
return $result;

// function for join
function outerjoin($query)
global $conn,$result,$selectoutter,$rowcount;
$selectoutter = array();

$mail = new PHPMailer;
$mail->Host = '';
$mail->SMTPAuth = true;
$mail->Username = '';
$mail->Password = 'xxxxxxxxxxxxx';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->SMTPDebug = 1;
$mail->setFrom('', 'Arun');
$mail->addAddress('', 'kumaresh');

$mail->IsHTML (true);
$mail->Subject = 'Messages From Forum';

foreach ($selectarray as $cid) {

$query="SELECT as forumname, as discussion,c.message,c.userid,d.firstname,,d.phone1 from mdl_forum as a inner join mdl_forum_discussions b on inner join mdl_forum_posts c on inner join mdl_user d on where a.course=".$cid['id']." and a.category=1 and from_unixtime(c.modified,'%Y-%m-%d')=CURDATE()";

foreach ($selectoutter as $fmsg) {
$mail->Body = '<p><b>Forum Name:</b>&nbsp;&nbsp;&nbsp;'.$fmsg['forumname'].'</p>';
$mail->Body.= '<p><b>Discussion Name:</b>&nbsp;&nbsp;&nbsp;'.$fmsg['discussion'].'</p>';
$mail->Body.= '<p><b>Message:</b>&nbsp;&nbsp;&nbsp;'.$fmsg['message'].'</p>';

$send = $mail->Send();
if (!$send) {
echo 'Message could not be sent.';
// echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';

Answer Source

For each row of the query result, you're truncating the body of the mail:

$mail->Body=  '<p><b>Forum [...]

Set $mail->Body before the loop, and then add the rows info with .=

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download