From 9b2bc802f0a0d7ead3515b428f547f3e85da0a8f Mon Sep 17 00:00:00 2001 From: Tjardo Date: Sat, 15 Jan 2022 17:00:17 +0100 Subject: [PATCH 01/17] WIP --- .github/workflows/php-cs-fixer.yml | 2 +- .github/workflows/phpstan.yml | 26 +++++ .github/workflows/run-tests.yml | 6 +- .gitignore | 5 +- .php_cs | 145 ------------------------- .php_cs.dist.php | 39 +++++++ CHANGELOG.md | 8 ++ README.md | 15 ++- composer.json | 14 +-- phpstan.neon.dist | 4 +- src/Listeners/CreateMailViewerItem.php | 8 +- 11 files changed, 101 insertions(+), 171 deletions(-) create mode 100644 .github/workflows/phpstan.yml delete mode 100644 .php_cs create mode 100644 .php_cs.dist.php diff --git a/.github/workflows/php-cs-fixer.yml b/.github/workflows/php-cs-fixer.yml index c99e1fc..75c74aa 100644 --- a/.github/workflows/php-cs-fixer.yml +++ b/.github/workflows/php-cs-fixer.yml @@ -13,7 +13,7 @@ jobs: ref: ${{ github.head_ref }} - name: Run PHP CS Fixer - uses: docker://oskarstark/php-cs-fixer-ga:2.18.6 + uses: docker://oskarstark/php-cs-fixer-ga with: args: --config=.php_cs --allow-risky=yes diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml new file mode 100644 index 0000000..3c8743a --- /dev/null +++ b/.github/workflows/phpstan.yml @@ -0,0 +1,26 @@ +name: PHPStan + +on: + push: + paths: + - '**.php' + - 'phpstan.neon.dist' + +jobs: + phpstan: + name: phpstan + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.1' + coverage: none + + - name: Install composer dependencies + uses: ramsey/composer-install@v1 + + - name: Run PHPStan + run: ./vendor/bin/phpstan --error-format=github \ No newline at end of file diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 5a0f874..2cd6fc6 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -9,12 +9,14 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest] - php: [7.4, 8.0, 8.1] - laravel: [8.*] + php: [8.0, 8.1] + laravel: [8.*, 9.*] dependency-version: [prefer-stable] include: - laravel: 8.* testbench: 6.* + - laravel: 9.* + testbench: 7.* name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} diff --git a/.gitignore b/.gitignore index 132662b..f4825fc 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,8 @@ composer.phar composer.lock .php_cs.cache -.phpunit.result.cache .DS_Store /.idea -/.vscode \ No newline at end of file +/.vscode +.phpunit.result.cache +.php-cs-fixer.cache \ No newline at end of file diff --git a/.php_cs b/.php_cs deleted file mode 100644 index 88fca70..0000000 --- a/.php_cs +++ /dev/null @@ -1,145 +0,0 @@ -exclude([ - 'node_modules', - 'vendor', - 'storage', - 'bootstrap', - 'public', - 'resources/lang' - ]) - ->in(__DIR__); - - return PhpCsFixer\Config::create() - ->setRiskyAllowed(true) - ->setRules([ - '@PSR2' => true, - 'align_multiline_comment' => [ - 'comment_type' => 'phpdocs_only', - ], - 'array_syntax' => [ - 'syntax' => 'short', - ], - 'binary_operator_spaces' => [ - 'default' => 'single_space', - ], - 'blank_line_after_namespace' => true, - 'blank_line_after_opening_tag' => true, - 'blank_line_before_statement' => [ - 'statements' => [ - 'return', - 'throw', - 'try', - ], - ], - 'braces' => [ - 'allow_single_line_closure' => true - ], - 'cast_spaces' => true, - 'class_definition' => true, - 'concat_space' => [ - 'spacing' => 'none', - ], - 'declare_equal_normalize' => true, - 'elseif' => true, - 'encoding' => true, - 'full_opening_tag' => true, - 'function_declaration' => true, - 'function_typehint_space' => true, - 'general_phpdoc_annotation_remove' => [ - 'access', - 'package', - 'subpackage', - ], - 'hash_to_slash_comment' => true, - // 'heredoc_to_nowdoc' => true, - 'include' => true, - 'indentation_type' => true, - 'line_ending' => true, - 'lowercase_cast' => true, - 'lowercase_constants' => true, - 'lowercase_keywords' => true, - 'magic_constant_casing' => true, - 'method_argument_space' => true, - 'method_separation' => true, - 'native_function_casing' => true, - 'new_with_braces' => true, - 'no_blank_lines_after_class_opening' => true, - 'no_blank_lines_after_phpdoc' => true, - 'no_closing_tag' => true, - 'no_empty_phpdoc' => true, - 'no_empty_statement' => true, - 'no_extra_consecutive_blank_lines' => [ - 'throw', - 'use', - 'useTrait', - 'extra', - ], - 'no_leading_import_slash' => true, - 'no_leading_namespace_whitespace' => true, - 'no_mixed_echo_print' => [ - 'use' => 'echo', - ], - 'no_multiline_whitespace_around_double_arrow' => true, - 'no_multiline_whitespace_before_semicolons' => true, - 'no_short_bool_cast' => true, - 'no_singleline_whitespace_before_semicolons' => true, - 'no_spaces_after_function_name' => true, - 'no_spaces_around_offset' => [ - 'inside', - 'outside', - ], - 'no_spaces_inside_parenthesis' => true, - 'no_trailing_comma_in_list_call' => true, - 'no_trailing_comma_in_singleline_array' => true, - 'no_trailing_whitespace' => true, - 'no_trailing_whitespace_in_comment' => true, - 'no_unneeded_control_parentheses' => true, - 'no_unused_imports' => true, - 'no_useless_return' => true, - 'no_whitespace_before_comma_in_array' => true, - 'no_whitespace_in_blank_line' => true, - 'normalize_index_brace' => true, - 'object_operator_without_whitespace' => true, - 'ordered_imports' => [ - 'sortAlgorithm' => 'alpha', - ], - 'phpdoc_align' => true, - 'phpdoc_indent' => true, - 'phpdoc_inline_tag' => true, - 'phpdoc_no_alias_tag' => [ - 'type' => 'var', - ], - 'phpdoc_no_useless_inheritdoc' => true, - 'phpdoc_order' => true, - 'phpdoc_scalar' => true, - 'phpdoc_separation' => true, - 'phpdoc_single_line_var_spacing' => true, - 'phpdoc_summary' => true, - 'phpdoc_to_comment' => true, - 'phpdoc_trim' => true, - 'phpdoc_types' => false, - 'phpdoc_var_without_name' => true, - 'self_accessor' => true, - 'short_scalar_cast' => true, - 'single_blank_line_at_eof' => true, - 'single_blank_line_before_namespace' => true, - 'single_class_element_per_statement' => true, - 'single_import_per_statement' => true, - 'single_line_after_imports' => true, - 'single_quote' => true, - 'space_after_semicolon' => true, - 'standardize_not_equals' => true, - 'switch_case_semicolon_to_colon' => true, - 'switch_case_space' => true, - 'ternary_operator_spaces' => true, - 'trailing_comma_in_multiline_array' => false, - 'trim_array_spaces' => true, - 'unary_operator_spaces' => true, - 'visibility_required' => [ - 'method', - 'property', - ] - ]) - ->setFinder($finder); diff --git a/.php_cs.dist.php b/.php_cs.dist.php new file mode 100644 index 0000000..0a4a56d --- /dev/null +++ b/.php_cs.dist.php @@ -0,0 +1,39 @@ +in([ + __DIR__.'/src', + ]) + ->name('*.php') + ->notName('*.blade.php') + ->ignoreDotFiles(true) + ->ignoreVCS(true); + +return (new PhpCsFixer\Config()) + ->setRules([ + '@PSR12' => true, + 'array_syntax' => ['syntax' => 'short'], + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + 'no_unused_imports' => true, + 'not_operator_with_successor_space' => true, + 'trailing_comma_in_multiline' => true, + 'phpdoc_scalar' => true, + 'unary_operator_spaces' => true, + 'binary_operator_spaces' => true, + 'blank_line_before_statement' => [ + 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], + ], + 'phpdoc_single_line_var_spacing' => true, + 'phpdoc_var_without_name' => true, + 'class_attributes_separation' => [ + 'elements' => [ + 'method' => 'one', + ], + ], + 'method_argument_space' => [ + 'on_multiline' => 'ensure_fully_multiline', + 'keep_multiple_spaces_after_comma' => true, + ], + 'single_trait_insert_per_statement' => true, + ]) + ->setFinder($finder); diff --git a/CHANGELOG.md b/CHANGELOG.md index 053d5da..0b5032e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ All notable changes to `laravel-mailviewer` will be documented in this file. +## 3.0 - 2022-01-15 + +- Laravel 9 support + +## 2.0 - 2021-12-22 + +- N/A + ## 1.0 - 2020-12-22 - Initial release diff --git a/README.md b/README.md index 6531dae..5e469a6 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,8 @@ [![Total Downloads](https://img.shields.io/packagist/dt/label84/laravel-mailviewer.svg?style=flat-square)](https://packagist.org/packages/label84/laravel-mailviewer) ![GitHub Workflow Status](https://img.shields.io/github/workflow/status/label84/laravel-mailviewer/run-tests?label=Tests&style=flat-square) -Laravel MailViewer enables you to view and filter mail that is sent by your Laravel application. It saves all sent mail to the database automatically. It also includes an overview page with the total number of mails sent grouped by Notification. +With ``laravel-mailviewer`` you can view and filter mail that is sent by your Laravel application in the browser. The package saves all mails sent to the database automatically. +You can get get an overview of all mails sent, view individual mails and get an overview of the number of mails sent grouped by Notification. ![MailViewer screenshot](./docs/screenshot_default.png?raw=true "MailViewer Screenshot") @@ -20,24 +21,26 @@ Laravel MailViewer enables you to view and filter mail that is sent by your Lara - [Commands](#commands) - [Exclude records](#exclude-records) - [Tests](#tests) -- [Security](#security) - [License](#license) ## Requirements -- Laravel 7.x or 8.x +- Laravel 8.x or 9.x - PHP >=7.4 or 8.x ## Laravel support | Version | Release | |---------|---------| +| 9.x | ^3.0 | | 8.x | ^2.0 | | 7.x | ^1.0 | ## Limitations -This package is only able to track mails send via [SwiftMailer library](https://swiftmailer.symfony.com). By default Laravel uses this library when sending mails via [Mailables](https://laravel.com/docs/8.x/mail) and [Notifications](https://laravel.com/docs/8.x/notifications). +This package is only able to track mails sent via [Symfony Mailer](https://symfony.com/doc/current/mailer). By default Laravel uses this library when sending mails via [Mailables](https://laravel.com/docs/9.x/mail) and [Notifications](https://laravel.com/docs/9.x/notifications). + +Release ``2.0`` of this package supports [Swift Mailer](https://swiftmailer.symfony.com/docs/introduction.html) the default mailer for Laravel 8.x. ## Installation @@ -151,10 +154,6 @@ composer analyse composer test ``` -## Security - -By default the package only applies the 'web and 'auth' middleware to the routes. When used in production make sure you apply additional middleware or other security measures to restrict access. - ## License [MIT](https://opensource.org/licenses/MIT) diff --git a/composer.json b/composer.json index 4ab71f5..9c5c231 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "label84/laravel-mailviewer", - "description": "A mail viewer package.", + "description": "View mails sent by Laravel in the browser", "type": "library", "license": "MIT", "authors": [ @@ -10,14 +10,14 @@ } ], "require": { - "php": "^7.4 || ^8.0", - "illuminate/auth": "^8.0", - "illuminate/routing": "^8.0", - "illuminate/support": "^8.0", - "illuminate/database": "^8.0" + "php": "^8.0", + "illuminate/auth": "^9.0", + "illuminate/routing": "^9.0", + "illuminate/support": "^9.0", + "illuminate/database": "^9.0" }, "require-dev": { - "orchestra/testbench": "^6.0", + "orchestra/testbench": "^7.0", "nunomaduro/larastan": "^1.0", "phpunit/phpunit": "^9.5" }, diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 261285f..633e9b1 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -9,7 +9,7 @@ parameters: - routes - config - # The level 8 is the highest level + # The level 9 is the highest level level: 8 ignoreErrors: @@ -21,6 +21,6 @@ parameters: path: src\Listeners\CreateMailViewerItem.php - excludes_analyse: + excludePaths: checkMissingIterableValueType: false \ No newline at end of file diff --git a/src/Listeners/CreateMailViewerItem.php b/src/Listeners/CreateMailViewerItem.php index 6e9161d..3b3d5af 100644 --- a/src/Listeners/CreateMailViewerItem.php +++ b/src/Listeners/CreateMailViewerItem.php @@ -4,8 +4,7 @@ use Illuminate\Mail\Events\MessageSent; use Label84\MailViewer\Models\MailViewerItem; -use Swift_Message; -use Swift_Mime_SimpleHeaderSet; +use Symfony\Component\Mime\Email; class CreateMailViewerItem { @@ -27,7 +26,8 @@ public function handle(MessageSent $event): void ]); } - private function formatHeaders(Swift_Mime_SimpleHeaderSet $header): array + /** @param mixed $headers */ + private function formatHeaders($header): array { if (!config('mailviewer.database.include.headers')) { return []; @@ -42,7 +42,7 @@ private function formatHeaders(Swift_Mime_SimpleHeaderSet $header): array ]; } - private function formatRecipients(Swift_Message $message): array + private function formatRecipients(Email $message): array { return array_merge( ['to' => array_keys($message->getTo() ?: [])], From 1dd178da74f99d3ab23c55194065c19ce0f7366d Mon Sep 17 00:00:00 2001 From: Tjardo Date: Sat, 15 Jan 2022 17:01:45 +0100 Subject: [PATCH 02/17] Update github workflow path --- .github/workflows/php-cs-fixer.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/php-cs-fixer.yml b/.github/workflows/php-cs-fixer.yml index 75c74aa..b9b18d9 100644 --- a/.github/workflows/php-cs-fixer.yml +++ b/.github/workflows/php-cs-fixer.yml @@ -15,7 +15,7 @@ jobs: - name: Run PHP CS Fixer uses: docker://oskarstark/php-cs-fixer-ga with: - args: --config=.php_cs --allow-risky=yes + args: --config=.php_cs.dist.php --allow-risky=yes - name: Commit changes uses: stefanzweifel/git-auto-commit-action@v4 From fdbabd89a06aa9ac66d0ab17de65fcf5e24bc4e9 Mon Sep 17 00:00:00 2001 From: tjardoo Date: Sat, 15 Jan 2022 16:02:02 +0000 Subject: [PATCH 03/17] Fix styling --- src/Console/Commands/InstallMailViewer.php | 4 ++-- src/Listeners/CreateMailViewerItem.php | 6 +++--- src/MailViewerServiceProvider.php | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Console/Commands/InstallMailViewer.php b/src/Console/Commands/InstallMailViewer.php index 73d08ab..b0dd592 100644 --- a/src/Console/Commands/InstallMailViewer.php +++ b/src/Console/Commands/InstallMailViewer.php @@ -18,12 +18,12 @@ public function handle(): void $this->call('vendor:publish', [ '--provider' => "Label84\MailViewer\MailViewerServiceProvider", - '--tag' => 'config' + '--tag' => 'config', ]); $this->call('vendor:publish', [ '--provider' => "Label84\MailViewer\MailViewerServiceProvider", - '--tag' => 'migrations' + '--tag' => 'migrations', ]); $this->info('All done!'); diff --git a/src/Listeners/CreateMailViewerItem.php b/src/Listeners/CreateMailViewerItem.php index 3b3d5af..2161568 100644 --- a/src/Listeners/CreateMailViewerItem.php +++ b/src/Listeners/CreateMailViewerItem.php @@ -10,7 +10,7 @@ class CreateMailViewerItem { public function handle(MessageSent $event): void { - if (!$this->shouldLog($event)) { + if (! $this->shouldLog($event)) { return; } @@ -29,7 +29,7 @@ public function handle(MessageSent $event): void /** @param mixed $headers */ private function formatHeaders($header): array { - if (!config('mailviewer.database.include.headers')) { + if (! config('mailviewer.database.include.headers')) { return []; } @@ -54,7 +54,7 @@ private function formatRecipients(Email $message): array private function shouldLog(MessageSent $event): bool { /* Check package enabled */ - if (!config('mailviewer.enabled')) { + if (! config('mailviewer.enabled')) { return false; } diff --git a/src/MailViewerServiceProvider.php b/src/MailViewerServiceProvider.php index 23f11ac..9cdbe17 100644 --- a/src/MailViewerServiceProvider.php +++ b/src/MailViewerServiceProvider.php @@ -21,7 +21,7 @@ public function boot(): void __DIR__.'/../resources/views' => resource_path('views/vendor/mailviewer'), ], 'views'); - if (!class_exists('CreateMailViewerItems')) { + if (! class_exists('CreateMailViewerItems')) { $this->publishes([ __DIR__.'/../database/migrations/create_mail_viewer_items_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_mail_viewer_items_table.php'), ], 'migrations'); From c9db3e13cc14ea51c9178c0952bd79b56f15417e Mon Sep 17 00:00:00 2001 From: Tjardo Date: Sat, 15 Jan 2022 17:04:30 +0100 Subject: [PATCH 04/17] Remove L8 from run-tests workflow --- .github/workflows/run-tests.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 2cd6fc6..afde7a3 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -10,11 +10,9 @@ jobs: matrix: os: [ubuntu-latest] php: [8.0, 8.1] - laravel: [8.*, 9.*] + laravel: [9.*] dependency-version: [prefer-stable] include: - - laravel: 8.* - testbench: 6.* - laravel: 9.* testbench: 7.* From a6d8fa25585fda30ca6d17d2fbce7369ca301ec4 Mon Sep 17 00:00:00 2001 From: Tjardo Date: Mon, 17 Jan 2022 20:48:43 +0100 Subject: [PATCH 05/17] Update Listener to Symfony Email --- README.md | 20 +++-------- phpstan.neon.dist | 3 -- src/Http/Controllers/MailViewerController.php | 2 +- src/Listeners/CreateMailViewerItem.php | 35 ++++++++++--------- src/Providers/EventServiceProvider.php | 4 ++- 5 files changed, 26 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 5e469a6..ffb3fb5 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ You can get get an overview of all mails sent, view individual mails and get an - [Laravel support](#laravel-support) - [Installation](#installation) - [Usage](#usage) - - [Query filters](#query-filters) + - [Filters](#Filters) - [Analytics](#analytics) - [Examples](#examples) - [Commands](#commands) @@ -52,26 +52,14 @@ Add the package to your application: composer require label84/laravel-mailviewer ``` -You can also manually update your composer.json. - -### 2. Install package - -Add the config and migration to your application. - -```sh -php artisan mailviewer:install -``` - -#### 2.1 Install package manually (alternative) - -You can also install the package manually by executing the following two commands. +### 2. Publish the config file and migration ```sh php artisan vendor:publish --provider="Label84\MailViewer\MailViewerServiceProvider" --tag="config" php artisan vendor:publish --provider="Label84\MailViewer\MailViewerServiceProvider" --tag="migrations" ``` -#### 2.2 Publish the views (optional) +#### 2.1 Publish the views (optional) To change the default views, you can publish them to your application. @@ -95,7 +83,7 @@ You can change the url in the config file. To view the content of the mail you can click on the UUID (blue link). -### Query filters +### Filters You can filter the mails in the overview with query parameters - example ``/admin/mailviewer?notification=WelcomeMail``. diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 633e9b1..4c8075d 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -16,9 +16,6 @@ parameters: - message: '#Parameter [a-zA-Z0-9\\_]+ $time of static method Carbon\Carbon::parse() expects DateTimeInterface|string|null, array|string|null given.#' path: src/Http/Controllers/MailViewerController.php - - - message: '#Cannot call method getFieldBody\(\) on Swift_Mime_Header\|null.#' - path: src\Listeners\CreateMailViewerItem.php excludePaths: diff --git a/src/Http/Controllers/MailViewerController.php b/src/Http/Controllers/MailViewerController.php index 87cf8eb..fcca44b 100644 --- a/src/Http/Controllers/MailViewerController.php +++ b/src/Http/Controllers/MailViewerController.php @@ -52,7 +52,7 @@ public function analytics(): View DB::raw('MAX(sent_at) AS last_sent_at'), ]) ->groupBy('notification') - ->orderBy('total', 'DESC') + ->orderByDesc('total') ->get(); return view('mailviewer::analytics', compact('items')); diff --git a/src/Listeners/CreateMailViewerItem.php b/src/Listeners/CreateMailViewerItem.php index 2161568..e7f38ca 100644 --- a/src/Listeners/CreateMailViewerItem.php +++ b/src/Listeners/CreateMailViewerItem.php @@ -5,40 +5,41 @@ use Illuminate\Mail\Events\MessageSent; use Label84\MailViewer\Models\MailViewerItem; use Symfony\Component\Mime\Email; +use Symfony\Component\Mime\Header\Headers; class CreateMailViewerItem { public function handle(MessageSent $event): void { - if (! $this->shouldLog($event)) { + if (!$this->shouldLog($event)) { return; } + /** @var \Symfony\Component\Mime\Email $message */ + $message = $event->message; + MailViewerItem::create([ 'event_type' => MessageSent::class, 'mailer' => config('mail.default') ?? '', - 'headers' => $this->formatHeaders($event->message->getHeaders()), + 'headers' => $this->formatHeaders($message->getHeaders()), 'notification' => $event->data['__laravel_notification'] ?? null, - 'recipients' => $this->formatRecipients($event->message), - 'subject' => $event->message->getSubject(), - 'body' => $event->message->getBody(), + 'recipients' => $this->formatRecipients($message), + 'subject' => $message->getSubject(), + 'body' => $message->getBody(), 'sent_at' => now(), ]); } - /** @param mixed $headers */ - private function formatHeaders($header): array + private function formatHeaders(Headers $headers): array { - if (! config('mailviewer.database.include.headers')) { + if (!config('mailviewer.database.include.headers')) { return []; } return [ - 'content-type' => $header->get('content-type')->getFieldBody(), - 'mime-version' => $header->get('mime-version')->getFieldBody(), - 'date' => $header->get('date')->getFieldBody(), - 'message-id' => $header->get('message-id')->getFieldBody(), - 'from' => $header->get('from')->getFieldBody(), + 'date' => $headers->get('date'), + 'message-id' => $headers->get('message-id'), + 'from' => $headers->get('from'), ]; } @@ -53,17 +54,17 @@ private function formatRecipients(Email $message): array private function shouldLog(MessageSent $event): bool { - /* Check package enabled */ - if (! config('mailviewer.enabled')) { + /* Make sure package is enabled */ + if (!config('mailviewer.enabled')) { return false; } - /* Check exclude notification list */ + /* Make sure notification is not in list of excluded notifications */ if (isset($event->data['__laravel_notification']) && in_array($event->data['__laravel_notification'], config('mailviewer.database.exclude.notification') ?? [])) { return false; } - /* Check exclude email list */ + /* Make sure recipient is not in list of exlcuded email addresses */ if ($event->message->getTo() && in_array(array_keys($event->message->getTo())[0], config('mailviewer.database.exclude.email') ?? [])) { return false; } diff --git a/src/Providers/EventServiceProvider.php b/src/Providers/EventServiceProvider.php index a75ac90..221a639 100644 --- a/src/Providers/EventServiceProvider.php +++ b/src/Providers/EventServiceProvider.php @@ -9,7 +9,9 @@ class EventServiceProvider extends ServiceProvider { protected $listen = [ - MessageSent::class => [CreateMailViewerItem::class], + MessageSent::class => [ + CreateMailViewerItem::class, + ], ]; public function boot(): void From 6aa6feb1014a635d67991d20c001c99c9ef258ca Mon Sep 17 00:00:00 2001 From: Tjardo Date: Mon, 17 Jan 2022 20:49:24 +0100 Subject: [PATCH 06/17] Remove the install command --- src/Console/Commands/InstallMailViewer.php | 31 ---------------------- 1 file changed, 31 deletions(-) delete mode 100644 src/Console/Commands/InstallMailViewer.php diff --git a/src/Console/Commands/InstallMailViewer.php b/src/Console/Commands/InstallMailViewer.php deleted file mode 100644 index b0dd592..0000000 --- a/src/Console/Commands/InstallMailViewer.php +++ /dev/null @@ -1,31 +0,0 @@ -info('Installing mail viewer package...'); - - $this->call('vendor:publish', [ - '--provider' => "Label84\MailViewer\MailViewerServiceProvider", - '--tag' => 'config', - ]); - - $this->call('vendor:publish', [ - '--provider' => "Label84\MailViewer\MailViewerServiceProvider", - '--tag' => 'migrations', - ]); - - $this->info('All done!'); - } -} From ab05c740459ac7285cd55570a65aa29f4f278f05 Mon Sep 17 00:00:00 2001 From: tjardoo Date: Mon, 17 Jan 2022 19:49:56 +0000 Subject: [PATCH 07/17] Fix styling --- src/Listeners/CreateMailViewerItem.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Listeners/CreateMailViewerItem.php b/src/Listeners/CreateMailViewerItem.php index e7f38ca..bd2072d 100644 --- a/src/Listeners/CreateMailViewerItem.php +++ b/src/Listeners/CreateMailViewerItem.php @@ -11,7 +11,7 @@ class CreateMailViewerItem { public function handle(MessageSent $event): void { - if (!$this->shouldLog($event)) { + if (! $this->shouldLog($event)) { return; } @@ -32,7 +32,7 @@ public function handle(MessageSent $event): void private function formatHeaders(Headers $headers): array { - if (!config('mailviewer.database.include.headers')) { + if (! config('mailviewer.database.include.headers')) { return []; } @@ -55,7 +55,7 @@ private function formatRecipients(Email $message): array private function shouldLog(MessageSent $event): bool { /* Make sure package is enabled */ - if (!config('mailviewer.enabled')) { + if (! config('mailviewer.enabled')) { return false; } From 35fcf7bbad53df8d1852ee7b04963328e85968db Mon Sep 17 00:00:00 2001 From: Tjardo Date: Mon, 17 Jan 2022 20:58:45 +0100 Subject: [PATCH 08/17] Remove install mailviewer command test case --- src/Listeners/CreateMailViewerItem.php | 6 ++--- tests/Commands/InstallMailViewerTest.php | 32 ------------------------ 2 files changed, 3 insertions(+), 35 deletions(-) delete mode 100644 tests/Commands/InstallMailViewerTest.php diff --git a/src/Listeners/CreateMailViewerItem.php b/src/Listeners/CreateMailViewerItem.php index e7f38ca..bd2072d 100644 --- a/src/Listeners/CreateMailViewerItem.php +++ b/src/Listeners/CreateMailViewerItem.php @@ -11,7 +11,7 @@ class CreateMailViewerItem { public function handle(MessageSent $event): void { - if (!$this->shouldLog($event)) { + if (! $this->shouldLog($event)) { return; } @@ -32,7 +32,7 @@ public function handle(MessageSent $event): void private function formatHeaders(Headers $headers): array { - if (!config('mailviewer.database.include.headers')) { + if (! config('mailviewer.database.include.headers')) { return []; } @@ -55,7 +55,7 @@ private function formatRecipients(Email $message): array private function shouldLog(MessageSent $event): bool { /* Make sure package is enabled */ - if (!config('mailviewer.enabled')) { + if (! config('mailviewer.enabled')) { return false; } diff --git a/tests/Commands/InstallMailViewerTest.php b/tests/Commands/InstallMailViewerTest.php deleted file mode 100644 index 04444f2..0000000 --- a/tests/Commands/InstallMailViewerTest.php +++ /dev/null @@ -1,32 +0,0 @@ -assertFalse(File::exists(config_path('mailviewer.php'))); - - Artisan::call('vendor:publish', [ - '--provider' => "Label84\MailViewer\MailViewerServiceProvider", - '--tag' => 'config' - ]); - - $this->assertTrue(File::exists(config_path('mailviewer.php'))); - } -} From efb8bc9fafea65d84fb6fdd21b8d17c5ef276d8d Mon Sep 17 00:00:00 2001 From: Tjardo Date: Mon, 17 Jan 2022 21:00:51 +0100 Subject: [PATCH 09/17] WIP --- src/MailViewerServiceProvider.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/MailViewerServiceProvider.php b/src/MailViewerServiceProvider.php index 9cdbe17..0ab6714 100644 --- a/src/MailViewerServiceProvider.php +++ b/src/MailViewerServiceProvider.php @@ -5,7 +5,6 @@ use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; use Label84\MailViewer\Console\Commands\CleanupMailViewerItems; -use Label84\MailViewer\Console\Commands\InstallMailViewer; use Label84\MailViewer\Providers\EventServiceProvider; class MailViewerServiceProvider extends ServiceProvider @@ -28,7 +27,6 @@ public function boot(): void } $this->commands([ - InstallMailViewer::class, CleanupMailViewerItems::class, ]); } From d170f7cffe5c65ead3fb21f546f3bae53c28a27c Mon Sep 17 00:00:00 2001 From: Tjardo Date: Mon, 17 Jan 2022 21:08:16 +0100 Subject: [PATCH 10/17] Format body to string format --- src/Listeners/CreateMailViewerItem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Listeners/CreateMailViewerItem.php b/src/Listeners/CreateMailViewerItem.php index bd2072d..5a34014 100644 --- a/src/Listeners/CreateMailViewerItem.php +++ b/src/Listeners/CreateMailViewerItem.php @@ -25,7 +25,7 @@ public function handle(MessageSent $event): void 'notification' => $event->data['__laravel_notification'] ?? null, 'recipients' => $this->formatRecipients($message), 'subject' => $message->getSubject(), - 'body' => $message->getBody(), + 'body' => $message->getBody()->toString(), 'sent_at' => now(), ]); } From 3b717ccd08ab3f2aa01e5488fe594596c7c97129 Mon Sep 17 00:00:00 2001 From: Tjardo Date: Fri, 21 Jan 2022 22:06:02 +0100 Subject: [PATCH 11/17] Remove auth from default middleware --- config/mailviewer.php | 2 +- src/Listeners/CreateMailViewerItem.php | 11 +++++++---- tests/Config/ConfigTest.php | 6 +++--- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/config/mailviewer.php b/config/mailviewer.php index 33806b7..0543aaa 100644 --- a/config/mailviewer.php +++ b/config/mailviewer.php @@ -20,7 +20,7 @@ */ 'route' => [ 'prefix' => 'admin/mailviewer', - 'middleware' => ['web', 'auth'], + 'middleware' => ['web'], ], /* diff --git a/src/Listeners/CreateMailViewerItem.php b/src/Listeners/CreateMailViewerItem.php index 5a34014..880dd96 100644 --- a/src/Listeners/CreateMailViewerItem.php +++ b/src/Listeners/CreateMailViewerItem.php @@ -3,6 +3,7 @@ namespace Label84\MailViewer\Listeners; use Illuminate\Mail\Events\MessageSent; +use Illuminate\Support\Collection; use Label84\MailViewer\Models\MailViewerItem; use Symfony\Component\Mime\Email; use Symfony\Component\Mime\Header\Headers; @@ -46,9 +47,9 @@ private function formatHeaders(Headers $headers): array private function formatRecipients(Email $message): array { return array_merge( - ['to' => array_keys($message->getTo() ?: [])], - ['cc' => array_keys($message->getCc() ?: [])], - ['bcc' => array_keys($message->getBcc() ?: [])], + ['to' => (new Collection($message->getTo()))->map(fn ($address) => $address->getAddress())->toArray()], + ['cc' => (new Collection($message->getCc()))->map(fn ($address) => $address->getAddress())->toArray()], + ['bcc' => (new Collection($message->getBcc()))->map(fn ($address) => $address->getAddress())->toArray()], ); } @@ -65,7 +66,9 @@ private function shouldLog(MessageSent $event): bool } /* Make sure recipient is not in list of exlcuded email addresses */ - if ($event->message->getTo() && in_array(array_keys($event->message->getTo())[0], config('mailviewer.database.exclude.email') ?? [])) { + if ($event->message->getTo() && count((new Collection($event->message->getTo())) + ->map(fn ($address) => $address->getAddress()) + ->rejects(fn ($email) => in_array($email, config('mailviewer.database.exclude.email') ?? []))) == 0) { return false; } diff --git a/tests/Config/ConfigTest.php b/tests/Config/ConfigTest.php index 8c62c13..d5ee373 100644 --- a/tests/Config/ConfigTest.php +++ b/tests/Config/ConfigTest.php @@ -15,7 +15,7 @@ public function setUp(): void } /** @test */ - public function it_wont_create_mail_viewer_items_when_set_disabled() + public function it_does_not_an_item_when_disabled() { config()->set('mailviewer.enabled', false); @@ -25,7 +25,7 @@ public function it_wont_create_mail_viewer_items_when_set_disabled() } /** @test */ - public function it_wont_create_mail_viewer_items_when_notification_in_exclude_notification_list() + public function it_does_not_create_an_item_when_notification_is_in_exclude_notification_list() { config()->set('mailviewer.database.exclude.notification', [ TestNotification::class, @@ -37,7 +37,7 @@ public function it_wont_create_mail_viewer_items_when_notification_in_exclude_no } /** @test */ - public function it_wont_create_mail_viewer_items_when_email_in_exclude_email_list() + public function it_does_not_create_an_item_when_email_is_in_exclude_email_list() { config()->set('mailviewer.database.exclude.email', [ 'info@example.com', From 3296211db591816797b94aca18548b0350843b10 Mon Sep 17 00:00:00 2001 From: Tjardo Date: Fri, 21 Jan 2022 22:11:00 +0100 Subject: [PATCH 12/17] Fix typo in collection method --- src/Listeners/CreateMailViewerItem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Listeners/CreateMailViewerItem.php b/src/Listeners/CreateMailViewerItem.php index 880dd96..ca32519 100644 --- a/src/Listeners/CreateMailViewerItem.php +++ b/src/Listeners/CreateMailViewerItem.php @@ -68,7 +68,7 @@ private function shouldLog(MessageSent $event): bool /* Make sure recipient is not in list of exlcuded email addresses */ if ($event->message->getTo() && count((new Collection($event->message->getTo())) ->map(fn ($address) => $address->getAddress()) - ->rejects(fn ($email) => in_array($email, config('mailviewer.database.exclude.email') ?? []))) == 0) { + ->reject(fn ($email) => in_array($email, config('mailviewer.database.exclude.email') ?? []))) == 0) { return false; } From 00225c491165a647229278583151c5beb1225151 Mon Sep 17 00:00:00 2001 From: Tjardo Date: Fri, 21 Jan 2022 22:14:45 +0100 Subject: [PATCH 13/17] Change to html body for correct formatting --- src/Listeners/CreateMailViewerItem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Listeners/CreateMailViewerItem.php b/src/Listeners/CreateMailViewerItem.php index ca32519..d34f43f 100644 --- a/src/Listeners/CreateMailViewerItem.php +++ b/src/Listeners/CreateMailViewerItem.php @@ -26,7 +26,7 @@ public function handle(MessageSent $event): void 'notification' => $event->data['__laravel_notification'] ?? null, 'recipients' => $this->formatRecipients($message), 'subject' => $message->getSubject(), - 'body' => $message->getBody()->toString(), + 'body' => $message->getHtmlBody(), 'sent_at' => now(), ]); } From 15f277610c958575428b7601efd973a23d7c0834 Mon Sep 17 00:00:00 2001 From: Tjardo Date: Fri, 21 Jan 2022 22:22:02 +0100 Subject: [PATCH 14/17] Fix tests auth middleware --- tests/Views/ViewTest.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/Views/ViewTest.php b/tests/Views/ViewTest.php index 70783c5..a326f5c 100644 --- a/tests/Views/ViewTest.php +++ b/tests/Views/ViewTest.php @@ -20,9 +20,15 @@ public function it_has_admin_mailviewer_as_default_route_prefix() } /** @test */ - public function it_has_web_and_auth_as_default_middleware() + public function it_has_web_as_default_middleware() { - $this->assertEquals(['web', 'auth'], config('mailviewer.route.middleware')); + $this->assertEquals(['web'], config('mailviewer.route.middleware')); + } + + /** @test */ + public function it_does_not_have_auth_as_default_middleware() + { + $this->assertNotContains(['auth'], config('mailviewer.route.middleware')); } /** @test */ From f63ed6a7a27434d3f0f8312713d06c5ad1102134 Mon Sep 17 00:00:00 2001 From: Tjardo Date: Fri, 21 Jan 2022 22:24:21 +0100 Subject: [PATCH 15/17] Update phpstan ignore error list --- phpstan.neon.dist | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 4c8075d..fe03069 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -16,6 +16,12 @@ parameters: - message: '#Parameter [a-zA-Z0-9\\_]+ $time of static method Carbon\Carbon::parse() expects DateTimeInterface|string|null, array|string|null given.#' path: src/Http/Controllers/MailViewerController.php + - + message: '#Unable to resolve the template type TKey in call to function collect#' + path: src/Models/MailViewerItem.php + - + message: '#Unable to resolve the template type TValue in call to function collect#' + path: src/Models/MailViewerItem.php excludePaths: From 2a31444e3b99cc42116b43632c3b568ebcaec59a Mon Sep 17 00:00:00 2001 From: Tjardo Date: Fri, 21 Jan 2022 22:41:39 +0100 Subject: [PATCH 16/17] Update readme --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ffb3fb5..edff783 100644 --- a/README.md +++ b/README.md @@ -34,13 +34,12 @@ You can get get an overview of all mails sent, view individual mails and get an |---------|---------| | 9.x | ^3.0 | | 8.x | ^2.0 | -| 7.x | ^1.0 | ## Limitations -This package is only able to track mails sent via [Symfony Mailer](https://symfony.com/doc/current/mailer). By default Laravel uses this library when sending mails via [Mailables](https://laravel.com/docs/9.x/mail) and [Notifications](https://laravel.com/docs/9.x/notifications). +This package tracks mails sent via [Symfony Mailer](https://symfony.com/doc/current/mailer). Laravel 9 uses this library by default when sending mails via [Mailables](https://laravel.com/docs/master/mail) and [Notifications](https://laravel.com/docs/master/notifications). -Release ``2.0`` of this package supports [Swift Mailer](https://swiftmailer.symfony.com/docs/introduction.html) the default mailer for Laravel 8.x. +With the upgrade to Laravel 9 the SwiftMailer has been replaced with Symfony Mailer [Upgrade Symfony Mailer](https://laravel.com/docs/master/upgrade#symfony-mailer). To add this package to a Laravel 8 application you have to use version ``^2.0`` of this package - ``composer require "label84/laravel-mailviewer:^2.0"``. ## Installation From 62ac40525345ff923addbfcf1affa0cd7af6614f Mon Sep 17 00:00:00 2001 From: Tjardo Date: Mon, 7 Feb 2022 12:28:59 +0100 Subject: [PATCH 17/17] Update readme --- CHANGELOG.md | 4 ++-- README.md | 12 ++++-------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b5032e..45c7db9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,9 @@ All notable changes to `laravel-mailviewer` will be documented in this file. -## 3.0 - 2022-01-15 +## 3.0 - 2022-02-07 -- Laravel 9 support +- Laravel 9 support (Symfony Mailer) ## 2.0 - 2021-12-22 diff --git a/README.md b/README.md index edff783..99cd4cc 100644 --- a/README.md +++ b/README.md @@ -76,11 +76,7 @@ php artisan migrate ## Usage -To preview the mails sent by your application visit: ``/admin/mailviewer`` - -You can change the url in the config file. - -To view the content of the mail you can click on the UUID (blue link). +To preview the mails sent by your application visit: ``/admin/mailviewer``. You can change this url in the config file. ### Filters @@ -104,9 +100,9 @@ The around parameter show all mails sent around the given time. By default is wi ![MailViewer Analytics screenshot](./docs/screenshot_analytics.png?raw=true "MailViewer Analytics Screenshot") -To preview the analytics page visit: ``/admin/mailviewer/analytics`` +To preview the analytics page visit: ``/admin/mailviewer/analytics``. You can change this url in the config file. -You can change the route in the config file. +On the analytics page you can view the number of mails sent per Notification and see how long ago the latest Notification was sent. ### Examples @@ -132,7 +128,7 @@ php artisan mailviewer:cleanup --days=30 ### Exclude records -In the config file you can add an array of notification classes and an array of email addresses that should be excluded. Those notifications and email addresses won't be saved to the database. +In the config file you can add an array of Notification classes and an array of email addresses that should be excluded. Those notifications and email addresses won't be saved to the database. ## Tests