I have to create a Cron in Laravel to send emails every 24h. I must upload the email to the bank. So after a query in the database cron should send the email with the query data.
I have to create a Cron in Laravel to send emails every 24h. I must upload the email to the bank. So after a query in the database cron should send the email with the query data.
To create CRON
in laravel follow these steps :
1) Create a command as follows:
php artisan make:command ExampleCron --command=example:cron
within this file that was created in the app/Console/Commands/ExampleCron.php
folder.
namespace App\Console\Commands;
use Illuminate\Console\Command;
use DB;
class ExampleCron extends Command
{
protected $signature = 'example:cron';
protected $description = 'Command E-mail';
public function __construct()
{
parent::__construct();
}
// aqui você coloca a lógica do seu processo
// pode utilizar todos os recursos do Laravel
public function handle()
{
\DB::table('emails')->get(); // pega os e-mails
// siga o código de sua preferencia
// executando as funções de envio de e-mail
$this->info('Example Cron comando rodando com êxito');
}
}
Change the variables $signature
and $description
by example and in the method handle()
process logic.
2) To register this command
enter in the folder and file app/Console/Kernel.php
and open the Kernel.php
to add in array
of $commands
add plus command
created.
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
Commands\Inspire::class,
Commands\ExampleCron::class,
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('inspire')->hourly();
$schedule->command('example:cron')->daily(); // email diários
}
}
3) Now add this line to your cron
* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1
It can be configured in several ways and several time, example frame:
***Schedule Frequency Options***
*---------------------------------*-----------------------------------------------------*
| Method | Description |
| ->cron('* * * * * *'); | Run the task on a custom Cron schedule |
| ->everyMinute(); | Run the task every minute |
| ->everyFiveMinutes(); | Run the task every five minutes |
| ->everyTenMinutes(); | Run the task every ten minutes |
| ->everyFifteenMinutes(); | Run the task every fifteen minutes |
| ->everyThirtyMinutes(); | Run the task every thirty minutes |
| ->hourly(); | Run the task every hour |
| ->hourlyAt(17); | Run the task every hour at 17 mins past the hour |
| ->daily(); | Run the task every day at midnight |
| ->dailyAt('13:00'); | Run the task every day at 13:00 |
| ->twiceDaily(1, 13); | Run the task daily at 1:00 & 13:00 |
| ->weekly(); | Run the task every week |
| ->monthly(); | Run the task every month |
| ->monthlyOn(4, '15:00'); | Run the task every month on the 4th at 15:00 |
| ->quarterly(); | Run the task every quarter |
| ->yearly(); | Run the task every year |
| ->timezone('America/New_York'); | Set the timezone |
*---------------------------------*-----------------------------------------------------*
Source: link
Example: obtained and referenced on the link ( source ): link )
Your question has an email, here on the site already has a minimal example, follow the same logic , follow this basic example that can help you understand the process.
References