简体   繁体   English

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

[英]Crontab task and email attachment not sent

I made a PHP script yesterday which send an email with a PDF as an attachment. 我昨天制作了一个PHP脚本,它发送一封带有PDF作为附件的电子邮件。

When I make a crontab with this script shedulded, I recieve the email but not the attachment. 当我用这个脚本制作一个crontab时,我收到了电子邮件,但没有附件。 When I launch the script manually, I have the email and the attachment. 当我手动启动脚本时,我有电子邮件和附件。

Here is the PHP code of the sendMail function : 这是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);
}

The script that I put as a crontask : 我把它作为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

Do someone already encounter the same type of problem ? 有人已经遇到过同样类型的问题吗?

How can you solve it ? 你怎么解决它?

Thanks ;) 谢谢 ;)

One of the main differences between running a PHP file from command-line and by requesting it from the webserver, is the current directory. 从命令行运行PHP文件和从Web服务器请求PHP文件之间的主要区别之一是当前目录。

It's a common mistake to forget about it, so I'd try chdir(dirname(__FILE__).'/'); 忘记它是一个常见的错误,所以我会尝试chdir(dirname(__FILE__).'/'); at the top of your file. 在您的文件的顶部。

If that's not the problem, show some code, run it with all errors enabled and check its output (of the cronjob). 如果这不是问题,请显示一些代码,在启用所有错误的情况下运行它并检查其输出(cronjob)。

Try something like this and continue with verifing file permissions. 尝试这样的事情并继续验证文件权限。

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 http://www.php.net/manual/en/function.is-readable.php

在cron作业脚本中,在MY_PATH_TO_PHPSCRIPT_FOLDER前面缺少$ -sign。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM