Overview

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

Source is at https://bitbucket.org/vespula/vespula.notifier

Installation

Install via composer.

composer require vespula/notifier

Basic Usage

<?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

<?php
$adapters = $notifier->getAdapters();

Get a single adapter by the key id

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

Set all adapters

<?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);