Last active
February 4, 2022 15:06
-
-
Save onlime/79ec3693281066c5fa997e34bf05f00a to your computer and use it in GitHub Desktop.
Log emails as *.eml in Laravel Mailer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use App\Listeners\LogSentMessage; | |
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; | |
use Illuminate\Mail\Events\MessageSent; | |
class EventServiceProvider extends ServiceProvider | |
{ | |
/** | |
* The event listener mappings for the application. | |
* | |
* @var array | |
*/ | |
protected $listen = [ | |
MessageSent::class => [ | |
LogSentMessage::class, | |
], | |
]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
return [ | |
// ... | |
'disks' => [ | |
// ... | |
'emails' => [ | |
'driver' => 'local', | |
'root' => storage_path('logs/emails'), | |
], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Listeners; | |
use Illuminate\Mail\Events\MessageSent; | |
use Illuminate\Support\Facades\Storage; | |
use Illuminate\Support\Str; | |
class LogSentMessage | |
{ | |
public function handle(MessageSent $event) | |
{ | |
$messageId = $event->data['__laravel_notification_id'] ?? Str::uuid(); | |
Storage::disk('emails')->put( | |
sprintf('%s_%s.eml', now()->format('YmdHis'), $messageId), | |
$event->message->toString() | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment