Created
October 17, 2020 15:18
-
-
Save eusonlito/773bd0ef3de74ba998e0fb6f8cc42d9a to your computer and use it in GitHub Desktop.
Laravel Mail Logger
This file contains 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 declare(strict_types=1); | |
namespace App\Providers; | |
use Illuminate\Support\ServiceProvider; | |
use App\Services\Mail\Logger as LoggerMail; | |
class Debug extends ServiceProvider | |
{ | |
/** | |
* @return void | |
*/ | |
public function boot() | |
{ | |
$this->logging(); | |
} | |
/** | |
* @return void | |
*/ | |
protected function logging(): void | |
{ | |
$this->loggingMail(); | |
} | |
/** | |
* @return void | |
*/ | |
protected function loggingMail(): void | |
{ | |
if (config('logging.channels.mail.enabled')) { | |
LoggerMail::listen(); | |
} | |
} | |
} |
This file contains 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 declare(strict_types=1); | |
namespace App\Services\Mail; | |
use Illuminate\Mail\Events\MessageSending; | |
use Illuminate\Support\Facades\Event; | |
class Logger | |
{ | |
/** | |
* @return void | |
*/ | |
public static function listen(): void | |
{ | |
Event::listen(MessageSending::class, static fn ($event) => static::store($event)); | |
} | |
/** | |
* @param \Illuminate\Mail\Events\MessageSending $event | |
* | |
* @return void | |
*/ | |
protected static function store(MessageSending $event): void | |
{ | |
$file = storage_path('logs/mails/'.date('Y-m-d/H-i-s').'-'.uniqid().'.log'); | |
$dir = dirname($file); | |
if (is_dir($dir) === false) { | |
mkdir($dir, 0755, true); | |
} | |
file_put_contents($file, (string)$event->message, LOCK_EX); | |
} | |
} |
This file contains 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 [ | |
'providers' => [ | |
/* | |
* Application Service Providers... | |
*/ | |
App\Providers\Debug::class, | |
], | |
]; |
This file contains 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 [ | |
'channels' => [ | |
'mail' => [ | |
'enabled' => env('LOG_MAIL', false), | |
], | |
], | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment