Skip to content

Commit

Permalink
Add support for Monolog 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
filips123 committed Sep 7, 2018
1 parent 37cfcaf commit 6d6309c
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 77 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ MonologPHPMailer is a [PHPMailer][link-phpmailer] handler for [Monolog][link-mon

### Requirements

MonologPHPMailer requires *[PHP][link-php] 5.5.0* or higher, *[Monolog][link-monolog] 1.x* and *[PHPMailer][link-phpmailer] 6.x*. Monolog 2.x **is not** supported, but it will be supported in the future.
MonologPHPMailer requires *[PHP][link-php] 5.5.0* or higher, *[Monolog][link-monolog] 1.x or 2.x* and *[PHPMailer][link-phpmailer] 6.x*.

### Using Composer

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "filips123/monolog-phpmailer",
"description": "PHPMailer handler for Monolog.",
"description": "PHPMailer handler for Monolog",
"keywords": [
"log",
"logging",
Expand Down
82 changes: 7 additions & 75 deletions src/PHPMailerHandler.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
<?php

namespace MonologPHPMailer;

use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\MailHandler;
use Monolog\Logger;
use PHPMailer\PHPMailer\PHPMailer;

/**
* PHPMailer handler for Monolog.
* Loads required handler.
*
* It uses [PHPMailer](https://github.com/PHPMailer/PHPMailer/) to send emails.
* It loads required handler class for Monolog 1.x or 2.x depending on its version.
*
* @since 1.0.0
*
Expand All @@ -21,71 +13,11 @@
*
* @package MonologPHPMailer
*/
class PHPMailerHandler extends MailHandler
{
/**
* A PHPMailer instance.
*
* @var PHPMailer $mailer
*/
protected $mailer;

/**
* Constructs the PHPMailer handler.
*
* @param PHPMailer $mailer A PHPMailer instance to use.
* @param int|string $level The minimum logging level at which this handler will be triggered.
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not.
*
* @return void
*/
public function __construct(PHPMailer $mailer, $level = Logger::ERROR, $bubble = true)
{
parent::__construct($level, $bubble);
$this->mailer = $mailer;
}

/**
* Sends a mail with the given content.
*
* @param string $content Formatted email body to be sent.
* @param array $records The array of log records that formed this content.
*
* @return void
*/
protected function send($content, array $records)
{
$mailer = $this->buildMessage($content, $records);
$mailer->send();
}

/**
* Builds a message to be sent.
*
* @param string $content Formatted email body to be sent.
* @param array $records The array of log records that formed this content.
*
* @return PHPMailer Builded message.
*/
public function buildMessage($content, $records)
{
$mailer = clone $this->mailer;

if (substr($content, 0, 1) == '<') {
$mailer->isHTML(true);
}

// @codingStandardsIgnoreStart
if ($records) {
$subjectFormatter = new LineFormatter($mailer->Subject);
$mailer->Subject = $subjectFormatter->format($this->getHighestRecord($records));
}
// @codingStandardsIgnoreEnd

// @codingStandardsIgnoreStart
$mailer->Body = $content;
// @codingStandardsIgnoreEnd
use Monolog\Logger;

return $mailer;
}
if (Logger::API == 1) {
class_alias('MonologPHPMailer\PHPMailerHandler1', 'MonologPHPMailer\PHPMailerHandler');
} else {
class_alias('MonologPHPMailer\PHPMailerHandler2', 'MonologPHPMailer\PHPMailerHandler');
}
89 changes: 89 additions & 0 deletions src/PHPMailerHandler1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace MonologPHPMailer;

use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\MailHandler;
use Monolog\Logger;
use PHPMailer\PHPMailer\PHPMailer;

/**
* PHPMailer handler for Monolog 1.x.
*
* It uses [PHPMailer](https://github.com/PHPMailer/PHPMailer/) to send emails.
*
* @since 1.0.0
*
* @author Filip Š <[email protected]>
*
* @license MIT
*
* @package MonologPHPMailer
*/
class PHPMailerHandler1 extends MailHandler
{
/**
* A PHPMailer instance.
*
* @var PHPMailer $mailer
*/
protected $mailer;

/**
* Constructs the PHPMailer handler.
*
* @param PHPMailer $mailer A PHPMailer instance to use.
* @param int|string $level The minimum logging level at which this handler will be triggered.
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not.
*/
public function __construct(PHPMailer $mailer, $level = Logger::ERROR, $bubble = true)
{
parent::__construct($level, $bubble);
$this->mailer = $mailer;
}

/**
* Sends a mail with the given content.
*
* @param string $content Formatted email body to be sent.
* @param array $records The array of log records that formed this content.
*
* @return void
*/
protected function send($content, array $records)
{
$mailer = $this->buildMessage($content, $records);
$mailer->send();
}

/**
* Builds a message to be sent.
*
* @param string $content Formatted email body to be sent.
* @param array $records The array of log records that formed this content.
*
* @return PHPMailer Builded message.
*/
public function buildMessage($content, array $records)
{
$mailer = clone $this->mailer;

if (substr($content, 0, 1) == '<') {
$mailer->isHTML(true);
}

// @codingStandardsIgnoreStart
if ($records) {
$subjectFormatter = new LineFormatter($mailer->Subject);
$mailer->Subject = $subjectFormatter->format($this->getHighestRecord($records));
}
// @codingStandardsIgnoreEnd

// @codingStandardsIgnoreStart
$mailer->Body = $content;
// @codingStandardsIgnoreEnd

return $mailer;
}
}
89 changes: 89 additions & 0 deletions src/PHPMailerHandler2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace MonologPHPMailer;

use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\MailHandler;
use Monolog\Logger;
use PHPMailer\PHPMailer\PHPMailer;

/**
* PHPMailer handler for Monolog 2.x.
*
* It uses [PHPMailer](https://github.com/PHPMailer/PHPMailer/) to send emails.
*
* @since 1.0.0
*
* @author Filip Š <[email protected]>
*
* @license MIT
*
* @package MonologPHPMailer
*/
class PHPMailerHandler2 extends MailHandler
{
/**
* A PHPMailer instance.
*
* @var PHPMailer $mailer
*/
protected $mailer;

/**
* Constructs the PHPMailer handler.
*
* @param PHPMailer $mailer A PHPMailer instance to use.
* @param int|string $level The minimum logging level at which this handler will be triggered.
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not.
*/
public function __construct(PHPMailer $mailer, $level = Logger::ERROR, bool $bubble = true)
{
parent::__construct($level, $bubble);
$this->mailer = $mailer;
}

/**
* Sends a mail with the given content.
*
* @param string $content Formatted email body to be sent.
* @param array $records The array of log records that formed this content.
*
* @return void
*/
protected function send(string $content, array $records): void
{
$mailer = $this->buildMessage($content, $records);
$mailer->send();
}

/**
* Builds a message to be sent.
*
* @param string $content Formatted email body to be sent.
* @param array $records The array of log records that formed this content.
*
* @return PHPMailer Builded message.
*/
public function buildMessage(string $content, array $records): PHPMailer
{
$mailer = clone $this->mailer;

if (substr($content, 0, 1) == '<') {
$mailer->isHTML(true);
}

// @codingStandardsIgnoreStart
if ($records) {
$subjectFormatter = new LineFormatter($mailer->Subject);
$mailer->Subject = $subjectFormatter->format($this->getHighestRecord($records));
}
// @codingStandardsIgnoreEnd

// @codingStandardsIgnoreStart
$mailer->Body = $content;
// @codingStandardsIgnoreEnd

return $mailer;
}
}

0 comments on commit 6d6309c

Please sign in to comment.