0

I'm using PHPMailer, and have a test.php file. Whenever this page is reloaded in the browser, the test.php file executes and sends email messages and echoes the email addresses that were used. I have a cronjob setup for this to execute once a day. I created another file body.php, that includes this:

<?php
$homepage = file_get_contents('http://www.myrealsite.com/mailer/test.php');
echo $homepage;
?>

This returns the information that I want, which is basically just an output of who I emailed, but here is the problem: Every time I reload body.php it executes the test.php file and sends email again. I want to be able to reload body.php without it running body.php. I'm new at this.

Bart Dangus
  • 183
  • 1
  • 10
  • There's one thing I don't understand here and I am hesitant to post an answer (well, I did earlier but I ended up deleting it). You're loading a file that contains phpmailer code, yet you're using a cron job with the same code. – Funk Forty Niner May 04 '16 at 23:19
  • 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. It's one or the other. – Funk Forty Niner May 04 '16 at 23:23
  • Maybe I'm doing something dumb, but let me try and clarify. Whenever `test.php` is reloaded in the browser, it will send emails. I created a cronjob to automatically execute that file, once daily. It does it. So any time that file is loaded/executed, either through me doing it manually or the cronjob doing it when scheduled, it will send mail. – Bart Dangus May 04 '16 at 23:24
  • 1
    I'm putting another answer together, give me a minute or so. I think I have what you should use. – Funk Forty Niner May 04 '16 at 23:26
  • I undeleted it and overwrote it from my original. Have a look at that. – Funk Forty Niner May 04 '16 at 23:29

4 Answers4

0

You can get body.php to load without sending the e-mail by adding a parameter to your call to body.php:

http://whatever.your.server.is/body.php?send=no

Then, you just need to $_GET that param and implement a simple IF:

if (!$_GET['send'] != 'no'){
    //send the e-mail
Webomatik
  • 844
  • 7
  • 7
0

Okay I figured it out. My cronjob was initially this:

0 17 * * * php /var/www/html/mailer/test.php >> /var/www/html/mailer/cron_status.log

This would execute the test.php file everyday at 5pm and write to this cron_status.log file. By Removing one > and changing the cronjob to:

0 17 * * * php /var/www/html/mailer/test.php > /var/www/html/mailer/cron_status.log

deletes whats in the cron_status.log and writes to it. Then for body.php I just used:

$emailLog = file_get_contents("http://www.bartdangus.com/mailer/cron_status.log"); echo $emailLog;

Now obviously it would be better to have a log file loge everything, but my requirement that I needed to meet does not include logging everything, I just needed what happened in the past 24hrs.

Bart Dangus
  • 183
  • 1
  • 10
-1

Whenever you run body.php it will make http request to test.php and so it will send email. If I understand you correctly you wish to list all the email addresses to whom the email was sent when Cron Job ran.

So it would be better if you save the results in a separate text file and then in body.php read that file. Like this, in your cron file (test.php):

$yourFile = fopen("email_logs.txt", "a"); //make sure this file is writeable by php

$fileContents = date('Y-m-d H:i:is')."\r\n"; //write the time of file saved
$fileContents .= 'email_address_here'; //all email addresses here
$fileContents .= '\r\n=======\r\n'; //a separator line.

fwrite($yourFile, "\n". $fileContents);
fclose($yourFile);

In your other file to read the emails, do:

$emailLog = file_get_contents("http://www.myrealsite.com/mailer/email_logs.txt");
Ghulam Ali
  • 1,935
  • 14
  • 15
  • 1
    Sidenote: It would probably be best if the text file used a time/date additional name, as that file could grow rather large over time. – Funk Forty Niner May 04 '16 at 22:43
  • I noticed you got a downvote and I have no idea why. That isn't mine, in case you're wondering. – Funk Forty Niner May 04 '16 at 22:47
  • I don't care for the downvotes, as far as it would give a general idea for readers on how to solve this problem. For your sidenote, yes it's a good idea but I think he wants also to list the data using php, so it would be then to iterate all the files and show all the data which would become complicated at this stage as he said he is new in php. – Ghulam Ali May 04 '16 at 22:53
-1

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.

  • It's one or the other.

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];
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • phpmailer code is only contained in `test.php` and the cron job executes that file. For `body.php` the only thing I'm trying to do on it show the emails that were sent the last time `test.php` was executed. Currently when the cronjob is executed it logs the results to `cron_status.log` – Bart Dangus May 04 '16 at 23:36
  • @BartDangus I am very confused here. – Funk Forty Niner May 04 '16 at 23:38
  • @BartDangus so why use the `file_get_contents('http://www.myrealsite.com/mailer/test.php')` in body.php is what I don't understand. You are already sending mail with test.php in cron. So get the contents of the log file instead of test.php – Funk Forty Niner May 04 '16 at 23:39
  • Right. I'm just trying to have a separate page the will show ONLY the emails I sent last, and using that `file_get_contents` was what I came up with. – Bart Dangus May 04 '16 at 23:44
  • @BartDangus ah... now I know the whole picture ;-) Ok, well that's where things start to get a bit more complicated (but not impossible), as you would either do as I suggested in my footnotes using a date/time naming convention... yet you'd need to use a conditional statement using PHP's `filemtime()` function http://php.net/manual/en/function.filemtime.php and comparing to today's date for example, or use the `w` switch instead of `a` in `fwrite` yet still use date/time file naming convention. That's all that comes to mind at this stage for me right now. – Funk Forty Niner May 04 '16 at 23:49
  • @BartDangus I've made a few edits to my answer since I'm trying to find you a full solution. Reload it and do go over it in its entirety. That is the best I can for you here. This being under my **Footnotes:** – Funk Forty Niner May 05 '16 at 00:10