Skip to content

Commit

Permalink
Add event listener
Browse files Browse the repository at this point in the history
  • Loading branch information
andreia committed Apr 9, 2024
1 parent a5a0ae3 commit e623b7a
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 15 deletions.
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/filament-maillog.jpg?t=1" width="419px" />](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

Expand Down
5 changes: 3 additions & 2 deletions config/filament-maillog.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

// config for Tapp/FilamentMailLog
return [

'amazon-ses' => [
'configuration-set' => null,
],
];
79 changes: 79 additions & 0 deletions src/Events/MailLogEventHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Tapp\FilamentMailLog\Events;

use Carbon\Carbon;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Mail\Events\MessageSending;
use Illuminate\Support\Str;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Part\DataPart;
use Tapp\FilamentMailLog\Models\MailLog;

class MailLogEventHandler
{
public function __construct(
) {
}

public function subscribe(Dispatcher $events): void
{
$events->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");
}
}
11 changes: 6 additions & 5 deletions src/FilamentMailLogServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Models/MailLog.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tapp\Models;
namespace Tapp\FilamentMailLog\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand Down

0 comments on commit e623b7a

Please sign in to comment.