deslynx/alert-bundle

Send alert email service and specialized Monolog handler

Installs: 4

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 0

Open Issues: 0

Type:symfony-bundle

v1.1.1 2024-07-18 08:51 UTC

This package is auto-updated.

Last update: 2024-09-18 09:45:28 UTC


README

Latest stable version License PHPStan Enabled PHPStan Level max

Installation

Make sure Composer is installed globally, as explained in the installation chapter of the Composer documentation.

Applications that use Symfony Flex

Open a command console, enter your project directory and execute:

composer require deslynx/alert-bundle

Applications that don't use Symfony Flex

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

composer require deslynx/alert-bundle

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in the config/bundles.php file of your project:

// config/bundles.php

return [
    // ...
    DesLynx\AlertBundle\DesLynxAlertBundle::class => ['all' => true],
];

Configuration

# /config/packages/deslynx_alert.yaml
deslynx_alert:

    # The sender of the email, like "Name <email@address.com>" or a simple email.
    from: ~ # Required

    # A list of always added recipients for the email, like "Name <email@address.com>" or a simple email.
    to: [] # Required

    # Your project name. Included in the email subject
    projectName: 'My project'

    # If you want to use a custom email transport set this to the name of the transport.
    transport: null

# If you need another configuration for specific environments you can overwrite some configs
#when@dev:
#  deslynx_alert:
#    from: ~
#    to: []
#    projectName: 'My project'
#    transport: null

Important note

This bundle rely on the Symfony Mailer component for sending email so be sure to have it configured.

Usage (service)

This bundle provide a single service for sending an alert email which you can autowire by using the AlertHelper type-hint:

// src/Controller/SomeController.php

use DesLynx\AlertBundle\Service\AlertHelper;
//...

class SomeController
{
    public function index(AlertHelper $alertHelper) {
        $alertHelper->sendAlert(
            'A significant subject.',
            'A significant message. Either text or HTML.',
            false, // Optional. Set to true if the message is HTML
            ['email1@address.com', 'email2@address.com' /*, ...*/] // Optional. A list of recipients to add in cc in addition to the globally defined recipients (see configuration)
         );
    }    
}

Usage (Monolog Handler)

This bundle provide a Monolog Handler using the AlertHelper service. It allows to add a monolog handler config to send an alert email with the full log stack on every critical error happening in the project.

To enable this functionality, require the MonologBundle if not already done then just add this to your monolog config:

# config/packages/monolog.yaml
monolog:
  handlers:
    # ...
    deslynx_critical:
      type: fingers_crossed
      action_level: critical
      handler: deslynx_deduplicated
    deslynx_deduplicated:
      type: deduplication
      handler: deslynx_alert_mailer
    deslynx_alert_mailer:
      type: service
      id: DesLynx\AlertBundle\Monolog\Handler\AlertMailerHandler
      level: debug