Emails once a day


11/8/02
 

Emails once a day

Last night I had a good idea! Up until then I had been thinking of either using cron together with a PHP script capable of finding and processing every web page in the website (which was my initial idea) or of triggering a suitable PHP script via web page hits. I discounted the second idea because it might lead to page owners getting too many email messages. But last night I thought of how to restrict this to one message per day (maximum).

My idea is for the PHP script to create a text file listing all the reminders sent so far that day. Whenever the script is triggered it must first check the expiry date in the page that triggered it, then if it's a past date that means an email needs to be sent. Before sending the email however, the script must check whether the text file already contains a record of an email having been sent for the page in question.

Probably the "best" way to tackle this would be to set up a MySQL database and store the information about past emails in that. I haven't used MySQL before but I have written PHP scripts that read and write to files, so because I feel more familiar with that I'm going to work on the idea of a script that uses a text file. I'm going to assume that the file starts with a date (dd/mm/yy) and then contains just a list of URLs. It'll grow longer as the day progresses, but then be rewound by the first hit of the next day.

A skeleton for the solution?

I now feel in a position that I haven't felt in before with this mini-project - I think I can see the solution on the horizon! Rather than just create another small test script, I'm going to see if I can now produce a skeleton for the solution. At this point I'm not writing about what I did - I'm writng down my thoughts as I go along.

Here's my first attempt at the pseudo-code:
read Expiry Checker information (expiry_date&owner&message)
parse the information using the parse_str function
if (expiry_date <= today's date) then
  lock file reminders_sent_today.txt
  read date_of_file from file
  if (date_of_file < today's date) then
    rewind file
    write today's date to file
  endif
  search file for url of web page
  if (URL not found in file) then
    write URL to file
    email URL and message to owner
  endif
  unlock file reminders_sent_today.txt
endif
 
The lock needs to be there to avoid any potential problems occurring due to the script being triggered concurrently by different web pages. I think I know how to convert all of this pseudo-code into PHP, although I'm not sure at this point quite how the script is going to be able to read the Expiry Checker information from the web page.

 
 

<<contents ^top^ next>>