Skip to content

Instantly share code, notes, and snippets.

@onlime
Last active February 4, 2022 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onlime/79ec3693281066c5fa997e34bf05f00a to your computer and use it in GitHub Desktop.
Save onlime/79ec3693281066c5fa997e34bf05f00a to your computer and use it in GitHub Desktop.
Log emails as *.eml in Laravel Mailer
<?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,
],
];
}
<?php
return [
// ...
'disks' => [
// ...
'emails' => [
'driver' => 'local',
'root' => storage_path('logs/emails'),
],
<?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