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


PHPMailer error - Message body empty when using in cron job

我有一个脚本,它会检查连接的数据库中的匹配项,当匹配项存在时,就会发送电子邮件。

我的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的来源。

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