Is there any way to send an email when ever error happened in entire application globally?

Hi Support Team,

Can you please guide me to setup email program that program will send whenever error occurred.

Thanks

Hello,

Slim framework does not include an e-mail component by itself, but you can use an external component like PHPMailer or SwiftMailer.

To send an e-mail on errors, create a service to send out the e-mail and register this in the container. Then override the Slim error handlers (see documentation) to call the notification service.

A minimal example application is given below.

As an alternative, you can look into applications like LogStash and Nagios to analyze the log files and send notifications.

<?php

require_once __DIR__ . '/vendor/autoload.php';

$container = new Slim\Container();

// register e-mail error notifier in container
$container['errorNotifier'] = function($container) {
    return function(\Exception $e) {
        $mail = new PHPMailer();
        // configure mail (port, address) 
        // ...

        $mail->Subject = 'An exception occurred in slim application';
        $mail->Body = '<p>An exception occurred: <tt>'.htmlspecialchars($e->getMessage()).'</tt></p>';

        $mail->send();
    };
};

// replace the error handler with an error handler that calls the notifier service and then the default error handler
$defaultErrorHandler = $container['errorHandler'];
unset($container['errorHandler']);
$container['errorHandler'] = function($container) use ($defaultErrorHandler) {
    return function($request, $response, $exception) use($container, $defaultErrorHandler) {
        $container['errorNotifier']($exception); // send e-maiil to notify that an exception occurred

        return $defaultErrorHandler($request, $response, $exception); // call default error handler
    };
};

// also replace the error handler for PHP errors (PHP 7 only)
$defaultPhpErrorHandler = $container->get('phpErrorHandler');
unset($container['phpErrorHandler']);
$container['phpErrorHandler'] = function($container) use($defaultPhpErrorHandler) {
    return function($request, $response, $exception) use($container, $defaultPhpErrorHandler) {
        $container['errorNotifier']($exception); // send e-maiil to notify that an exception occurred

        return $defaultPhpErrorHandler($request, $response, $exception); // call default error handler
    };
};

$app = new Slim\App($container);

$app->get('/', function($request, $response) {
    // trigger an error
    throw new \Exception('Oops it went wrong.');
});

$app->run();
1 Like

Thank you so much. Will try this solution.

Hi,

We are using * @version 2.6.1 and the given solution is not aviabale in 2.6.1. Can you please provide example or documetation for 2.6.1?

Error: Fatal error: Call to undefined method Slim\Slim::getContainer()

Thanks

Hello,

I haven’t worked with Slim version 2, so I can’t give you an example. The Slim 2 documentation may help you. The approach is the same for Slim 2 and Slim 3:

Thank You so much. Will check and get back to You.