Alert by email such a day

Asked

Viewed 431 times

3

Hi need some way to alert by email the person when arriving on such day by email using php...example: day September 1st send such email to the person such...

how to do this with php ? I thought of cron Jobs, but I don’t know if it’s the best option...

  • 3

    best way I know is with cron Jobs even

2 answers

6


Yes the best way is cron Jobs. It is unclear where you get this date from but basically you will have to have a function to be called by cron job, which minimalistically can be so:

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 same day date in the same format.

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

  • Hi @Sergio, no command do cron Jobs how do I call the script ?

  • @Alanps the best is to have cron Jobs call a PHP file. Create a file cron.php and make it run daily. Inside the file put this function that has above.

1

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 eventually need to repetitive tasks, but with little interval between iterations.

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

<?php
   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 must be executed at system startup, via script, unico scheduling or startup folders, depending on the OS.

Do not access this script from the browser! Use the command line not to unnecessarily occupy a web server process. Also, the directive max_execution_time has default value 0 by the command line, allowing the loop rotate indefinitely.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.