【问题标题】:Cron not sending PHP emailCron 不发送 PHP 电子邮件
【发布时间】:2018-11-15 08:34:02
【问题描述】:

我有以下脚本:

<?
$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";

我正在尝试通过 cron 运行此脚本。当我通过在浏览器中访问它来运行它时,它工作正常。我已经使用了所有可以使用的 cron 表达式,但没有发送电子邮件。有什么建议?

【问题讨论】:

  • 您确认正在调用脚本吗?这是第一步,因此您可以知道这是 cron 问题还是编码问题(或代码运行后的问题)。
  • 如何确认?
  • 一种简单的方法是写入日志文件。如果该文件未创建或更新,则该脚本未执行。
  • 你使用的是什么 cron 表达式?这可能会有所帮助。
  • 目前正在尝试这个:* * * * * wget example.com/session-folder-size.php

标签: php email cron


【解决方案1】:

试试这个: */10 * * * * php LOCALPATHTOYOURSCRIPT/session-folder-size.php

其中 */10 * * * * - “每 10 分钟。”

【讨论】:

  • 我现在正在运行它 * * * * * php /home/xxxxx/public_html/session-folder-size.php 但没有发送电子邮件。我按照建议记录它,并且日志文件具有确切的 php 脚本,即在 session-folder-size.php
【解决方案2】:

我找到了解决方案。 Wget 被 htaccess 规则阻止。我在日志文件中注意到 403 错误。不知道为什么不使用php运行...

感谢 John Conde 提供的日志文件提示。

【讨论】:

  • 因为例如 CLI 和 WebServer 的 PHP 设置不同
猜你喜欢
  • 2012-01-21
  • 2011-09-16
  • 2011-04-26
  • 2014-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多