What you're asking is impossible. You can't run a file that contains phpmailer code using file_get_contents()
and using it in a cron job. You can, but that's why it's not working for you in the way you hoped it would.
Sidenote: The method and array used to catch the addresses of each email is unknown.
So, base yourself on the following, which writes to a file and reads from it, and checks if the file first exists.
body.php
<?php
if(file_exists('/path/to/email_sent_to.txt')){
$show_emails = file_get_contents('/path/to/email_sent_to.txt');
echo $show_emails;
}
Your cron job file and to the effect of:
<?php
// your phpmailer code
$emails = array("email1@example.com", "email2@example.com", "email3@example.com");
foreach($emails as $value) {
$value = $value . "\n";
$file = fopen("email_sent_to.txt", "a");
fwrite($file, $value);
fclose($file);
}
which the above will write to file as:
email1@example.com
email2@example.com
email3@example.com
Footnotes:
You may want to use a date/time format for your file naming convention, otherwise that file may get rather large over time. This is just a suggestion.
I.e.:
$prefix = "email_logs_";
$date = date('Y-m-H');
$time = date('h_i_s');
$logfile = $prefix . $date . "_" . $time . ".log";
which would create something like email_logs_2016-05-23_11_59_40.log
.
Then read your file based on the most currently written file using PHP's filemtime()
function.
Borrowed from this question and using a different approach while using a date/time file naming convention as I already suggested you do:
How to get the newest file in a directory in php
$files = scandir('logfiles', SCANDIR_SORT_DESCENDING);
$newest_file = $files[0];