Download the PHP package binarcode/laravel-mailator without Composer
On this page you can find all versions of the php package binarcode/laravel-mailator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download binarcode/laravel-mailator
More information about binarcode/laravel-mailator
Files in binarcode/laravel-mailator
Package laravel-mailator
Short Description Laravel email scheduler
License MIT
Homepage https://github.com/binarcode/laravel-mailator
Informations about the package laravel-mailator

Laravel Mailator provides a featherweight system for configure email scheduler and email templates based on application events.
Installation
You can install the package via composer:
Publish
Publish migrations: php artisan vendor:publish --tag=mailator-migrations
Publish config: php artisan vendor:publish --tag=mailator-config
Usage
It has mainly 2 directions of usage:
-
Schedule emails sending (or actions triggering)
- Email Templates & Placeholders
Scheduler
To set up a mail to be sent after or before an event, you can do this by using the Scheduler facade.
Here is an example of how to send the invoice reminder email 3 days before the $invoice->due_date:
Let's explain what each line means.
Mailable
This should be an instance of laravel Mailable.
Recipients
This should be a list or valid emails where the email will be sent.
It could be an array of emails as well.
Weeks
This should be a number of weeks the email should be delayed.
Days
This should be a number of days the email should be delayed.
Hours
Instead of days() you can use hours() as well.
Minutes
If your scheduler run by minute, you can also use minutes() to delay the email.
Before
The before constraint accept a CarbonInterface and indicates from when scheduler should start run the mail or action. For instance:
says, send this email 1 day before 02 June 2021, so basically the email will be scheduled for 01 June 2021.
After
The after constraint accept a CarbonInterface as well. The difference, is that it inform scheduler to send it after the specified timestamp. Say we want to send a survey email 1 week after the order is placed:
Precision
Hour Precision
The precision method provides fine-grained control over when emails are sent using MailatorSchedule. It allows you to specify specific hours or intervals within a 24-hour period. Here's an example of how to use the precision method:
This will schedule the email dispatch between '03:00:00' AM and '04:59:59' AM.
or
This will schedule the email dispatch between '01:00:00' AM and '01:59:59'.
You can continue this pattern to specify the desired hour(s) within the range of 1 to 24.
Important: When using the precision feature in the Mailator scheduler, it is recommended to set the scheduler to run at intervals that are less than an hour. You can choose intervals such as every 5 minutes, 10 minutes, 30 minutes, or any other desired duration.
Constraint
The constraint() method accept an instance of Binarcode\LaravelMailator\Constraints\SendScheduleConstraint. Each constraint will be called when the scheduler will try to send the email. If all constraints return true, the email will be sent.
The constraint() method could be called many times, and each constraint will be stored.
Since each constraint will be serialized, it's very indicated to use Illuminate\Queue\SerializesModels trait, so the serialized models will be loaded properly, and the data stored in your storage system will be much less.
Let's assume we have this BeforeInvoiceExpiresConstraint constraint:
Constraintable
Instead of defining the constraint from the mail definition, sometimes it could be more readable if you define it directly into the mailable class:
Action
Using Scheduler you can even define your custom action:
The CustomAction should implement the Binarcode\LaravelMailator\Actions\Action class.
Target
You can link the scheduler with any entity like this:
and then in the Invoice model you can get all emails related to it:
Mailator provides the Binarcode\LaravelMailator\Models\Concerns\HasMailatorSchedulers trait you can put in your Invoice model, so the relations will be loaded.
Daily
By default, scheduler run the action, or send the email only once. You can change that, and use a daily reminder till the constraint returns a truth condition:
This scheduler will send the InvoiceReminderMailable email daily starting with 13 June 2021 (one week before the expiration date).
How to stop the email sending if the invoice was paid meanwhile? Simply adding a constraint that will do not send it:
and the constraint handle method could be something like this:
Stop conditions
There are few ways email stop to be sent.
The first condition, is that if for some reason sending email fails 3 times, the MailatorSchedule will be marked as completed_at. Number of times could be configured in the config file mailator.scheduler.mark_complete_after_fails_count.
Any successfully sent mail, that should be sent only once, will be marked as completed_at.
Stopable
You can configure your scheduler to be marked as completed_at if in the you custom constraint returns a falsy condition. Back to our InvoiceReminderMailable, say the invoice expires on 20 June, we send the first reminder on 13 June, then the second reminder on 14 June, if the client pay the invoice on 14 June the InvoicePaidConstraint will return a falsy value, so there is no reason to try to send the invoice reminder on 15 June again. So the system could mark this scheduler as completed_at.
To do so, you can use the stopable() method.
Unique
You can configure your scheduler to store a unique relationship with the target class for mailable by specifying:
ie:
This will store a single scheduler for the $user.
Events
Mailator has few events you can use.
If your mailable class extends the Binarcode\LaravelMailator\Contracts\Beforable, you will be able to inject the before method, that will be called right before the sending the email.
If your mailable class extends the Binarcode\LaravelMailator\Contracts\Afterable, you will be able to inject the after method, that will be called right after the mail has being sent.
And latest, after each mail has being sent, mailator will fire the Binarcode\LaravelMailator\Events\ScheduleMailSentEvent event, so you can listen for it.
Run
Now you have to run a scheduler command in your Kernel, and call:
Package provides the Binarcode\LaravelMailator\Console\MailatorSchedulerCommand command you can put in your Console Kernel:
Templating
To create an email template:
Adding some placeholders with description to this template:
To use the template, you simply have to add the WithMailTemplate trait to your mailable.
This will enforce you to implement the getReplacers method, this should return an array of replacers to your template.
The array may contain instances of Binarcode\LaravelMailator\Replacers\Replacer or even Closure instances.
Mailator shipes with a builtin replacer ModelAttributesReplacer, it will automaticaly replace attributes from the
model you provide to placeholders.
The last step is how to say to your mailable what template to use. This could be done into the build method as shown bellow:
Testing
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] or message me on twitter instead of using the issue tracker.
Credits
- Eduard Lupacescu
- All Contributors
License
The MIT License (MIT). Please see License File for more information.