Send email once a day

Asked

Viewed 1,069 times

1

Good Afternoon, How do I send email once a day. I use phpmailer. I tried using while, but don’t know what function determines the amount 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 force

  • 3

    Have you ever heard of Cron Job? I think it will be the best way out.

  • Yes, but it is a web task system that I have, and let’s say that while the task is not completed it sends an email a day, reminding the user to finish the task. Could I do this cronjob communication with my web system ?. Thank you

  • Yes, create a cronjob to make a Curl request to your web api that initializes the daily email submission script

2 answers

2

For this Voce must use CRON.

What are the Cron Jobs?

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

How to create a Cron Job?

If your site runs on some specialized server and you have a control panel like cPanel I recommend you take a look there because there is a web interface ready 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 line with 6 values separated by space, thus:

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

Let’s go to some examples of setting time before creating the cron itself:

Cron Job running every day at 06:00am

0 6 * * * linha-de-comando

Reference

0


The way you’re trying to do it won’t work. PHP is different from other languages that run 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 the Task Scheduler (windows).

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

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

For Cron, you can look at the link below: How to run cron job on the 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 (Task Scheduler - Windows environments):

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

In addition, the Task Scheduler is visual.

Browser other questions tagged

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