PHPMailer错误 - 在cron作业中使用时消息体为空

时间:2015-02-23 21:24:38

标签: php cron phpmailer

我有一个脚本可以检查匹配时连接的数据库,当匹配时,正在发送电子邮件。

我的Cronjob日志说出以下内容:

Warning: file_get_contents(template/emailMatchLost.temp): failed to open stream: No such file or directory in /home/webbro/webapps/wrongle/script.php on line 54
Mailer Error: Message body empty

Warning: file_get_contents(template/emailMatchFound.temp): failed to open stream: No such file or directory in /home/webbro/webapps/wrongle/script.php on line 77
Mailer Error: Message body empty

Fatal error: Cannot redeclare phpmailerautoload() (previously declared in /home/webbro/webapps/wrongle/PHPMailer/PHPMailerAutoload.php:24) in /home/webbro/webapps/wrongle/PHPMailer/PHPMailerAutoload.php on line 31

应该运行的脚本如下:

<?php
include("settings/settings.inc.php");
$mysqli = new mysqli($config['server'], $config['username'], $config['password'], $config['database']);
/* check connection */
if ($mysqli->connect_errno) {
    error_Log("Connect failed: klote%s\n", $mysqli->connect_error, 0);
    exit();
}

$query = "SELECT * 
FROM LostFound AS Lost
INNER JOIN (
SELECT *
FROM LostFound
)Found ON Lost.Serial = Found.Serial
WHERE Lost.Type = 'Lost' and Found.Type = 'Found' And Lost.MatchId = 0 And Found.MatchId = 0";

if ($result = $mysqli->query($query)) 
{
    while ($row = $result->fetch_row()) 
    {
        $recIdLost = $row[0];
        $recIdFound = $row[9];
        $toLost = $row[2];
        $toFound = $row[11];
        $serial = $row[3];
        $place = $row[13];
        $reward = $row[14];

        $updateQuery[] = "UPDATE LostFound SET MatchId = '". $recIdFound. "', flag = '1' WHERE LostFound.RecId = '".$recIdLost."'";
        $updateQuery[] = "UPDATE LostFound SET MatchId = '". $recIdLost. "', flag = '1' WHERE LostFound.RecId = '".$recIdFound."'";

        $find = array("{Serial}", "{Place}", "{Reward}", "{EmailOwner}", "{EmailFinder}");
        $replace = array($serial, $place, $reward, $toLost, $toFound);

        require 'PHPMailer/PHPMailerAutoload.php';
        // Email to lost record
        $mailLost = new PHPMailer();
        $mailLost->isSMTP();
        $mailLost->SMTPDebug = $config['emailSMTPDebug'];
        $mailLost->Debugoutput = $config['emailSMTPDebugoutput'];
        $mailLost->Host = $config['emailSMTPHost'];
        $mailLost->Port = $config['emailSMTPPort'];
        $mailLost->SMTPSecure = $config['emailSMTPSecure'];
        $mailLost->SMTPAuth = $config['emailSMTPAuth'];
        $mailLost->Username = $config['emailSMTPUsername'];
        $mailLost->Password = $config['emailSMTPPassword'];
        $mailLost->setFrom($config['emailLost'], $config['emailNameLost']);
        $mailLost->addReplyTo($config['emailLost'], $config['emailNameLost']);
        $mailLost->addAddress($toLost, $toLost);
        $mailLost->Subject = $config['emailSubjectLostMatch'];
        $mailLost->msgHTML(str_replace($find, $replace, file_get_contents($config['emailBodyLostMatch'])), dirname(__FILE__));

        if (!$mailLost->send()) {
            error_log("Mailer Error: " . $mailLost->ErrorInfo, 0);
        } else {
            error_log("Message sent to " . $toLost, 0);
        }

        // Email to found record
        $mailFound = new PHPMailer();
        $mailFound->isSMTP();
        $mailFound->SMTPDebug = $config['emailSMTPDebug'];
        $mailFound->Debugoutput = $config['emailSMTPDebugoutput'];
        $mailFound->Host = $config['emailSMTPHost'];
        $mailFound->Port = $config['emailSMTPPort'];
        $mailFound->SMTPSecure = $config['emailSMTPSecure'];
        $mailFound->SMTPAuth = $config['emailSMTPAuth'];
        $mailFound->Username = $config['emailSMTPUsername'];
        $mailFound->Password = $config['emailSMTPPassword'];
        $mailFound->setFrom($config['emailFound'], $config['emailNameFound']);
        $mailFound->addReplyTo($config['emailFound'], $config['emailNameFound']);
        $mailFound->addAddress($toLost, $toLost);
        $mailFound->Subject = $config['emailSubjectFoundMatch'];
        $mailFound->msgHTML(str_replace($find, $replace, file_get_contents($config['emailBodyFoundMatch'])), dirname(__FILE__));

        if (!$mailFound->send()) {
            error_log("Mailer Error: " . $mailFound->ErrorInfo, 0);
        } else {
            error_log("Message sent to " . $toFound, 0);
        }
    }

    $result->close();

    if ($mysqli->multi_query(implode(';', $updateQuery)))
    {
        $i = 0; 
        do { 
            $i++; 
        } while ($mysqli->next_result()); 
    } 
    if ($mysqli->errno) { 
        error_log("Batch execution prematurely ended on statement".$i, 0);
        ob_start();                    // start buffer capture
        var_dump($object);             // dump the values
        $contents = ob_get_contents(); // put the buffer into a variable
        ob_end_clean();                // end capture
        error_log($contents); 
    } 
}

$mysqli->close();
error_log("Script done! ", 0);
?>

如果有人能指出正确的方向,那就太棒了。连接有效,因此无法解决问题。此外,我无法找到变量emailBodyLostMatch的来源。

1 个答案:

答案 0 :(得分:0)

错误消息非常清楚,但原因可能是您从cron运行时PATH不同或未设置。在使用相对路径之前,请使用绝对路径或cd到显式目录。