codekanzlei / cake-notifications
CakePHP3 Notifications Plugin
Installs: 38 445
Dependents: 1
Suggesters: 0
Security: 0
Stars: 8
Watchers: 4
Forks: 6
Open Issues: 0
Type:cakephp-plugin
pkg:composer/codekanzlei/cake-notifications
Requires
- php: >=7.2
- cakephp/cakephp: >=4.0.0
- josegonzalez/cakephp-queuesadilla: ^0.7.0
Requires (Dev)
- phpstan/phpstan: ^0.11
- phpunit/phpunit: ^6.0
- scherersoftware/coding-standard: ^4.0.0
- 4.x-dev
- v4.0.0-rc3
- v4.0.0-rc2
- v4.0.0-rc1
- v3.1.1
- v3.1.0
- v3.0.2
- v3.0.1
- v3.0.0
- v2.x-dev
- v2.2.1
- v2.2.0
- v2.1.2
- v2.1.1
- v2.1
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0
- 1.1.x-dev
- v1.1.33
- v1.1.32
- v1.1.31
- v1.1.30
- v1.1.29
- v1.1.28
- v1.1.27
- v1.1.26
- v1.1.25
- v1.1.24
- v1.1.23
- v1.1.22
- v1.1.21
- v1.1.20
- v1.1.19
- v1.1.18
- v1.1.17
- v1.1.16
- v1.1.15
- v1.1.14
- v1.1.13
- v1.1.12
- v1.1.11
- v1.1.10
- v1.1.9
- v1.1.8
- v1.1.7
- v1.1.6
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1
- v1.0.5
- v1.0.3
- v1.0.2
- dev-remove-wrong-error-level
- dev-master
This package is auto-updated.
Last update: 2025-10-24 05:53:31 UTC
README
A CakePHP 3.8 notification plugin which can send out emails asynchronously through to the cakephp-queuesadilla job queue.
Requirements
- CakePHP Queuesadilla Plugin 3.0
- PHP 7.1+
Installation
1. Install the plugin via composer
composer require codekanzlei/cake-notifications
2. Load the plugin in your src/Application.php
$this->addPlugin('Notifications');
3. Configure config/app.php
Set your default locale in a config file, for example in app.php.
This config is mandatory and will cause an exception if not set.
'Notifications' => [
'defaultLocale' => 'en_US'
]
You can also override the queue options like attempts, attempts_delay, delay, expires_in and queue.
'Notifications' => [
'queueOptions' => [
'queue' => 'notification'
]
]
This doesn't affect the use of queueOptions() later. You can still override the options there.
Also, be sure to set up the the cakephp-queuesadilla plugin config. You can find an example config here: https://cakephp-queuesadilla.readthedocs.io/en/latest/.
Or you can find available config options inside your used Engine file (vendor/josegonzalez/queuesadilla/src/josegonzalez/Queuesadilla/Engine/*Engine.php) inside the $baseConfig property.
IMPORTANT: Set "date.timezone" in your cli/php.ini to an appropriate value, else notifications with a delay_until could be sent out at the wrong time.
Usage
The EmailNotification is completely compatible with the CakePHP Email.
Add the following to your class where you want to send an email:
use Notifications\Notification\EmailNotification;
Then simply create a new EmailNotification object.
$email = new EmailNotification();
$email->to('john.doe@example.com')
->setSubject('Send with cake-notifications v2')
->send('Hello :)');
You can chain all methods provided by the CakePHP Email Class http://book.cakephp.org/3.0/en/core-libraries/email.html
Additional, following functions are available:
send( array|string|null $content null )
Send out the email immediately. before- and afterSend callbacks are still available
setLocale( string|null $locale null )
Set the locale for the notification. If null, Configure::read('Notifications.defaultLocale') is used.
push()
Push the email into the queue to send it asynchronous
setQueueOptions( array $options null )
You can change some of the default options from the cakephp-queuesadilla plugin.
Supported options:
attemptshow often the notification will be executed again after a failureattempts_delayhow long it takes in seconds until the notification will be executed againdelayhow long it takes until the notification will be executed for the first time in secondsexpires_inhow long the notification will stay in the queue in secondsqueuename of the queue
setBeforeSendCallback( array|string|null $class null, array $args [] )
Pass a callable as the $class parameter. Static and none-static functions are supported.
$email->beforeSendCallback(['Foo', 'bar'], ['first_param', 'second_param'])
This will call the bar method inside the Foo class with two parameters before the email is send.
To manipulate the EmailNotification instance before sending, the beforeSendCallback may return a function taking the notification instance reference and for example changing the profile.
The bar method may then look something like this:
public function bar($first_param, $second_param)
{
// do something
return function (&$instance) {
$instance->profile([
'from' => 'email@example.com'
]);
};
}
setAfterSendCallback( array|string|null $class null, array $args [] )
Pass a callable as the $class parameter. Static and none-static functions are supported.
$email-> afterSendCallback(['Foo::bar'], ['first_param', 'second_param'])
This will call the static bar method inside the Foo class with two parameters after the email was send.
addBeforeSendCallback( array|string|null $class null, array $args [] )
Add an additional callback to beforeSend.
addAfterSendCallback( array|string|null $class null, array $args [] )
Add an additional callback to afterSend.
