From 7aecc32db9105cd2cc156df82550b291f7efafae Mon Sep 17 00:00:00 2001 From: Karel FAILLE Date: Thu, 27 Feb 2020 13:14:17 +0100 Subject: [PATCH] feat: support multiple to configuration formats --- README.md | 49 +++++++++++++++++++++++++++++++++++----------- src/MailLogger.php | 49 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 76 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index cac339c..76631a5 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,6 @@ A service provider to add support for logging via email using Laravels built-in This package is a fork of [laravel-log-mailer](https://packagist.org/packages/designmynight/laravel-log-mailer) by Steve Porter. -**This fork introduces some breaking changes. If you're upgrading from it, please follow the [configuration](#configuration) bellow.** - ![image](https://user-images.githubusercontent.com/12199424/45576336-a93c1300-b86e-11e8-9575-d1e4c5ed5dec.png) @@ -33,8 +31,8 @@ composer require shaffe/laravel-mail-log-channel Laravel | Package | :---------|:--------| - 7.x | 2.0.x | - 6.x | 2.0.x | + 7.x | 1.1.x | + 6.x | 1.1.x | 5.6.x | 1.0.x | The package will automatically register itself if you use Laravel. @@ -77,15 +75,13 @@ To ensure all unhandled exceptions are mailed: ] ], - // Optionally overwrite the sender. - // Default is config('mail.from.address') and config('mail.from.name') - // 'from' => [ - // 'address' => env('LOG_MAIL_ADDRESS'), - // 'name' => 'Errors' - // ], + 'from' => [ + 'address' => env('LOG_MAIL_ADDRESS'), + 'name' => 'Errors' + ], // Optionally overwrite the subject format pattern - // 'subject_format' => env('LOG_MAIL_SUBJECT_FORMAT', '[%datetime%] %level_name%: %message%'), + 'subject_format' => env('LOG_MAIL_SUBJECT_FORMAT', '[%datetime%] %level_name%: %message%'), // Optionally overwrite the mailable template // Two variables are sent to the view: `string $content` and `array $records` @@ -94,4 +90,35 @@ To ensure all unhandled exceptions are mailed: ], ``` +The following `to` config formats are supported: + +* single email address: + + ```php + 'to' => env('LOG_MAIL_ADDRESS', ''), + ``` + +* array of email addresses: + + ```php + 'to' => explode(',', env('LOG_MAIL_ADDRESS', '')), + ``` + +* associative array of email/name addresses: + + ```php + 'to' => [env('LOG_MAIL_ADDRESS', '') => 'Error'],` + ``` + +* an array of email and name: + + ```php + 'to' => [ + [ + 'address' => env('LOG_MAIL_ADDRESS', ''), + 'name' => 'Error', + ], + ], + ``` + You can specify multiple channels and change the recipients and customize the email template per channel. diff --git a/src/MailLogger.php b/src/MailLogger.php index e6aa947..769ac92 100644 --- a/src/MailLogger.php +++ b/src/MailLogger.php @@ -52,21 +52,17 @@ public function __invoke(array $config) */ protected function buildMailable(): Mailable { - $mailable = $this->config('mailable') ?? MailableLog::class; - $mailable = new $mailable(); + $mailableClass = $this->config('mailable') ?? MailableLog::class; + /** @var \Illuminate\Contracts\Mail\Mailable $mailable */ + $mailable = new $mailableClass(); - if (! ($recipients = $this->config('to'))) { - throw new InvalidArgumentException('"To" address is required.'); + if (! ($recipients = $this->buildRecipients())) { + throw new InvalidArgumentException('"To" address is required. Please check the `to` driver\'s logging config.'); } - foreach ($recipients as $recipient) { - $mailable->to( - $recipient['address'], - $recipient['name'] - ); - } + $mailable->to($recipients); - if (! $this->defaultFromAddress()) { + if (! $this->defaultFromAddress() && ! isset($this->config('from')['address'])) { throw new InvalidArgumentException('"From" address is required. Please check the `from.address` driver\'s config and the `mail.from.address` config.'); } @@ -78,6 +74,37 @@ protected function buildMailable(): Mailable return $mailable; } + protected function buildRecipients(): array + { + if (! ($to = $this->config('to'))) { + return []; + } + + $recipients = []; + foreach ((array)$to as $emailOrIndex => $nameOrEmail) { + if (is_array($nameOrEmail)) { + $email = $nameOrEmail['email'] ?? $nameOrEmail['address'] ?? null; + if ($email) { + $recipients[] = [ + 'email' => $email, + 'name' => $nameOrEmail['name'] ?? null, + ]; + } + } elseif (is_string($emailOrIndex)) { + $recipients[] = [ + 'email' => $emailOrIndex, + 'name' => $nameOrEmail, + ]; + } elseif (is_string($nameOrEmail)) { + $recipients[] = [ + 'email' => $nameOrEmail, + 'name' => null, + ]; + } + } + + return $recipients; + } /** * Get the default from address.