Scheduling Emails in Laravel Web App | Laravel Task Schedule
Are you a web developer looking to enhance user engagement by sending out weekly newsletters to your subscribers automatically? Laravel, a popular PHP framework, offers a seamless solution through its artisan commands and scheduling capabilities. In this guide, we'll walk you through the process of creating a weekly newsletter system using Laravel's artisan commands and job scheduling.
Creating the Email
The first step is to create an email template for your weekly newsletter. In Laravel, this is achieved by running the following command:
php artisan make:mail WeeklyNewsLetter
This command generates a new mail class at app/Mail/WeeklyNewsLetter.php. Open this file and make the necessary modifications:
// app/Mail/WeeklyNewsLetter.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 WeeklyNewsLetter extends Mailable
{
use Queueable, SerializesModels;
public function __construct()
{
// Constructor logic, if any
}
public function envelope(): Envelope
{
return new Envelope(
subject: 'Weekly Newsletter',
);
}
public function content(): Content
{
return new Content(
view: 'emails.newsletter' // Blade file in the resource/views directory
);
}
public function attachments(): array
{
return [];
}
}
Creating the Job to Send Newsletters
Next, let's create a job responsible for sending out the newsletters. Run the following command:
php artisan make:job SendNewsLetterJob
Now, open the generated job file at app/Jobs/SendNewsLetterJob.php and update it as follows:
// app/Jobs/SendNewsLetterJob.php
use App\Mail\WeeklyNewsLetter;
use Illuminate\Bus\Queueable;
use Illuminate\Support\Facades\Mail;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class SendNewsLetterJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(public $user)
{
// Constructor logic, if any
}
public function handle(): void
{
Mail::to($this->user->email)->send(new WeeklyNewsLetter());
}
}
Creating the Command to Dispatch Jobs
Now, let's create a command that dispatches the newsletter job to users with an 'active' status. Run the command:
php artisan make:command SendNewsLetter
This will generate a command file at app/Console/Commands/SendNewsLetter.php. Open the file and update it:
// app/Console/Commands/SendNewsLetter.php
namespace App\Console\Commands;
use App\Jobs\SendNewsLetterJob;
use App\Models\User;
use Illuminate\Console\Command;
class SendNewsLetter extends Command
{
protected $signature = 'send:newsletter';
protected $description = 'Send weekly newsletter to subscribers.';
public function handle()
{
User::where('status', 'active')->chunk(100, function ($users) {
foreach ($users as $user) {
dispatch(new SendNewsLetterJob($user));
}
});
$this->info('Email scheduled successfully!');
}
}
Scheduling the Newsletter Command
To automate the newsletter dispatch, we'll schedule the command to run weekly. Open the app/Console/Kernel.php file and make the following additions:
// app/Console/Kernel.php
namespace App\Console;
use App\Console\Commands\SendNewsLetter;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected function schedule(Schedule $schedule): void
{
$schedule->command(SendNewsLetter::class)->weekly();
}
protected function commands(): void
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
Scheduling the Task
To ensure the command runs at the scheduled time, execute the following command:
php artisan schedule:work
Additionally, add the following cron job to your server to run Laravel's scheduler:
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
Congratulations! You've successfully set up a weekly newsletter system using Laravel's artisan commands and scheduling features. Your subscribers will now receive engaging newsletters automatically every week.
