Skip to content

Overview

A simple notification library for sending simple notifications to services such as Email, Slack, Mattermost and MS Teams

Installation

Install via composer.

composer require vespula/notifier

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
require './vendor/autoload.php';

use Vespula\Notifier\Notifier;
use Vespula\Notifier\Adapter\Stdout;
use Vespula\Notifier\Adapter\Email;
use Vespula\Notifier\Adapter\Webhook;
use Laminas\Mail\Message as MailMessage;
use Laminas\Mail\Message\File as FileTransport;
use Laminas\Mail\Message\FileOptions as FileTransportOptions;

$stdout = new Stdout();

$mailMessage = new MailMessage();
$mailTransport = new FileTransport();
$options = new FileTransportOptions([
    'path'=>'/tmp'
]);
$mailTransport->setOptions($options);

$mailMessage->setTo('user@example.com');
$mailMessage->setSubject('Notification');
$mailMessage->setFrom('notification@example.com');

$email = new Email($mailMessage, $mailTransport);

$webhook = new Webhook('http://some.mattermost.org/hooks/xxx-generatedkey-xxx');

$notifier = new Notifier();

// Add adapters using a string key as an identifier
$notifier->addAdapter('stdout', $stdout);
$notifier->addAdapter('email', $email);
$notifier->addAdapter('webhook', $webhook);

// Set the message on all adapters
$notifier->setMessage('This is my message');

// Notify all adapters
$notifier->notify();


// Alternatively, you can set messages on individual adapters and send the notification signal to individual adapters
// Set a message on the email and webhook adapters
$notifier->setMessage('This is my message', ['email', 'webhook']);

// Set a different message on the stdout adapter
$notifier->setMessage('This is my other message', ['stdout']);

// Notify all adapters
$notifier->notify();

// Or Notify specific adapters
$notifier->notify(['email', 'webhook']);

Get all adapters

1
2
3
<?php 

$adapters = $notifier->getAdapters();

Get a single adapter by the key id

1
2
3
<?php 

$adapter = $notifier->getAdapter('webhook');

Set all adapters

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?php 
$adapters = new \Vespula\Notifier\Adapter\AdapterList();
$adapters['stdout'] = new \Vespula\Notifier\Adapter\Stdout();
$adapters['webhook'] = new \Vespula\Notifier\Adapter\Webhook('https://....');

$notifier->setAdapters($adapters);

// You can also just get the empty adapter list right after construction of the notifier

$notifier = new Notifier();

$adapters = $notifier->getAdapters();

$adapters['email'] = $emailAdapter;

$notifier->setAdapters($adapters);