In this Post “Laravel 12 cron job task scheduling example using email” ,I will show you how to schedule task to send email daily at specific time using cron job in laravel 12 application.
In modern web applications, automation is key. Task scheduling allows you to automatically execute repetitive tasks like sending emails, generating reports, or cleaning up databases. Laravel simplifies task automation with its built-in scheduler.
Step for Laravel 12 Cron Job Task Scheduling Example Tutorial Using Email
- Step 1: Install Laravel 12
- Step 2: Configure Mail Settings
- Step 3: Create Mailable Class
- Step 4: Create a Custom Command
- Step 5: Schedule The Task
- Step 6: Run Scheduler Command Manually For Testing
- Step 7: Laravel 12 Setup Cron Job On Server
Step 1: Install Laravel 12
To get started, you’ll need a Laravel project. If you already have one set up, you can skip this step. Otherwise, run the following command in your terminal to create a new Laravel project:
composer create-project laravel/laravel laravel-12-cron-job-task-scheduling-example-tutorial
Prerequisites
- Laravel 12 or 11 any version project installed
- Basic understanding of Artisan commands
- Mail configuration set up
Step 2: Configure Mail Settings
Open your .env file and add the mail configuration. if you’d like a detailed guide on sending emails in Laravel using Gmail SMTP, click here. Below is an example configuration for Gmail SMTP:
MAIL_MAILER=smtp
MAIL_SCHEME=null
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=your_gmail_username@gmail.com
MAIL_PASSWORD=spvaxqyulyqumrxh
MAIL_FROM_ADDRESS="your_gmail_username@gmail.com"
MAIL_FROM_NAME=itstuffsolutions
Step 3: Create Mailable Class
Use the command below in your terminal to create a SendNewsletterMail class within the App\Mail folder.
php artisan make:mail SendNewsletterMail
To send newsletters to all users, we use the SendNewsletterMail class. It takes a users object through its constructor and sets the email view in the content() method.
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
class SendNewsletterMail extends Mailable
{
use Queueable, SerializesModels;
public $users;
/**
* Create a new message instance.
*/
public function __construct($users)
{
$this->users = $users;
}
/**
* Get the message envelope.
*/
public function envelope(): Envelope
{
return new Envelope(
subject: 'Send Newsletter Mail',
);
}
/**
* Get the message content definition.
*/
public function content(): Content
{
return new Content(
view: 'newsletter_mail',
);
}
/**
* Get the attachments for the message.
*
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
*/
public function attachments(): array
{
return [];
}
}
Step 4: Create a Custom Command
To send emails at a specific time in Laravel, you need to create a custom Artisan command. This command will be executed through Laravel’s task scheduling using a cron job. Run the following command to generate the SendEmailCron file inside the App\Console\Commands directory.
php artisan make:command SendEmailCron --command=send:email
Open App\Console\Commands\SendEmailCron.php , Add code to send email
Open the App\Console\Commands\SendEmailCron.php file and add the code to send emails inside the handle() method. In this method, I fetched user data from the User model and used a foreach loop to send the newsletter to each user individually.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
Use App\Models\User;
use Illuminate\Support\Facades\Mail;
use App\Mail\SendNewsletterMail;
class SendEmailCron extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'send:email';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Newsletter Sent to Subscribers Daily at 1:00 AM';
/**
* Execute the console command.
*/
public function handle()
{
$users = User::select('email','name','content')->get();
foreach($users as $u){
Mail::to($u->email)->send(new SendNewsletterMail($u));
info('Mail send Successfully '.$u->email);
}
}
}
To send the newsletter email, create a Blade template at resources/views/newsletter_mail.blade.php. This file contains a basic HTML layout for the email content.
Copy and use this code into your newsletter_mail.blade.php file.
<!DOCTYPE html>
<html>
<head>
<title>Newsletter</title>
</head>
<body>
<h1>Hello, {{$users->name}}</h1>
<p>
{!! $users->content !!}
</p>
<p>Regards,<br>{{ config('app.name') }}</p>
</body>
</html>
Step 5: Schedule The Task
In this step, we will learn how to schedule a command in Laravel. Open routes/console.php, remove or comment the existing code, and add the following code to define schedule tasks using the schedule method:
<?php
use Illuminate\Support\Facades\Schedule;
Schedule::command('send:email')->dailyAt("1:00");
In the example code, we’ve used the ->dailyAt(‘1:00’) method, which schedules the task to run every day at 1:00 AM. Laravel provides a variety of expressive methods to define task frequency. For instance:
- Use ->everyMinute() to execute the task every minute.
- Use ->weekly() to run the task once a week—by default, this runs every Sunday at midnight (00:00).
- Use ->twiceDaily(1, 13) to schedule the task twice daily, at 1:00 AM and 1:00 PM.
Laravel’s scheduler includes many more options like ->hourly(), ->monthly(), ->quarterly(), ->yearly(), and even granular ones such as ->everyTwoMinutes() or –>everySecond() .
For a complete list of available scheduling methods, refer to the official Laravel task scheduling documentation.
Step 6: Run Scheduler Command Manually For Testing
All the steps have been completed. Now you can manually test the cron job by executing the command given below.
php artisan schedule:run
The output is look like this :

We have used the info() function to print the message to the log file. Open storage/logs/laravel.log to see the printed message, which looks like this:

Step 7: Laravel 12 Laravel 12 Setup Cron Job On Server
In this step we will see how to setup cron job on server
1. Go To Your Laravel Project Path
First, find the full path to your Laravel project.For example, your Laravel path might be:
/var/www/html/laravel-project
2. Open the Crontab File
Open the crontab configuration for the current user:
crontab -e
If it’s your first time, it may ask you to select an editor. Choose nano for simplicity.
3. Add Laravel’s Scheduler Cron Entry
Add the following line to the bottom of the file:
* * * * * cd /full-path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
Replace /full-path-to-your-project
with your actual Laravel project path. For example:
* * * * * cd /var/www/home/laravel-12-cron-job-task-scheduling && php artisan schedule:run >> /dev/null 2>&1
Source Code / Demo Link
GitHub repo or download link for readers.