Send e-mail at the end of each month

-2

I have a user signature system, I need to send a notification every end of the month stating that the signature is winning, the problem is that I do not know how to make this script run automatically without having to go to a certain page for it to run, I'll try to give an example simplistically:

Let's suppose I want to send this email on October 2nd, so I would do so:

$data_programada = '02/10/2018';
if(date('d/m/Y') == $data_programada) {

    mail('[email protected]','vencimento','mensagem de vencimento');

}

The problem is that this email will only be sent if I access the page that contains this script, I need it to do it automatically

    
asked by anonymous 02.10.2018 / 17:22

1 answer

-1

What you want is a CronJob: What are CRON JOBS and how to use them with PHP

This example uses the cron mechanism of unix / linux, which is the most common to use, but there are mechanisms that integrate directly the language, as is the case of Quartz for Java, but I do not know any of that for PHP.

My tip: Use linux cron:)

Complementing the answer:

You need to add an entry in your system user's cron (linux):

Step 1: Create the file to be executed: Helloworld.php file

<?php

echo 'Hello world';

?>

Step 2: Start the change in the system cron file using the 'crontab -e' command. This command will open the cron file using the default editor, probably vim

Step 3: Add the desired cron entry with the file that was created. In this example I will put it to run every first day of the month at 9 am:

0 9 1 * * php -f /home/murilo/meu_php/helloworld.php > /dev/null 2>&1

Description:

0 9 1 * * -> Expressão cron para executar em todo primeiro dia do mes as 9 da manhã (para mais exemplos acesse https://crontab-generator.org/)

php -f /home/murilo/meu_php/helloworld.php -> Comando a ser executado

> /dev/null 2>&1 -> Direcionando sysouts para /dev/null

Step 4: Save the file and you're done!

To run exactly at the end of the month is a bit trickier, as the months have different end days (it may end on the 28th, 29th, 30th or 31st) the cron expression gets a bit more complicated. The easiest way I found was by creating more than one entry in cron to cover the different cases.

See:

p>

link

    
02.10.2018 / 17:51