简体   繁体   中英

Cron not sending PHP email

I have the following script:

<?
$dir = './var/session';
$fi = new FilesystemIterator($dir, FilesystemIterator::SKIP_DOTS);
$fileCount = iterator_count($fi);

//    echo 'Directory Count: ';echo $fileCount;

//echo '<br />';



$bytes = 0;
foreach (new DirectoryIterator('./var/session') as $file) {
    if ($file->isFile()) {
        $bytes += $file->getSize();
    }
}

//echo $bytes;
//echo ' Bytes';
//echo '<br />';

 $MBbytes = number_format($bytes / 1048576, 2);
//echo $MBbytes;echo ' MB';



    $from = "mail@example.com";
    $to = "admin@example.com";
    $subject = "Session MB = " .$MBbytes;
    $message = "Session MB = " .$MBbytes."\n Directory Count: " .$fileCount;
    $headers = "From:" . $from;
    mail($to,$subject,$message, $headers,"-fmail@example.com");
//    echo "Email sent";

I'm trying to run this script by cron. When I run it by going to it in my browser it works fine. I've used every cron expression I can but the email doesn't get sent. Any suggestions?

try this: */10 * * * * php LOCALPATHTOYOURSCRIPT/session-folder-size.php

where */10 * * * * - “At every 10th minute.”

I found the solution. Wget was being blocked by an htaccess rule. I noticed in the log file a 403 error. Don't know why it doesn't run using php...

Thanks John Conde for the log file tip.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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