Crontab任务和电子邮件附件未发送


Crontab task and email attachment not sent

我昨天做了一个PHP脚本,可以发送一个PDF作为附件的电子邮件。

当我用这个脚本安排一个crontab时,我收到了电子邮件,但没有收到附件。当我手动启动脚本时,我有电子邮件和附件。

sendMail函数的PHP代码:

function sendMail()
{
            $corpse = file_get_contents(dirname(__FILE__).'/output/output.tpl');
            $mail = new PHPMailer;
            $mail->isMail();
            $mail->IsHTML(true);
            $mail->From='SenderMailAddress';
            $mail->FromName='SenderName';
            $mail->AddAddress('MyEmail');
            $date = date("Ymd", time());
            $yesterday = date("Ymd", strtotime("-1 day"));
            if ($this->type == cur)
                    $pj = "/opt/birt/ReportEngine/output/bookingperiod_".$date.".pdf";
            else
                    $pj = "/opt/birt/ReportEngine/output/bookingperiod_".$yesterday.".pdf";
            echo $pj;
            $mail->AddAttachment($pj);
            $mail->AddReplyTo('NoReplyAddress');
            $mail->Subject='SubjectOfTheMail';
            $mail->Body=$corpse;
            if (!$mail->Send())
                    echo "Error Sending: ".$mail->ErrorInfo;
            unset($mail);
}

作为crontask的脚本:

TODAY=`date "+%Y-%m-%d"`
export BIRT_HOME=/opt/birt
echo $TODAY
cd /opt/birt/ReportEngine
php GenPeriod.php PDF $TODAY /*first generation of a PDF file which will be the attachment for the PHP script*/
cd MY_PATH_TO_PHPSCRIPT_FOLDER
php Launche.php cur

是否有人已经遇到了相同类型的问题?

你怎么解决这个问题?

谢谢,)

尝试这样做并继续验证文件权限。

function sendMail()
{
        $corpse = file_get_contents(dirname(__FILE__).'/output/output.tpl');
        $mail = new PHPMailer;
        $mail->isMail();
        $mail->IsHTML(true);
        $mail->From='SenderMailAddress';
        $mail->FromName='SenderName';
        $mail->AddAddress('MyEmail');
        $date = date("Ymd", time());
        $yesterday = date("Ymd", strtotime("-1 day"));
        if ($this->type == cur)
                $pj = "/opt/birt/ReportEngine/output/bookingperiod_".$date.".pdf";
        else
                $pj = "/opt/birt/ReportEngine/output/bookingperiod_".$yesterday.".pdf";
        //echo $pj;
        $mail->Subject = (is_readable($pj)) ? 'The file is readable' : 'The file is NOT readable'; // DEBUG
        $mail->AddAttachment($pj);
        $mail->AddReplyTo('NoReplyAddress');
        //$mail->Subject='SubjectOfTheMail';
        $mail->Body=$corpse;
        if (!$mail->Send())
                echo "Error Sending: ".$mail->ErrorInfo;
        unset($mail);
}
http://www.php.net/manual/en/function.is-readable.php

从命令行运行PHP文件和从web服务器请求它之间的主要区别之一是当前目录。

这是一个常见的错误,忘记它,所以我尝试chdir(dirname(__FILE__).'/');在你的文件的顶部。

如果这不是问题,请显示一些代码,在启用所有错误的情况下运行它,并检查(cronjob的)输出。

在cron作业脚本中MY_PATH_TO_PHPSCRIPT_FOLDER前面缺少$-符号