oneduo / laravel-mail-scheduler
A simple way to send batch emails
Requires
- php: ^8.1
- ext-json: *
- illuminate/contracts: ^8.0 || ^9.0 || ^10.0 || ^11.0
- spatie/laravel-package-tools: ^1.12
Requires (Dev)
- guzzlehttp/guzzle: ^7.0.1
- laravel/pint: ^1.2
- mockery/mockery: ^1.5
- orchestra/testbench: ^6.0 || ^7.6 || ^8.0 || ^9.0
- pestphp/pest: ^2.0
- pestphp/pest-plugin-faker: ^1.0 || ^2.0
- pestphp/pest-plugin-laravel: ^1.2 || ^2.0
- pestphp/pest-plugin-mock: ^1.0 || ^2.0
- spatie/laravel-ray: ^1.29
README
This package gives you the ability to send emails in batches. After creating ScheduledEmails you may send emails using the auto schedule feature or registering the command in the Console kernel yourself.
Table of Contents
Getting Started
Prerequisites
This package requires the following :
- PHP 8.1 or higher
- Laravel 8.0 or higher
Installing
To get started, you will need to install the following dependencies :
composer require oneduo/laravel-mail-scheduler
That's it, you're ready to go!
Configuration
You may publish the package's configuration by running the following command :
php artisan vendor:publish --tag="mail-scheduler-config"
Note You can find details about the configuration options in the configuration file section.
Usage
The package provides a fluent facade to create a scheduled email:
<?php use App\Mail\OrderShipped; use Oneduo\MailScheduler\Support\Facades\ScheduledEmail; $instance = ScheduledEmail::mailable(new OrderShipped) ->to(['john@doe.com']) ->save();
Encryption
For security reasons you may want to encrypt the mailable to protect sensible data. You may use the encrypted
method:
<?php use App\Mail\OrderShipped; use Oneduo\MailScheduler\Support\Facades\ScheduledEmail; $instance = ScheduledEmail::mailable(new OrderShipped) ->to(['john@doe.com']) ->encrypted() // will encrypt the mailable in database ->save();
Configure mailer
You may want to use a specific mailer for schedule email.
<?php use App\Mail\OrderShipped; use Oneduo\MailScheduler\Support\Facades\ScheduledEmail; $instance = ScheduledEmail::mailable(new OrderShipped) ->to(['john@doe.com']) ->mailer('my_mailer') // mailer defined in config/mail.php ->save();
Link email to a source model
You may want to link a ScheduledEmail instance to one of your models using a morphTo
relationship. It could be a user or a product. It's up to you.
<?php use App\Mail\OrderShipped; use App\Models\Product; use Oneduo\MailScheduler\Support\Facades\ScheduledEmail; $product = Product::query()->first(); $instance = ScheduledEmail::mailable(new OrderShipped($product)) ->to(['john@doe.com']) ->encrypted() // will encrypt the mailable in database ->source($product) // ->save();
<?php namespace App\Models\Product; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphMany; use Oneduo\MailScheduler\Models\ScheduledEmail; class Product extends Model { public function emails(): MorphMany { return $this->morphMany(ScheduledEmail::class, 'source'); } }
Send emails
The package can register the command for you when auto_schedule
is true. You may configure the CRON expression with schedule_cron
.
If you want more control on the scheduler, you may disable the auto_schedule
and register the command yourself:
<?php namespace App\Console; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; class Kernel extends ConsoleKernel { /** * Define the application's command schedule. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */ protected function schedule(Schedule $schedule) { $schedule->command('mail-scheduler:send') ->everyMinute() ->between('08:00', '18:00'); } /** * Register the commands for the application. * * @return void */ protected function commands() { $this->load(__DIR__.'/Commands'); require base_path('routes/console.php'); } }
Error handling
If an exception occurs while sending an email, the exception message and stacktrace will be saved into the model. The command will resend emails with an error status till max_attempts
is reached.
Configuration file
Authors
See also the list of contributors who participated in this project.
Changelog
Please see CHANGELOG for more information what has changed recently.
Security
Please review our security policy on how to report security vulnerabilities.
Contributing
Please see CONTRIBUTING for details.
License
The MIT License (MIT). Please see License File for more information.