2015-02-23 47 views
0

我有一个脚本,用于检查匹配的连接数据库,当匹配时,邮件正在发送。PHPMailer错误 - 在cron作业中使用邮件正文

我的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

错误消息对我来说似乎已清除 – 2015-02-23 21:28:09

+2

由于错误消息很明确,因此我正在投票结束此问题作为脱离主题。 – 2015-02-23 21:34:17

回答

0

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