PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Filip Štamcar   PHPMailer Logging with Monolog   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHPMailer Logging with Monolog
Log email sent via PHPMailer using Monolog
Author: By
Last change:
Date: 6 years ago
Size: 974 bytes
 

Contents

Class file image Download
<?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!');