Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
Synchro committed Sep 11, 2015
0 parents commit a1e0e27
Show file tree
Hide file tree
Showing 8 changed files with 431 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.phar
vendor/

33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#PHPMailer logger for Apix Log

An extension for the [Apix/Log](https://github.com/frqnck/apix-log) PSR-3 logger that sends log messages via email using [PHPMailer](https://github.com/PHPMailer/PHPMailer).

##Usage

Create an Apix PhpMailer Log instance, providing a pre-configured PHPMailer instance to the constructor.
This instance will be used for all subsequent messages.

By default the logger sends an email for each individual log message received, which can be quite inefficient, so call `$logger->setDeferred(true)` to save up the log messages and send them all in one message on `__destruct`.

We suggest you enable exceptions in your PHPMailer instance otherwise you may not be told about problems sending your log messages.

##Example

```php
// Create a PHPMailer instance with exceptions enabled
$mailer = new \PHPMailer(true);
$mailer->addAddress('[email protected]', 'Log Mailbox');
$mailer->setFrom('[email protected]', 'My App');
$mailer->isSMTP();
$mailer->SMTPAuth = true;
$mailer->Host = 'tls://mail.example.com:587';
$mailer->Username = 'user';
$mailer->Password = 'pass';
$mailer->isHTML(false);
$mailer->Subject = 'Error log';

$logger = new Apix\Logger\PhpMailer($mailer);
$logger->setDeferred(true);
$logger->info('Log me!');
$logger->error('Log me too!');
```
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "phpmailer/apix-log-phpmailer",
"description": "A PHPMailer logger for Apix log",
"type": "library",
"minimum-stability": "stable",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Marcus Bointon",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.3",
"apix/log": "^1.1",
"phpmailer/phpmailer": "^5.2"
},
"autoload": {
"psr-4": {
"Apix\\Log\\Logger\\": "src/",
"Apix\\Log\\Logger\\tests\\": "tests/"
}
}
}
170 changes: 170 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit
backupGlobals="false"
backupStaticAttributes="false"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="Apix Log Unit Tests">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>

</phpunit>

<!-- vim: set tabstop=4 shiftwidth=4 expandtab: -->
60 changes: 60 additions & 0 deletions src/PhpMailer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Apix PHPMailer logger.
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License
* @author Marcus Bointon <[email protected]>
*/

namespace Apix\Log\Logger;

use Apix\Log\Exception;
use Psr\Log\InvalidArgumentException;

/**
* Apix logger for sending logs via PHPMailer.
*
* @author Marcus Bointon <[email protected]>
*/
class PhpMailer extends AbstractLogger implements LoggerInterface
{
/**
* A PHPMailer instance to use for sending.
* @type \PHPMailer
*/
protected $phpmailer;

/**
* Constructor.
* Note PHPMailer is *NOT* namespaced.
* @param \PHPMailer $phpmailer A preconfigured PHPMailer instance to use for sending.
*/
public function __construct($phpmailer)
{
if (!$phpmailer instanceof \PHPMailer) {
throw new InvalidArgumentException(
'Must be an instance of \PHPMailer', 1
);
}

if (count($phpmailer->getToAddresses()) < 1) {
throw new InvalidArgumentException(
'No valid email address set in \PHPMailer'
);
}

$this->phpmailer = $phpmailer;
}

/**
* {@inheritDoc}
*/
public function write(array $log)
{
$this->phpmailer->Body = $log['msg'];
try {
return $this->phpmailer->send();
} catch (\phpmailerException $e) {
throw new Exception($e->getMessage(), $e->getCode(), $e);
}
}
}
Loading

0 comments on commit a1e0e27

Please sign in to comment.