我昨天制作了一个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
有人遇到过同样类型的问题吗?
你怎么解决?
谢谢;)
答案 0 :(得分:1)
从命令行运行PHP文件和从Web服务器请求PHP文件之间的主要区别之一是当前目录。
忘掉它是一个常见的错误,所以我会在你文件的顶部尝试chdir(dirname(__FILE__).'/');
。
如果这不是问题,请显示一些代码,在启用所有错误的情况下运行它并检查其输出(cronjob)。
答案 1 :(得分:1)
尝试类似的操作并继续验证文件权限。
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);
}
答案 2 :(得分:0)
在cron作业脚本的MY_PATH_TO_PHPSCRIPT_FOLDER前面缺少$ -sign。