Email once a day

1

Good afternoon, How do I send email once a day. I use phpmailer. I've tried using the while, but I do not know which function determines the amount of every 24 hours.

My code:

$assunto = "[RemocenteR] Nova Tarefa Cadastrada!";
    $mensagem = "Prezado usuário, foi enviada uma tarefa pelo sistema remocenter para você. Acesse o menu e verique as tarefas pendentes";
    $dadosDaTarefa = '<br><br>';
    $dadosDaTarefa .= '<strong>Dados da Tarefa</strong>';
    $dadosDaTarefa .= '<br>';
    if($TA_TAREFA != ""){
      $dadosDaTarefa .= 'Tarefa: ' . $TA_TAREFA;
      $dadosDaTarefa .= '<br>';
    }
    if($TA_DESCRICAO != ""){
      $dadosDaTarefa .= 'Descrição: ' . $TA_DESCRICAO;
      $dadosDaTarefa .= '<br>';
    }
    if($TA_PRAZO != ""){
      $dadosDaTarefa .= 'Prazo: ' . $TA_PRAZO_BR;
      $dadosDaTarefa .= '<br>';
    }
    if($TA_PRIORIDADE != ""){
      $dadosDaTarefa .= 'Prioridade: ' .$TA_PRIORIDADE;
      $dadosDaTarefa .= '<br>';
    }
    if($TA_SOLICITANTE != ""){
      $dadosDaTarefa .= 'Soliciante: '. $TA_SOLICITANTE;
      $mensagem .= $dadosDaTarefa;
    }
    $mensagem .= '<br><br>';
    $mensagem .= 'Mensagem gerada automaticamente pelo Sistema!';
    

    if(sendMail('[email protected]',$FET_USUARIO['US_EMAIL'], $mensagem, $assunto)){
	   	echo '<script language = "javascript">alert("Tarefa registrada, foi enviado um e-mail para notificar o  solicitado!")</script>';
    }
		  echo ("<script language='javascript'>location.href='tarefas.php'</script>");
		}

Thank you for the strength

    
asked by anonymous 24.05.2017 / 21:38

2 answers

0

The way you're trying to do will not work. PHP is different from other languages that keeps running intermittently on the server. It has a cycle of beginning, middle, and end. And the end is the very end.

To solve your problem, you can use CronJobs (Linux) or Task Scheduler (windows).

The definition is simple: schedule the daily execution of your script. It can be programmed to run every 24 hours or at other intervals.

So every 24 hours your script will be called and will send the emails.

For Cron, you can look at the link below: Running cron job on server

Cron (Linux environments):

1 2 3 4 5 /root/script.php

For the task scheduler, you can use the link below: Run PHP script in Windows Task Scheduler

Task Scheduler - Windows environments:

c:/caminhocorreto/php [ -f ] c:/caminho/para/o/script.php

In addition, the Task Scheduler is visual.

    
24.05.2017 / 21:43
2

For this you should use CRON.

What are Cron Jobs?

Cron Jobs are like Windows Scheduled Tasks: tasks are run automatically from X to X times ... Making a real-life analogy is when you take out the garbage or tidy up your room, you probably do this by always following one same interval of time ... Every 2 days, every 1 week and so on.

How to create a Cron Job?

If your site runs on any specialized server and you have a control panel like cPanel recommend you take a look there because there is a ready web interface to manage the crons ... if you do not have this panel or do not have access to it will have to go straight to the shell / terminal of your server and start spending your finger. The definition of a cron job consists of a row with 6 values separated by space, like this:

minuto hora dia mes dia-da-semana linha-de-comando

Here are some examples of setting the time before creating cron itself:

Cron Job that runs every day at 06:00 am

0 6 * * * linha-de-comando

Reference

    
24.05.2017 / 21:43