I made a PHP script yesterday which send an email with a PDF as an attachment.
When I make a crontab with this script shedulded, I recieve the email but not the attachment. When I launch the script manually, I have the email and the attachment.
Here is the PHP code of the sendMail function :
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');
$mai开发者_开发知识库l->Subject='SubjectOfTheMail';
$mail->Body=$corpse;
if (!$mail->Send())
echo "Error Sending: ".$mail->ErrorInfo;
unset($mail);
}
The script that I put as a 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 ;)
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
One of the main differences between running a PHP file from command-line and by requesting it from the webserver, is the current directory.
It's a common mistake to forget about it, so I'd try 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).
Missing $-sign in front of MY_PATH_TO_PHPSCRIPT_FOLDER in cron job script.
精彩评论