├── config
├── params.php
└── di.php
├── rector.php
├── psalm80.xml
├── CHANGELOG.md
├── LICENSE.md
├── composer.json
├── src
└── EmailTarget.php
└── README.md
/config/params.php:
--------------------------------------------------------------------------------
1 | [
9 | 'emailTarget' => [
10 | 'emailTo' => 'admin@example.com',
11 | 'subjectEmail' => 'Application Log',
12 | 'levels' => [
13 | LogLevel::CRITICAL,
14 | LogLevel::EMERGENCY,
15 | LogLevel::ERROR,
16 | LogLevel::WARNING,
17 | ],
18 | ],
19 | ],
20 | ];
21 |
--------------------------------------------------------------------------------
/rector.php:
--------------------------------------------------------------------------------
1 | paths([
11 | __DIR__ . '/src',
12 | __DIR__ . '/tests',
13 | ]);
14 |
15 | // register a single rule
16 | $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
17 |
18 | // define sets of rules
19 | $rectorConfig->sets([
20 | LevelSetList::UP_TO_PHP_80,
21 | ]);
22 | };
23 |
--------------------------------------------------------------------------------
/psalm80.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/config/di.php:
--------------------------------------------------------------------------------
1 | static function (MailerInterface $mailer) use ($params) {
12 | return new EmailTarget(
13 | mailer: $mailer,
14 | emailTo: $params['yiisoft/log-target-email']['emailTarget']['emailTo'],
15 | subjectEmail: $params['yiisoft/log-target-email']['emailTarget']['subjectEmail'],
16 | levels: $params['yiisoft/log-target-email']['emailTarget']['levels'],
17 | );
18 | },
19 | ];
20 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Yii Logging Library - Email Target Change Log
2 |
3 | ## 4.1.1 under development
4 |
5 | - no changes in this release.
6 |
7 | ## 4.1.0 December 13, 2025
8 |
9 | - New #60: Add optional `$levels` parameter to `EmailTarget` constructor for log level filtering at instantiation (@samdark)
10 |
11 | ## 4.0.0 February 17, 2023
12 |
13 | - Chg #41: Adapt configuration group names to Yii conventions (@vjik)
14 | - Enh #39: In `EmailTarget` move type hints from phpdoc to constructor signature (@vjik)
15 | - Enh #39: Add support of `yiisoft/mailer` version `^4.0` (@vjik)
16 | - Enh #40: Add support of `yiisoft/mailer` version `^5.0` (@vjik)
17 |
18 | ## 3.1.0 May 23, 2022
19 |
20 | - Chg #29: Raise the minimum `yiisoft/log` version to `^2.0` and the minimum PHP version to 8.0 (@rustamwin)
21 |
22 | ## 3.0.1 August 26, 2021
23 |
24 | - Bug #28: Remove `Psr\Log\LoggerInterface` definition from configuration for using multiple targets
25 | to application (@devanych)
26 |
27 | ## 3.0.0 August 25, 2021
28 |
29 | - Chg: Use `yiisoft/mailer` version `^3.0` (@samdark)
30 |
31 | ## 2.0.0 August 24, 2021
32 |
33 | - Chg: Use `yiisoft/mailer` version `^2.0` (@samdark)
34 |
35 | ## 1.0.0 July 05, 2021
36 |
37 | Initial release.
38 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | Copyright © 2008 by Yii Software (https://www.yiiframework.com/)
2 | All rights reserved.
3 |
4 | Redistribution and use in source and binary forms, with or without
5 | modification, are permitted provided that the following conditions
6 | are met:
7 |
8 | * Redistributions of source code must retain the above copyright
9 | notice, this list of conditions and the following disclaimer.
10 | * Redistributions in binary form must reproduce the above copyright
11 | notice, this list of conditions and the following disclaimer in
12 | the documentation and/or other materials provided with the
13 | distribution.
14 | * Neither the name of Yii Software nor the names of its
15 | contributors may be used to endorse or promote products derived
16 | from this software without specific prior written permission.
17 |
18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 | COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 | POSSIBILITY OF SUCH DAMAGE.
30 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "yiisoft/log-target-email",
3 | "type": "library",
4 | "description": "Yii Logging Library - Email Target",
5 | "keywords": [
6 | "yii",
7 | "framework",
8 | "log",
9 | "logger"
10 | ],
11 | "homepage": "https://www.yiiframework.com/",
12 | "license": "BSD-3-Clause",
13 | "support": {
14 | "issues": "https://github.com/yiisoft/log-target-email/issues?state=open",
15 | "source": "https://github.com/yiisoft/log-target-email",
16 | "forum": "https://www.yiiframework.com/forum/",
17 | "wiki": "https://www.yiiframework.com/wiki/",
18 | "irc": "ircs://irc.libera.chat:6697/yii",
19 | "chat": "https://t.me/yii3en"
20 | },
21 | "funding": [
22 | {
23 | "type": "opencollective",
24 | "url": "https://opencollective.com/yiisoft"
25 | },
26 | {
27 | "type": "github",
28 | "url": "https://github.com/sponsors/yiisoft"
29 | }
30 | ],
31 | "require": {
32 | "php": "^8.0",
33 | "yiisoft/log": "^2.2",
34 | "yiisoft/mailer": "^3.0|^4.0|^5.0"
35 | },
36 | "require-dev": {
37 | "maglnet/composer-require-checker": "^4.3",
38 | "phpunit/phpunit": "^9.5",
39 | "rector/rector": "^2.0.3",
40 | "roave/infection-static-analysis-plugin": "^1.25",
41 | "spatie/phpunit-watcher": "^1.23",
42 | "vimeo/psalm": "^4.30|^5.21",
43 | "yiisoft/di": "^1.2",
44 | "yiisoft/event-dispatcher": "^1.1"
45 | },
46 | "autoload": {
47 | "psr-4": {
48 | "Yiisoft\\Log\\Target\\Email\\": "src"
49 | }
50 | },
51 | "autoload-dev": {
52 | "psr-4": {
53 | "Yiisoft\\Log\\Target\\Email\\Tests\\": "tests"
54 | }
55 | },
56 | "extra": {
57 | "config-plugin-options": {
58 | "source-directory": "config"
59 | },
60 | "config-plugin": {
61 | "di": "di.php",
62 | "params": "params.php"
63 | }
64 | },
65 | "config": {
66 | "sort-packages": true,
67 | "allow-plugins": {
68 | "infection/extension-installer": true,
69 | "composer/package-versions-deprecated": true
70 | }
71 | },
72 | "scripts": {
73 | "test": "phpunit --testdox --no-interaction",
74 | "test-watch": "phpunit-watcher watch"
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/EmailTarget.php:
--------------------------------------------------------------------------------
1 | name]`.
26 | */
27 | private array|string $emailTo;
28 |
29 | /**
30 | * @var string The email message subject.
31 | */
32 | private string $subjectEmail;
33 |
34 | /**
35 | * @param MailerInterface $mailer The mailer instance.
36 | * @param string|string[] $emailTo The receiver email address.
37 | * You may pass an array of addresses if multiple recipients should receive this message.
38 | * You may also specify receiver name in addition to email address using format: `[email => name]`.
39 | * @param string $subjectEmail The email message subject.
40 | * @param string[] $levels The {@see \Psr\Log\LogLevel log message levels} that this target is interested in.
41 | *
42 | * @throws InvalidArgumentException If the "to" email message argument is invalid.
43 | */
44 | public function __construct(
45 | private MailerInterface $mailer,
46 | array|string $emailTo,
47 | string $subjectEmail = '',
48 | array $levels = []
49 | ) {
50 | if (empty($emailTo)) {
51 | throw new InvalidArgumentException('The "to" argument must be an array or string and must not be empty.');
52 | }
53 | $this->emailTo = $emailTo;
54 | $this->subjectEmail = $subjectEmail ?: 'Application Log';
55 | parent::__construct($levels);
56 | }
57 |
58 | /**
59 | * Sends log messages to specified email addresses.
60 | *
61 | * @throws RuntimeException If the log cannot be exported.
62 | */
63 | protected function export(): void
64 | {
65 | $message = $this->mailer
66 | ->compose()
67 | ->withTo($this->emailTo)
68 | ->withSubject($this->subjectEmail)
69 | ->withTextBody(wordwrap($this->formatMessages("\n"), 70))
70 | ;
71 |
72 | try {
73 | $this->mailer->send($message);
74 | } catch (Throwable $e) {
75 | throw new RuntimeException('Unable to export log through email.', 0, $e);
76 | }
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
Yii Logging Library - Email Target
6 |
7 |
8 |
9 | [](https://packagist.org/packages/yiisoft/log-target-email)
10 | [](https://packagist.org/packages/yiisoft/log-target-email)
11 | [](https://github.com/yiisoft/log-target-email/actions/workflows/build.yml)
12 | [](https://scrutinizer-ci.com/g/yiisoft/log-target-email/?branch=master)
13 | [](https://codecov.io/gh/yiisoft/log-target-email)
14 | [](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/log-target-email/master)
15 | [](https://github.com/yiisoft/log-target-email/actions?query=workflow%3A%22static+analysis%22)
16 | [](https://shepherd.dev/github/yiisoft/log-target-email)
17 |
18 | This package provides the Email target for the [yiisoft/log](https://github.com/yiisoft/log) library.
19 |
20 | ## Requirements
21 |
22 | - PHP 8.0 or higher.
23 |
24 | ## Installation
25 |
26 | The package could be installed with [Composer](https://getcomposer.org):
27 |
28 | ```shell
29 | composer require yiisoft/log-target-email
30 | ```
31 |
32 | ## General usage
33 |
34 | Creating a target:
35 |
36 | ```php
37 | $emailTarget = new \Yiisoft\Log\Target\Email\EmailTarget($mailer, $emailTo, $subjectEmail, $levels);
38 | ```
39 |
40 | - `$mailer (\Yiisoft\Mailer\MailerInterface)` - The mailer instance that sends email and should be already configured.
41 | - `$emailTo (string|array)` - The receiver email address.
42 | You may pass an array of addresses if multiple recipients should receive this message.
43 | You may also specify receiver name in addition to email address using format: `[email => name]`.
44 | - `$subjectEmail (string)` - The email message subject. Defaults to `Application Log`.
45 | - `$levels (array)` - Optional. The log message levels that this target is interested in. Defaults to `[]` (all levels).
46 | For example: `[LogLevel::ERROR, LogLevel::WARNING]`.
47 |
48 | Example with level filtering:
49 |
50 | ```php
51 | use Psr\Log\LogLevel;
52 |
53 | // Only log ERROR and WARNING messages
54 | $emailTarget = new \Yiisoft\Log\Target\Email\EmailTarget(
55 | $mailer,
56 | 'admin@example.com',
57 | 'Application Errors',
58 | [LogLevel::ERROR, LogLevel::WARNING]
59 | );
60 | ```
61 |
62 | Creating a logger:
63 |
64 | ```php
65 | $logger = new \Yiisoft\Log\Logger([$emailTarget]);
66 | ```
67 |
68 | For use in the [Yii framework](https://www.yiiframework.com/), see the configuration files:
69 | - [`config/di.php`](https://github.com/yiisoft/log-target-email/blob/master/config/di.php)
70 | - [`config/params.php`](https://github.com/yiisoft/log-target-email/blob/master/config/params.php)
71 |
72 | ## Documentation
73 |
74 | For a description of using the logger, see the [yiisoft/log](https://github.com/yiisoft/log) package.
75 |
76 | - [Yii guide to logging](https://github.com/yiisoft/docs/blob/master/guide/en/runtime/logging.md)
77 | - [Internals](docs/internals.md)
78 |
79 | If you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is a good place
80 | for that. You may also check out other [Yii Community Resources](https://www.yiiframework.com/community).
81 |
82 | ## License
83 |
84 | The Yii Logging Library - Email Target is free software. It is released under the terms of the BSD License.
85 | Please see [`LICENSE`](./LICENSE.md) for more information.
86 |
87 | Maintained by [Yii Software](https://www.yiiframework.com/).
88 |
89 | ## Support the project
90 |
91 | [](https://opencollective.com/yiisoft)
92 |
93 | ## Follow updates
94 |
95 | [](https://www.yiiframework.com/)
96 | [](https://twitter.com/yiiframework)
97 | [](https://t.me/yii3en)
98 | [](https://www.facebook.com/groups/yiitalk)
99 | [](https://yiiframework.com/go/slack)
100 |
--------------------------------------------------------------------------------