Skip to content

Commit a1e0e27

Browse files
committed
Initial import
0 parents  commit a1e0e27

8 files changed

+431
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
composer.phar
2+
vendor/
3+

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#PHPMailer logger for Apix Log
2+
3+
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).
4+
5+
##Usage
6+
7+
Create an Apix PhpMailer Log instance, providing a pre-configured PHPMailer instance to the constructor.
8+
This instance will be used for all subsequent messages.
9+
10+
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`.
11+
12+
We suggest you enable exceptions in your PHPMailer instance otherwise you may not be told about problems sending your log messages.
13+
14+
##Example
15+
16+
```php
17+
// Create a PHPMailer instance with exceptions enabled
18+
$mailer = new \PHPMailer(true);
19+
$mailer->addAddress('[email protected]', 'Log Mailbox');
20+
$mailer->setFrom('[email protected]', 'My App');
21+
$mailer->isSMTP();
22+
$mailer->SMTPAuth = true;
23+
$mailer->Host = 'tls://mail.example.com:587';
24+
$mailer->Username = 'user';
25+
$mailer->Password = 'pass';
26+
$mailer->isHTML(false);
27+
$mailer->Subject = 'Error log';
28+
29+
$logger = new Apix\Logger\PhpMailer($mailer);
30+
$logger->setDeferred(true);
31+
$logger->info('Log me!');
32+
$logger->error('Log me too!');
33+
```

composer.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "phpmailer/apix-log-phpmailer",
3+
"description": "A PHPMailer logger for Apix log",
4+
"type": "library",
5+
"minimum-stability": "stable",
6+
"license": "BSD-3-Clause",
7+
"authors": [
8+
{
9+
"name": "Marcus Bointon",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"require": {
14+
"php": ">=5.3",
15+
"apix/log": "^1.1",
16+
"phpmailer/phpmailer": "^5.2"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"Apix\\Log\\Logger\\": "src/",
21+
"Apix\\Log\\Logger\\tests\\": "tests/"
22+
}
23+
}
24+
}

composer.lock

+170
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpunit.xml.dist

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit
4+
backupGlobals="false"
5+
backupStaticAttributes="false"
6+
colors="false"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false"
12+
syntaxCheck="false"
13+
bootstrap="tests/bootstrap.php"
14+
>
15+
<testsuites>
16+
<testsuite name="Apix Log Unit Tests">
17+
<directory suffix="Test.php">tests</directory>
18+
</testsuite>
19+
</testsuites>
20+
21+
</phpunit>
22+
23+
<!-- vim: set tabstop=4 shiftwidth=4 expandtab: -->

src/PhpMailer.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Apix PHPMailer logger.
4+
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License
5+
* @author Marcus Bointon <[email protected]>
6+
*/
7+
8+
namespace Apix\Log\Logger;
9+
10+
use Apix\Log\Exception;
11+
use Psr\Log\InvalidArgumentException;
12+
13+
/**
14+
* Apix logger for sending logs via PHPMailer.
15+
*
16+
* @author Marcus Bointon <[email protected]>
17+
*/
18+
class PhpMailer extends AbstractLogger implements LoggerInterface
19+
{
20+
/**
21+
* A PHPMailer instance to use for sending.
22+
* @type \PHPMailer
23+
*/
24+
protected $phpmailer;
25+
26+
/**
27+
* Constructor.
28+
* Note PHPMailer is *NOT* namespaced.
29+
* @param \PHPMailer $phpmailer A preconfigured PHPMailer instance to use for sending.
30+
*/
31+
public function __construct($phpmailer)
32+
{
33+
if (!$phpmailer instanceof \PHPMailer) {
34+
throw new InvalidArgumentException(
35+
'Must be an instance of \PHPMailer', 1
36+
);
37+
}
38+
39+
if (count($phpmailer->getToAddresses()) < 1) {
40+
throw new InvalidArgumentException(
41+
'No valid email address set in \PHPMailer'
42+
);
43+
}
44+
45+
$this->phpmailer = $phpmailer;
46+
}
47+
48+
/**
49+
* {@inheritDoc}
50+
*/
51+
public function write(array $log)
52+
{
53+
$this->phpmailer->Body = $log['msg'];
54+
try {
55+
return $this->phpmailer->send();
56+
} catch (\phpmailerException $e) {
57+
throw new Exception($e->getMessage(), $e->getCode(), $e);
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)