MonologPHPMailer is a PHPMailer handler for Monolog. It enables you to send logs to emails with PHPMailer.
MonologPHPMailer requires PHP 5.5.0 or higher, Monolog 1.x and PHPMailer 6.x. Monolog 2.x is not supported, but it will be supported in the future.
The reccomended way to install MonologPHPMailer is with Composer, dependency manager for PHP.
You should just add filips123/monolog-phpmailer
to your project dependencies in composer.json
. It will also install Monolog and PHPMailer, but it is reccomended to add them manually to composer.json
.
{
"require": [
"monolog/monolog": "^1.0",
"phpmailer/phpmailer": "^6.0",
"filips123/monolog-phpmailer": "^1.0"
]
}
Do not forget to run composer install
and add require 'vendor/autoload.php';
to your main script.
Alternatively, you could download PHPMailerHandler.php
from GitHub and then manually include it in your script. You also have to install Monolog and PHPMailer manually.
You should just add handler MonologPHPMailer\PHPMailerHandler
to your logger. It's first argument must be PHPMailer instance.
<?php
use MonologPHPMailer\PHPMailerHandler;
use Monolog\Formatter\HtmlFormatter;
use Monolog\Logger;
use Monolog\Processor\IntrospectionProcessor;
use Monolog\Processor\MemoryUsageProcessor;
use Monolog\Processor\WebProcessor;
use PHPMailer\PHPMailer\PHPMailer;
require __DIR__ . '/vendor/autoload.php';
$mailer = new PHPMailer(true);
$logger = new Logger('logger');
$mailer->isSMTP();
$mailer->Host = 'smtp.example.com';
$mailer->SMTPAuth = true;
$mailer->Username = 'server@example.com';
$mailer->Password = 'password';
$mailer->setFrom('server@example.com', 'Logging Server');
$mailer->addAddress('user@example.com', 'Your Name');
$logger->pushProcessor(new IntrospectionProcessor);
$logger->pushProcessor(new MemoryUsageProcessor);
$logger->pushProcessor(new WebProcessor);
$handler = new PHPMailerHandler($mailer);
$handler->setFormatter(new HtmlFormatter);
$logger->pushHandler($handler);
$logger->error('Error!');
$logger->alert('Something went wrong!');