Hope there is someone out there that can help me. I am playing around with PHP mailer to send out my daily production reports for my machines (Just to make my life easier obviously) but every mail i send only returns "1" in the message body. Dont know where i went wrong if anyone can assist.
Here is my PHPMailer code:
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(true);
$mail->Host = 'smtp.gmail.com:465';
$mail->SMTPAuth = true;
$mail->Username = 'XXXXXXX';
$mail->Password = 'XXXXXXX';
$mail->SMTPSecure = 'ssl';
$mail->isHTML(true);
$mail->From = 'XXXXXXX';
$mail->FromName = 'Automated Reporting';
$mail->addAddress('XXXXXXX@XXXXXXX');
$mail->addReplyTo('XXXXXXX@XXXXXXX');
$mail->isHTML(true);
$mail->Subject = 'Daily Production Report ';
$mail->Body = include_once('./scripts/Daily.php');
$mail->SMTPDebug = 0;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
When i run this script it returns the values that i need it to (daily.php) and displays correctly on my screen and i also get a message that the mail has been sent. But the mail i am receiving just has "1" in the message body, nothing else.
Contents of DAILY.php
<?php
$mydate=getdate(date("U"));
//OPEN Table for the production report
echo "<h1> ".$mydate['weekday'] , " " , $mydate['month'] , " " , $mydate['mday'], " " , $mydate['year']." :: " , " 311 Warehouse" , "</h1>";
echo "<table width=100% border=1>";
echo "<tr>";
echo "<th width=124>Shift / Line</th>";
echo "<th width=124>Handset</th>";
echo "<th width=124>Handset EOL</th>";
echo "<th width=124>Wallet 1</th>";
echo "<th width=124>Wallet 2</th>";
echo "<th width=124>Wholesale</th>";
echo "<th width=124>Wholesale EOL</th>";
echo "<th width=124>Blister</th>";
echo "<th width=124>Blister EOL</th>";
echo "</tr>";
$SCADA = mysqli_connect("XXXXXXX","XXXXXXX","XXXXXXX","XXXXXXX");
//MYSQL Connection Error
if (mysqli_connect_errno()) {
printf("Connect failed: %s
", mysqli_connect_error());
exit();
}
$values = array();
//311 Working Queries
echo "<tr>";
echo "<th width=124>DayShift : </th>";
//Handset Kitting Totals
$query = "select count(id) from HandsetKit_T where createdDate between '2014-06-23 05:00' and '2014-06-23 14:00';";
$result = $SCADA->query($query) or die($SCADA->error.__LINE__);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<td width=124>", stripslashes($row['count(id)']);
}
}
else {
echo 'NO RESULTS';
}
//Handset Kitting EOL Totals
$query = "SELECT CONCAT
(
(SELECT COUNT(p.id) as 'PalletS'
FROM Pallet_T p, PalletNotify_T pn
WHERE p.id = pn.`palletID`
AND p.createdDate BETWEEN '2014-06-23 05:00' and '2014-06-23 14:00'
AND pn.esbStatus = '3'
AND p.mtnAssetID NOT LIKE '11' AND p.mtnAssetID NOT LIKE '12'
) , ' / ' ,
(SELECT COUNT(p.id) as 'PalletF'
FROM Pallet_T p, PalletNotify_T pn
WHERE p.id = pn.`palletID`
AND p.createdDate BETWEEN '2014-06-23 05:00' and '2014-06-23 14:00'
AND pn.esbStatus = '4'
AND p.mtnAssetID NOT LIKE '11' AND p.mtnAssetID NOT LIKE '12'
)) AS 'HK EOL'";
$result = $SCADA->query($query) or die($SCADA->error.__LINE__);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<td width=124>", stripslashes($row['HK EOL']);
}
}
else {
echo 'NO RESULTS';
}
//Wallet One Totals
$query = "select count(id)
from Kit_T where mtnAssetID = '3' and createdDate between '2014-06-23 05:00' and '2014-06-23 14:00';";
$result = $SCADA->query($query) or die($SCADA->error.__LINE__);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<td width=124>", stripslashes($row['count(id)']);
}
}
else {
echo 'NO RESULTS';
}
//Wallet Two Totals
$query = "select count(id)
from Kit_T where mtnAssetID = '10' and createdDate between '2014-06-23 05:00' and '2014-06-23 14:00';";
$result = $SCADA->query($query) or die($SCADA->error.__LINE__);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<td width=124>", stripslashes($row['count(id)']);
}
}
else {
echo 'NO RESULTS';
}
//Wholesale Bricking Totals
$query = "SELECT COUNT(b.id) as totals
FROM Brick_T b, BrickNotify_T bn
WHERE b.id = bn.brickID
AND createdDate BETWEEN '2014-06-23 05:00' and '2014-06-23 14:00'
AND bn.esbStatus = '3'";
$result = $SCADA->query($query) or die($SCADA->error.__LINE__);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<td width=62>", stripslashes($row['totals']);
}
}
else {
echo 'NO RESULTS';
}
//Wholesale EOL Totals
$query = "SELECT CONCAT
(
(SELECT COUNT(p.id) as 'PalletS'
FROM Pallet_T p, PalletNotify_T pn
WHERE p.id = pn.`palletID`
AND p.createdDate BETWEEN '2014-06-23 05:00' and '2014-06-23 14:00'
AND pn.esbStatus = '3'
AND p.mtnAssetID = '12'
) , ' / ' ,
(SELECT COUNT(p.id) as 'PalletF'
FROM Pallet_T p, PalletNotify_T pn
WHERE p.id = pn.`palletID`
AND p.createdDate BETWEEN '2014-06-23 05:00' and '2014-06-23 14:00'
AND pn.esbStatus = '4'
AND p.mtnAssetID = '12'
)) AS 'WEOL'";
$result = $SCADA->query($query) or die($SCADA->error.__LINE__);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<td width=124>", stripslashes($row['WEOL']);
}
}
else {
echo 'NO RESULTS';
}
//Blister Totatls
$query = "SELECT COUNT(bb.id) as totals
FROM BlisterBrick_T bb, BlisterBrickNotify_T bbn
WHERE bb.id = bbn.blisterBrickID
AND createdDate BETWEEN '2014-06-23 05:00' and '2014-06-23 14:00'
AND bbn.esbStatus = '3'";
$result = $SCADA->query($query) or die($SCADA->error.__LINE__);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<td width=62>", stripslashes($row['totals']);
}
}
else {
echo 'NO RESULTS';
}
//Blister EOL Totals
$query = "SELECT CONCAT
(
(SELECT COUNT(p.id) as 'PalletS'
FROM Pallet_T p, PalletNotify_T pn
WHERE p.id = pn.`palletID`
AND p.createdDate BETWEEN '2014-06-23 05:00' and '2014-06-23 14:00'
AND pn.esbStatus = '3'
AND p.mtnAssetID = '11'
) , ' / ' ,
(SELECT COUNT(p.id) as 'PalletF'
FROM Pallet_T p, PalletNotify_T pn
WHERE p.id = pn.`palletID`
AND p.createdDate BETWEEN '2014-06-23 05:00' and '2014-06-23 14:00'
AND pn.esbStatus = '4'
AND p.mtnAssetID = '11'
)) AS 'WEOL'";
$result = $SCADA->query($query) or die($SCADA->error.__LINE__);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<td width=124>", stripslashes($row['WEOL']);
}
}
else {
echo 'NO RESULTS';
}
echo "</tr>";
echo "</table>";
mysqli_close($SCADA);
?>
UPDATE: I have resolved the issue by changing the code in my mailer function using an output buffer to store the results in a variable which i include in my mailbody (see revised code below):
ob_start();
include('./scripts/Daily.php');
$mailbody = ob_get_contents();
ob_clean();
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(true);
$mail->Host = 'smtp.gmail.com:465';
$mail->SMTPAuth = true;
$mail->Username = 'XXXXXXX';
$mail->Password = 'XXXXXXX';
$mail->SMTPSecure = 'ssl';
$mail->isHTML(true);
$mail->From = 'XXXXXXX';
$mail->FromName = 'Automated Reporting';
$mail->addAddress('XXXXXXX@XXXXXXX');
$mail->addReplyTo('XXXXXXX@XXXXXXX');
$mail->isHTML(true);
$mail->Subject = 'Daily Production Report ';
$mail->Body = $mailbody;
$mail->SMTPDebug = 0;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}