From e623b7a4ccd73d35ae5eda14f3c389b41aeb4fb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9ia=20Bohner?= Date: Mon, 8 Apr 2024 22:44:22 -0300 Subject: [PATCH] Add event listener --- README.md | 7 --- config/filament-maillog.php | 5 +- src/Events/MailLogEventHandler.php | 79 ++++++++++++++++++++++++++ src/FilamentMailLogServiceProvider.php | 11 ++-- src/Models/MailLog.php | 2 +- 5 files changed, 89 insertions(+), 15 deletions(-) create mode 100644 src/Events/MailLogEventHandler.php diff --git a/README.md b/README.md index 9412720..c4a9916 100644 --- a/README.md +++ b/README.md @@ -7,13 +7,6 @@ This is where your description should go. Limit it to a paragraph or two. Consider adding a small example. -## Support us - -[](https://spatie.be/github-ad-click/filament-maillog) - -We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). - -We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). ## Installation diff --git a/config/filament-maillog.php b/config/filament-maillog.php index 9ec486a..ac8b68c 100644 --- a/config/filament-maillog.php +++ b/config/filament-maillog.php @@ -1,6 +1,7 @@ [ + 'configuration-set' => null, + ], ]; diff --git a/src/Events/MailLogEventHandler.php b/src/Events/MailLogEventHandler.php new file mode 100644 index 0000000..1a4fb4d --- /dev/null +++ b/src/Events/MailLogEventHandler.php @@ -0,0 +1,79 @@ +listen( + MessageSending::class, + 'Tapp\FilamentMailLog\Events\MailLogEventHandler@handleMessageSending', + ); + } + + /** + * Handle the event. + */ + public function handleMessageSending(MessageSending $event): void + { + $message = $event->message; + + $mailLog = MailLog::create([ + 'date' => Carbon::now()->format('Y-m-d H:i:s'), + 'from' => $this->formatAddressField($message, 'From'), + 'to' => $this->formatAddressField($message, 'To'), + 'cc' => $this->formatAddressField($message, 'Cc'), + 'bcc' => $this->formatAddressField($message, 'Bcc'), + 'subject' => $message->getSubject(), + 'body' => $message->getHtmlBody(), + 'headers' => $message->getHeaders()->toString(), + 'attachments' => $this->saveAttachments($message), + 'message_id' => Str::uuid(), + ]); + + if (config('filament-maillog.amazon-ses.configuration-set') !== null) { + $event->message->getHeaders()->addTextHeader('X-SES-CONFIGURATION-SET', config('filament-maillog.amazon-ses.configuration-set')); + } + + $event->message->getHeaders()->addTextHeader('unique-id', $mailLog->message_id); + } + + /** + * Format address strings for sender, to, cc, bcc. + */ + public function formatAddressField(Email $message, string $field): ?string + { + $headers = $message->getHeaders(); + + return $headers->get($field)?->getBodyAsString(); + } + + /** + * Collect all attachments and format them as strings. + */ + protected function saveAttachments(Email $message): ?string + { + if (empty($message->getAttachments())) { + return null; + } + + return collect($message->getAttachments()) + ->map(fn (DataPart $part) => $part->toString()) + ->implode("\n\n"); + } +} diff --git a/src/FilamentMailLogServiceProvider.php b/src/FilamentMailLogServiceProvider.php index 5890462..b063d11 100644 --- a/src/FilamentMailLogServiceProvider.php +++ b/src/FilamentMailLogServiceProvider.php @@ -4,20 +4,21 @@ use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; +use Tapp\FilamentMailLog\Events\MailLogEventHandler; class FilamentMailLogServiceProvider extends PackageServiceProvider { public function configurePackage(Package $package): void { - /* - * This class is a Package Service Provider - * - * More info: https://github.com/spatie/laravel-package-tools - */ $package ->name('filament-maillog') ->hasConfigFile() ->hasViews() ->hasMigration('create_filament-mail_log_table'); } + + public function packageBooted(): void + { + $this->app['events']->subscribe(MailLogEventHandler::class); + } } diff --git a/src/Models/MailLog.php b/src/Models/MailLog.php index ffb6055..6d50401 100644 --- a/src/Models/MailLog.php +++ b/src/Models/MailLog.php @@ -1,6 +1,6 @@