2016-05-11 31 views
1

我有一個名爲「basic.txt」具有以下文本的文本文件mail()函數將無法識別消息變量是在PHP的cronjob

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("examp[email protected]", "TEST", $string, "From: [email protected]"); 
?> 

爲什麼不是$字符串發送從文本文件中的文本,即使我可以用電子郵件的正文「回聲$字符串」,它工作正常?

感謝

+0

你可以試試'$ string。= file_get_contents($ file);'和/或把你的'$ fclose($ file);'發送郵件後。似乎你可能會很快關閉文件。也檢查文件權限。 –

+0

$ string。= file_get_contents($ file);不起作用,它說我需要在那裏有文件名。 $ FCLOSE($文件);在郵件功能不起作用之後。 – Michael

+0

'file_get_contents()'不需要'fopen()',因此''fclose()'也不需要 – RiggsFolly

回答

2

擺脫這些2線:

$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作業。

+0

我已經做到了這一點,它仍然不會發送給我。我在上面留下了一條評論,解釋發生了什麼 – Michael

+0

@Michael是的,我看到了這一點,並回復了一條評論。是否有任何/你遺漏的代碼,或者是「它」?檢查你的日誌,如果你有訪問 –

+2

@Michael另一件事;這兩個文件都與您正在執行的文件夾位於同一個文件夾內?如果不是,那麼您需要爲文本文件使用完整的系統路徑,或者使用完整的系統路徑。我很確定它與此有關。即:'/ var/user/public/folder/basic.txt' –