Package Data | |
---|---|
Maintainer Username: | haythammones |
Maintainer Contact: | haytham.mones@gmail.com (Haytham Mones) |
Package Create Date: | 2022-01-21 |
Package Last Update: | 2024-04-20 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-10-27 03:00:20 |
Package Statistics | |
---|---|
Total Downloads: | 9,251 |
Monthly Downloads: | 3,488 |
Daily Downloads: | 13 |
Total Stars: | 154 |
Total Watchers: | 1 |
Total Forks: | 11 |
Total Open Issues: | 1 |
A simple package to create and send digest emails every certain period or when the amount reaches a certain threshold. Usage Examples:
Via Composer
composer require hmones/laravel-digest
To publish the package configuration
php artisan vendor:publish --tag=laravel-digest-config
The configuration file contains the following parameters:
method
: the method that you would like to use to send emails, it takes two values, queue
or send
DIGEST_METHOD
queue
frequency.enabled
whether you would like to enable sending emails every certain period, if not enabled emails will not be scheduled
DIGEST_FREQUENCY_ENABLED
true
frequency.daily.time
if frequency is enabled this parameter is used as the time to send the daily digest emails
DIGEST_DAILY_TIME
00:00
frequency.weekly.time
if frequency is enabled this parameter is used as the time to send the weekly digest emails
DIGEST_WEEKLY_TIME
00:00
frequency.monthly.time
if frequency is enabled this parameter is used as the time to send the monthly digest emails
DIGEST_MONTHLY_TIME
00:00
frequency.weekly.day
if frequency is enabled this parameter is used as the day to send the weekly digest emails (1 is Sunday and 7 is Saturday)
DIGEST_WEEKLY_DAY
1
frequency.monthly.day
if frequency is enabled this parameter is used as the day to send the monthly digest emails
DIGEST_MONTHLY_DAY
1
frequency.custom
you can set as much custom frequency definitions as you want and the parameter takes a valid cron expressionamount.enabled
whether you would like to enable sending emails every certain amount per batch
DIGEST_AMOUNT_ENABLED
true
amount.threshold
the number of emails after which an digest email should be sent.
DIGEST_AMOUNT_THRESHOLD
10
To create an email digest, make sure you have the following first:
Example: Sending a digest email every time 10 new users register on the website with a summary of their names.
config\laravel-digest.php
by setting amount.enabled => true
and frequency.enabled => false
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class UserCreatedMailable extends Mailable
{
use Queueable, SerializesModels;
public $data;
public function __construct(array $data)
{
$this->data = $data;
}
public function build(): Mailable
{
return $this->view('userCreated')->subject('10 new users are registered')->to('email@test.com');
}
}
resources\userCreated.blade.php
<html>
<head><title>Sample Email</title></head>
<body>
<h1>The following users have just registered:</h1>
<ol>
@foreach($data as $record)
<li>{{$record['name']}}</li>
@endforeach
</ol>
</body>
</html>
<?php
namespace App\Observers;
use App\Mail\UserCreatedMailable;
use App\Models\User;
use Hmones\LaravelDigest\Facades\Digest;
class UserObserver
{
public function created(User $user)
{
$batchId = 'userCreated';
$mailable = UserCreatedMailable::class;
//Data can also be set to null if you don't want to attach any data to the email
$data = ['name' => $user->name];
//Frequency can take values such as daily, weekly, monthly, custom or an integer threshold 10, 20 ...etc
$frequency = 10;
Digest::add($batchId, $mailable, $data, $frequency);
}
}
Please see the changelog for more information on what has changed recently.
composer test
Please see contributing.md for details and a todolist.
If you discover any security related issues, please email author email instead of using the issue tracker.
Please see the license file for more information.