bwt-team/laravel-error-mailer

Package to send email notification when something goes wrong

v2.0.0 2017-02-07 08:41 UTC

This package is not auto-updated.

Last update: 2024-03-16 16:58:35 UTC


README

English description | Russian description

Laravel 5 Error Mailer

Latest Stable Version Latest Unstable Version License

This package allows to enable and set up email alerts in case errors appear.

Contents

Setup

Setup this package with composer using the following command:

composer require bwt-team/laravel-error-mailer

Setup in Laravel

After composer update add service provider into providers in config/app.php.

BwtTeam\LaravelErrorMailer\Providers\ErrorMailerServiceProvider::class

This service provider will enable with an option to publish config file to update package settings depending on your needs. After publication this service provider can be disabled, it is not needed for package work. For publication please use:

php artisan vendor:publish --provider="BwtTeam\LaravelErrorMailer\Providers\ErrorMailerServiceProvider" --tag=config

Also, to make package work please register the setup class in bootstrap/app.php. Registration should happen before Application sample is returned.

$app->bind(
    \Illuminate\Foundation\Bootstrap\ConfigureLogging::class,
    \BwtTeam\LaravelErrorMailer\ConfigureLogging::class
);

Setup in Lumen

To send email messages you can use laravel component or create a class for sending yourself by initializing class, which implements \Swift_Transport interface. To set up laravel component:

  • setup component using the following command: composer require illuminate/mail
  • copy config file into config directory, which is stored in root catalogue (or create the directory yourself).
  • Copy config file vendor/bwt-team/laravel-error-mailer/config/error-mailer.php into config directory, which is stored in root directory (or create it yourself if it is missing) and set it up according to your needs.
  • enable service provider in bootstrap/app. $app->register(\Illuminate\Mail\MailServiceProvider::class);
  • setup settings from configuration file. $app->configure('mail'); $app->configure('error-mailer');

To enable sending email alerts you need to create a class instance \BwtTeam\LaravelErrorMailer\Configurators\MailConfigurator in bootstrap/app, its constructor looks like following:

 public function __construct($subject, $to, $from, array $processors = [], $logLevel = Logger::ERROR, \Swift_Transport $sendmailTransport = null)

This class is responsible for sending alert emails by mail but enabling it will disable writing logs into file, which is enabled in lumen by default. In order not to disable writing you need to create a class instance \BwtTeam\LaravelErrorMailer\Configurators\FileConfigurator. Class constructor looks like the following:

 public function __construct($file = null, $logLevel = Logger::DEBUG)

After that you need to pass this instance into with method in \BwtTeam\LaravelErrorMailer\Configurators\MailConfiguratorclass. Each configuration class has options to work with monologue processors. For this you need to pass process instance to class name into constructor or add with the help of addProcessors method. In addition to standard monologue processors, the following out-of-box processors are available:

 \BwtTeam\LaravelErrorMailer\Processors\SqlProcessor::class,
 \BwtTeam\LaravelErrorMailer\Processors\PostDataProcessor::class,
 \BwtTeam\LaravelErrorMailer\Processors\HeadersProcessor::class,

When component is set up you need to pass it to configureMonologUsing method in Application class before it is returned.
Final setup will look like this:

$configurator = new \BwtTeam\LaravelErrorMailer\Configurators\MailConfigurator('subject', 'to@example.com', 'from@example.com');
$configurator->setSendmailTransport(\Swift_MailTransport::newInstance());
$configurator->addProcessors([
     \BwtTeam\LaravelErrorMailer\Processors\SqlProcessor::class,
     \BwtTeam\LaravelErrorMailer\Processors\PostDataProcessor::class,
     \BwtTeam\LaravelErrorMailer\Processors\HeadersProcessor::class,
     \Monolog\Processor\WebProcessor::class
]);
$configurator->with(new \BwtTeam\LaravelErrorMailer\Configurators\FileConfigurator());
$app->configureMonologUsing($configurator);

License

This package is using MIT.