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 快手联盟怎么快速的跑出建立模型
  • ¥20 ros环境下运行orb-slam一直报错
  • ¥15 关于腾讯云服务器中,PHP页面无法打开的问题
  • ¥60 如何把照片修复成原始拍摄图
  • ¥80 Exited too quickly (process log may have details)
  • ¥15 爬知乎登录之后内容加载不出来
  • ¥15 怎么用protues测量通频带
  • ¥15 zepelin使用sparkInterpreter 异常
  • ¥15 paho mqtt 接收不到消息
  • ¥15 函数r关于两个分量y,z方向上的图像,分开画r随y的图像,r随z的图像