Michael Michael - 9 months ago 26
PHP Question

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

I have a text file called "basic.txt" with the following text


is a

test file.

My cronjob runs my php program every minute and sends me an email. I receive the email no problem, but it will not include the body message. Here is my php code:

$string = "";
$file = fopen("basic.txt", "r");
$string .= file_get_contents("basic.txt");
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");

Why isn't $string sending the text from the text file to the body of the email even though I can use "echo $string" and it works fine?



Get rid of these 2 lines:

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

Since you're already using file_get_contents() which fetches the file's content.

Running your code from my server produced:

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

Your code is failing on you silently.

Therefore and in the OP's case and as per suggestions in comments, a full system path was required for the text file, and had to setup a new cron job.