PHP Mailer消息未正确通过

时间:2014-06-23 17:30:44

标签: php html email phpmailer

希望有人可以帮助我。我正在玩PHP邮件程序,为我的机器发送我的日常生产报告(只是为了让我的生活更加轻松)但我发送的每封邮件只在邮件正文中返回“1”。如果有人可以提供帮助,不知道我哪里出错了。

这是我的PHPMailer代码:

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';
}

当我运行此脚本时,它会将我需要的值返回到(daily.php)并在我的屏幕上正确显示,并且还会收到邮件已发送的消息。但我收到的邮件在邮件正文中只有“1”,没有别的。

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\n", 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);


?>

更新:我通过使用输出缓冲区更改邮件程序中的代码来解决此问题,将结果存储在我的邮件主体中包含的变量中(请参阅下面的修订代码):

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 个答案:

答案 0 :(得分:2)

这一行:

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

不正确。 include通常会做出&#39; T&#34;返回&#34;除了true / false之外的任何东西,表示文件是否实际加载。

你可能想要更像的东西:

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

这将捕获包含文件的输出,然后您可以将其输入PHPMailer。

答案 1 :(得分:0)

正如其他人所说,include返回1作为正常成功返回值,但您可以通过在包含的文件中包含return语句来获取a return value from an include statement,因此,如果你让Daily.php返回一个合适的字符串,你的原始代码就可以了。使用输出缓冲是非常低效的。

BTW include不是函数,因此您不需要在路径周围使用括号:

$mail->Body    = include './scripts/Daily.php';

此外,您不希望include_once - 如果您之前已经包含它,它将返回布尔值true而不是预期的返回值。