mail() 函数将无法识别 php cronjob 中的消息变量

mail() function will not recognize message variables in php cronjob

我有一个名为 "basic.txt" 的文本文件,其中包含以下文本

This
is a
test file.

我的 cronjob 每分钟运行我的 php 程序并给我发一封电子邮件。我收到电子邮件没有问题,但它不会包含正文消息。这是我的 php 代码:

<?php
    $string = "";
    $file = fopen("basic.txt", "r");
    $string .= file_get_contents("basic.txt");
    $fclose($file);
    echo $string; // this is to test that in fact $string contains the text and it does
    mail("example@hotmail.com", "TEST", $string, "From: example@hotmail.com");
?>

为什么 $string 不能将文本文件中的文本发送到电子邮件正文,即使我可以使用 "echo $string" 并且它工作正常?

谢谢

去掉这两行:

 $file = fopen("basic.txt", "r");
 $fclose($file);

因为您已经在使用 file_get_contents() 来获取文件的内容。

运行 你从我的服务器生成的代码:

Fatal error: Function name must be a string in /path/to/file.php on line 5

你的代码在你身上失败了默默地


因此,在 OP 的情况下,根据评论中的建议,文本文件需要完整的系统路径,并且必须设置一个新的 cron 作业。