duanmin0941 2014-06-23 17:30
浏览 35
已采纳

PHP Mailer消息未正确通过

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';
    }
  • 写回答

2条回答 默认 最新

  • dscrn1974 2014-06-23 17:32
    关注

    This line:

    $mail->Body    = include_once('./scripts/Daily.php');
    

    is incorrect. include generally DOESN'T "return" anything, other than a true/false to signify if the file was actually loaded or not.

    You probably want something more like:

    ob_start();
    include_once(...);
    $mail->Body = ob_end_clean();
    

    This'll capture the output of the included file, which you can then stuff into PHPMailer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line