Email alert on this day

Posted on

Question :

I need some way to alert the person by email when they arrive on such day by email using php … example: September 1st send the email such to the person such …

how to do this with php? I thought about cron jobs, but I do not know if it’s the best option …


Answer :

Yes the best way is cron jobs. It’s unclear where you get that date but basically you have to have a function called by the cron job, which can be minimalistically like this:

function verificarData($dia, $cliente){
    if ($dia == date("d-m-Y")) enviarmail($cliente);

where $dia is a string with format dd-mm-aaaa and date("d-m-Y") will give the date of the day itself in the same format.

This function will be called by cron job and will be for example (with revealed variables): verificarData('23-08-2014', '');


In the specific case of the body of the question, @Sergio’s answer is the ideal solution.

I am posting this alternative only to future users who may need repetitive tasks, but with little interval between iterations.

You can run a PHP script with an infinite loop at system startup, and have the ranges controlled by the sleep() function, which frees the OS for other tasks:

   set_time_limit( 0 ); // para o PHP poder rodar sem limite de tempo

   while( true ) {

       ... aqui vai a sua função repetitiva,
       que pode ser a checagem de uma condição
       especial, um envio de email em horários
       agendados, uma limpeza de DB, etc ...

       sleep( 30 ); // numero de segundos entre um loop e outro

This script should run at system startup, via script, unique scheduling or boot folders, depending on the OS.


Do not access this script through the browser! Do the command line not to unnecessarily occupy a web server process. In addition, the max_execution_time directive defaults to 0 from the command line, allowing loop to run indefinitely.


Leave a Reply

Your email address will not be published. Required fields are marked *